* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

:root {
  --bg-primary: #1a1a2e;
  --bg-secondary: #16213e;
  --bg-tertiary: #0f3460;
  --accent: #e94560;
  --accent-hover: #ff6b6b;
  --text-primary: #eee;
  --text-secondary: #aaa;
  --border: #333;
  --shadow: rgba(0, 0, 0, 0.3);
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  overflow: hidden;
  height: 100vh;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100vh;
}

/* Top Bar */
.top-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 16px;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
  gap: 16px;
}

.logo {
  display: flex;
  align-items: center;
  gap: 8px;
}

.logo-icon {
  font-size: 24px;
}

.logo-text {
  font-size: 18px;
  font-weight: 600;
}

.toolbar-options {
  display: flex;
  align-items: center;
  gap: 16px;
}

.toolbar-checkbox {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  cursor: pointer;
  color: var(--text-secondary);
  transition: color 0.2s;
}

.toolbar-checkbox:hover {
  color: var(--text-primary);
}

.toolbar-checkbox input {
  accent-color: var(--accent);
}

.toolbar-checkbox input:checked + span {
  color: var(--text-primary);
}

.toolbar-actions {
  display: flex;
  align-items: center;
  gap: 4px;
}

.action-btn {
  padding: 6px 10px;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--text-secondary);
  cursor: pointer;
  font-size: 12px;
  transition: all 0.2s;
}

.action-btn:hover {
  background: var(--bg-tertiary);
  border-color: var(--accent);
  color: var(--text-primary);
}

/* Button Group */
.btn-group {
  display: flex;
}

.btn-group .action-btn:first-child {
  border-radius: 4px 0 0 4px;
  border-right: none;
}

.btn-group .action-btn:last-child {
  border-radius: 0 4px 4px 0;
}

.auto-btn {
  background: #10b981;
  color: white;
  font-weight: bold;
}

.auto-btn:hover {
  background: #059669;
}

.validate-btn {
  background: var(--accent);
  color: white;
  font-weight: bold;
}

.validate-btn:hover {
  background: var(--accent-hover);
}

/* View Toggle Button in sidebar */
.view-toggle-btn {
  display: block;
  width: 100%;
  margin-top: 8px;
  padding: 6px 10px;
  background: #6366f1;
  border: 1px solid #4f46e5;
  border-radius: 4px;
  color: white;
  font-size: 12px;
  cursor: pointer;
  transition: all 0.2s;
}

.view-toggle-btn:hover {
  background: #4f46e5;
}

.separator {
  width: 1px;
  height: 24px;
  background: var(--border);
  margin: 0 8px;
}

.lang-selector {
  display: flex;
  gap: 4px;
}

.lang-btn {
  padding: 6px 12px;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--text-secondary);
  cursor: pointer;
  font-size: 12px;
  transition: all 0.2s;
}

.lang-btn:hover,
.lang-btn.active {
  background: var(--accent);
  border-color: var(--accent);
  color: white;
}

/* Main Content */
.main-content {
  display: flex;
  flex: 1;
  overflow: hidden;
}

/* Sidebars */
.sidebar,
.sidebar-right {
  width: 260px;
  background: var(--bg-secondary);
  border-right: 1px solid var(--border);
  padding: 8px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.sidebar-right {
  border-right: none;
  border-left: 1px solid var(--border);
  width: 160px;
}

.tool-section {
  background: var(--bg-tertiary);
  border-radius: 6px;
  padding: 8px;
}

.tool-section h3 {
  font-size: 11px;
  text-transform: uppercase;
  color: var(--text-secondary);
  margin-bottom: 6px;
  letter-spacing: 0.5px;
}

/* Drawing Tools */
.tool-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
}

.tool-grid-wide {
  display: flex;
  flex-wrap: wrap;
  gap: 3px;
}

.draw-tool {
  width: 28px;
  height: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.2s;
}

.draw-tool:hover {
  border-color: var(--accent);
  transform: scale(1.05);
}

.draw-tool.active {
  border-color: var(--accent);
  background: var(--accent);
}

.rib-tool,
.main-tool {
  padding: 8px 12px;
  background: var(--bg-secondary);
  border: 2px solid var(--border);
  border-radius: 6px;
  color: var(--text-primary);
  font-size: 12px;
  font-weight: bold;
  cursor: pointer;
  transition: all 0.2s;
}

.rib-tool:hover,
.main-tool:hover {
  border-color: var(--accent);
  transform: scale(1.05);
}

.rib-tool.active,
.main-tool.active {
  border-color: var(--accent);
  background: var(--accent);
  color: white;
}

/* Grid Settings */
.grid-settings {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.grid-settings label {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 13px;
}

.grid-settings input,
.grid-settings select {
  width: 80px;
  padding: 6px 8px;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--text-primary);
  text-align: right;
}

.grid-settings select {
  cursor: pointer;
}

/* Color Palette */
.color-palette {
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-height: 300px;
  overflow-y: auto;
}

.feeder-group {
  display: flex;
  align-items: center;
  gap: 8px;
}

.feeder-label {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-secondary);
  min-width: 36px;
}

.feeder-swatches {
  display: flex;
  gap: 4px;
  flex-wrap: wrap;
}

.add-color-btn {
  width: 28px;
  height: 28px;
  border-radius: 4px;
  border: 2px dashed var(--border);
  background: transparent;
  color: var(--text-secondary);
  font-size: 16px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s;
}

.add-color-btn:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.color-swatch {
  width: 32px;
  height: 32px;
  border-radius: 4px;
  cursor: pointer;
  border: 2px solid transparent;
  transition: all 0.2s;
  position: relative;
}

.color-swatch:hover {
  transform: scale(1.1);
}

.color-swatch.active {
  border-color: white;
  box-shadow: 0 0 0 2px var(--accent);
}

.color-swatch.eraser-swatch {
  border: 2px dashed #666;
}

.color-swatch.eraser-swatch .color-num {
  color: #333;
  text-shadow: none;
  font-size: 12px;
  bottom: 3px;
  right: 4px;
}

/* Dual swatch - machine | design side by side */
.color-swatch.dual-swatch {
  display: flex;
  gap: 4px;
  padding: 2px;
  width: auto;
  min-width: 60px;
}

.color-box {
  width: 24px;
  height: 24px;
  border-radius: 4px;
  border: 2px solid transparent;
  cursor: pointer;
}

.color-box.machine {
  cursor: default;
  opacity: 0.9;
}

.color-box.design {
  border: 2px dashed rgba(255,255,255,0.3);
}

.color-box.design:hover {
  border-color: var(--accent);
}

.color-box.current {
  border: 2px solid white;
  box-shadow: 0 0 4px rgba(255,255,255,0.5);
}

.dual-swatch .color-num {
  position: static;
  display: block;
  text-align: center;
  font-size: 10px;
  margin-top: 2px;
}

.color-num {
  position: absolute;
  bottom: 1px;
  right: 2px;
  font-size: 9px;
  font-weight: bold;
  color: white;
  text-shadow: 0 0 2px black, 0 0 2px black;
  pointer-events: none;
}

.color-swatch .delete-btn {
  position: absolute;
  top: -6px;
  right: -6px;
  width: 14px;
  height: 14px;
  background: var(--accent);
  border: none;
  border-radius: 50%;
  color: white;
  font-size: 10px;
  cursor: pointer;
  display: none;
  align-items: center;
  justify-content: center;
  line-height: 1;
}

.color-swatch:hover .delete-btn {
  display: flex;
}

.palette-actions {
  margin-top: 8px;
}

/* Options */
.options {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.checkbox-label {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  cursor: pointer;
}

.checkbox-label input {
  accent-color: var(--accent);
}

/* Zoom Controls */
.zoom-controls {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
}

#zoom-level {
  font-size: 14px;
  min-width: 50px;
  text-align: center;
}

/* Buttons */
.btn-primary {
  padding: 8px 16px;
  background: var(--accent);
  border: none;
  border-radius: 6px;
  color: white;
  font-size: 13px;
  cursor: pointer;
  transition: all 0.2s;
  width: 100%;
}

.btn-primary:hover {
  background: var(--accent-hover);
}

.btn-secondary {
  padding: 8px 16px;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text-primary);
  font-size: 13px;
  cursor: pointer;
  transition: all 0.2s;
}

.btn-secondary:hover {
  border-color: var(--text-secondary);
}

.btn-small {
  padding: 6px 12px;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--text-primary);
  font-size: 12px;
  cursor: pointer;
  transition: all 0.2s;
}

.btn-small:hover {
  border-color: var(--accent);
}

/* Canvas Area */
.canvas-area {
  flex: 1;
  background: #222;
  overflow: auto;
  position: relative;
}

.canvas-wrapper {
  position: relative;
  background: white;
  box-shadow: 0 4px 20px var(--shadow);
  margin: 20px;
}

#pattern-canvas,
#grid-canvas,
#preview-canvas {
  position: absolute;
  top: 0;
  left: 0;
  image-rendering: pixelated;
}

#pattern-canvas {
  z-index: 1;
}

#grid-canvas {
  z-index: 2;
  pointer-events: none;
}

#preview-canvas {
  z-index: 3;
  pointer-events: none;
}

/* Current Color Display */
.current-color {
  width: 100%;
  height: 60px;
  border-radius: 6px;
  border: 2px solid var(--border);
  margin-bottom: 8px;
}

#current-color-name {
  font-size: 14px;
  color: var(--text-secondary);
}

/* Position Info */
.position-info {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-family: monospace;
  font-size: 14px;
}

/* History */
.history-controls {
  display: flex;
  gap: 4px;
  margin-bottom: 8px;
}

.history-controls .btn-small {
  flex: 1;
}

.history-info {
  text-align: center;
  font-size: 12px;
  color: var(--text-secondary);
}

/* Footer */
.footer {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 16px;
  padding: 8px;
  background: var(--bg-secondary);
  border-top: 1px solid var(--border);
  font-size: 12px;
  color: var(--text-secondary);
}

.footer a {
  color: var(--accent);
  text-decoration: none;
}

.footer a:hover {
  text-decoration: underline;
}

.footer .kofi-link {
  background: #ff5e5b;
  color: #fff;
  padding: 3px 10px;
  border-radius: 4px;
  font-weight: 600;
  text-decoration: none;
}

.footer .kofi-link:hover {
  background: #e04e4b;
  text-decoration: none;
}

/* Modal */
.modal {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.7);
  z-index: 1000;
  align-items: center;
  justify-content: center;
}

.modal.show {
  display: flex;
}

.modal-content {
  background: var(--bg-secondary);
  border-radius: 12px;
  padding: 24px;
  min-width: 300px;
  box-shadow: 0 8px 32px var(--shadow);
}

.modal-content h2 {
  margin-bottom: 16px;
  font-size: 18px;
}

.modal-body {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-bottom: 20px;
}

.modal-body label {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.modal-body input,
.modal-body select {
  width: 120px;
  padding: 8px 12px;
  background: var(--bg-primary);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text-primary);
  text-align: right;
}

.modal-body select {
  cursor: pointer;
}

.modal-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
}

.modal-actions button {
  min-width: 80px;
}

/* Scrollbar */
::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}

::-webkit-scrollbar-track {
  background: var(--bg-primary);
}

::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
  background: var(--text-secondary);
}

/* Responsive */
@media (max-width: 900px) {
  .tool-btn .label {
    display: none;
  }
  
  .sidebar,
  .sidebar-right {
    width: 180px;
  }
}

@media (max-width: 700px) {
  .sidebar-right {
    display: none;
  }
}

/* Ad Banner in Sidebar */
.ad-banner {
  margin-top: auto;
  padding: 12px;
  border-top: 1px solid var(--border);
}

.ad-placeholder {
  background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
  border: 2px dashed var(--border);
  border-radius: 8px;
  padding: 24px;
  text-align: center;
  color: var(--text-secondary);
}

.ad-placeholder span {
  display: block;
  font-size: 24px;
  font-weight: bold;
  color: var(--accent);
}

.ad-placeholder small {
  font-size: 11px;
}

/* Ad Modal */
.ad-modal .modal-content {
  max-width: 400px;
}

.ad-content {
  text-align: center;
}

.ad-header {
  font-size: 24px;
  font-weight: bold;
  margin-bottom: 16px;
  color: var(--accent);
}

.ad-video-placeholder {
  background: linear-gradient(135deg, #0a0a15 0%, #1a1a2e 100%);
  border: 2px solid var(--border);
  border-radius: 8px;
  padding: 60px 24px;
  margin-bottom: 16px;
}

.ad-video-placeholder span {
  display: block;
  font-size: 32px;
  font-weight: bold;
  color: var(--accent);
}

.ad-video-placeholder small {
  color: var(--text-secondary);
  font-size: 12px;
}

.ad-countdown {
  font-size: 18px;
  margin-bottom: 16px;
  color: var(--text-primary);
}

.ad-countdown span {
  font-size: 28px;
  font-weight: bold;
  color: #10b981;
}

.ad-footer {
  margin-bottom: 12px;
}

.ad-footer button {
  width: 100%;
  padding: 12px 24px;
  font-size: 16px;
}

.ad-footer button:disabled {
  background: #4b5563;
  cursor: not-allowed;
}

.ad-tip {
  font-size: 12px;
  color: var(--text-secondary);
}

.ad-tip a {
  color: var(--accent);
  text-decoration: underline;
}

/* Help Button */
.help-btn {
  background: transparent;
  border: 2px solid var(--border);
  border-radius: 50%;
  width: 36px;
  height: 36px;
  font-size: 18px;
  cursor: pointer;
  transition: all 0.2s;
  margin-left: 8px;
}

.help-btn:hover {
  background: var(--bg-tertiary);
  border-color: var(--accent);
}

/* Help Modal */
.help-modal .modal-content {
  max-width: 520px;
  max-height: 80vh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.help-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-bottom: 16px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 16px;
}

.help-header h2 {
  margin: 0;
  font-size: 20px;
}

.help-close {
  background: transparent;
  border: none;
  color: var(--text-secondary);
  font-size: 24px;
  cursor: pointer;
  padding: 4px 8px;
  line-height: 1;
}

.help-close:hover {
  color: var(--accent);
}

.help-body {
  overflow-y: auto;
  padding-right: 8px;
}

.help-section {
  margin-bottom: 20px;
}

.help-section h3 {
  font-size: 15px;
  color: var(--accent);
  margin-bottom: 8px;
  padding-bottom: 4px;
  border-bottom: 1px solid var(--border);
}

.help-section ul {
  list-style: none;
  padding: 0;
}

.help-section li {
  padding: 4px 0;
  font-size: 13px;
  color: var(--text-primary);
}

.help-section li b {
  color: #10b981;
}

.help-section p {
  font-size: 13px;
  color: var(--text-secondary);
}

.help-tip {
  background: var(--bg-tertiary);
  padding: 8px 12px;
  border-radius: 6px;
  margin-top: 8px;
  font-size: 12px;
}
