/* ==========================================================================
   Pixel Art Studio - Design System & Modern Responsive Styling
   Targeting Safari on macOS (mandatory) and iOS (desired)
   ========================================================================== */

:root {
  --bg-primary: #121418;
  --bg-surface: #1a1d24;
  --bg-surface-elevated: #222630;
  --bg-surface-hover: #2c3240;
  --bg-input: #15181e;

  --border-subtle: #2d3340;
  --border-strong: #3e4656;
  --border-focus: #5b8bf7;

  --text-primary: #f0f3f8;
  --text-secondary: #9aa4b5;
  --text-muted: #657084;
  --text-accent: #6aa1ff;

  --accent-primary: #3b74e6;
  --accent-hover: #4c82f0;
  --accent-active: #2f62c9;
  --accent-text: #ffffff;

  --danger-primary: #e04747;
  --danger-hover: #f05858;
  --danger-bg: rgba(224, 71, 71, 0.15);

  --success-primary: #2eb872;
  --warning-primary: #f5a623;

  --radius-sm: 6px;
  --radius-md: 8px;
  --radius-lg: 12px;

  --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.3);
  --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.4);
  --shadow-lg: 0 8px 24px rgba(0, 0, 0, 0.5);

  --font-sans: -apple-system, BlinkMacSystemFont, "SF Pro Display", "SF Pro Text", "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --font-mono: "SF Mono", Monaco, "Cascadia Code", "Courier New", monospace;

  --header-height: 52px;
  --footer-height: 28px;
  --toolbar-width: 54px;
  --sidebar-width: 320px;
}

/* Reset and Base */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html, body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background-color: var(--bg-primary);
  color: var(--text-primary);
  font-family: var(--font-sans);
  font-size: 13px;
  line-height: 1.4;
  -webkit-font-smoothing: antialiased;
  -webkit-touch-callout: none;
}

/* App Layout Grid */
#app-container {
  display: grid;
  grid-template-rows: var(--header-height) 1fr var(--footer-height);
  grid-template-columns: var(--toolbar-width) 1fr var(--sidebar-width);
  grid-template-areas:
    "header header header"
    "toolbar canvas sidebar"
    "footer footer footer";
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
}

/* ==========================================================================
   Header Bar
   ========================================================================== */
#app-header {
  grid-area: header;
  background-color: var(--bg-surface);
  border-bottom: 1px solid var(--border-subtle);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  z-index: 20;
  gap: 12px;
}

.header-left, .header-center, .header-right {
  display: flex;
  align-items: center;
  gap: 8px;
}

.app-brand {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 700;
  font-size: 15px;
  letter-spacing: -0.3px;
  color: var(--text-primary);
  user-select: none;
}

.brand-icon {
  width: 22px;
  height: 22px;
  display: inline-grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  gap: 1px;
  background: var(--bg-input);
  padding: 2px;
  border-radius: 4px;
  border: 1px solid var(--border-strong);
}

.brand-icon span {
  background: var(--accent-primary);
  border-radius: 1px;
}
.brand-icon span:nth-child(2) { background: #f5a623; }
.brand-icon span:nth-child(4) { background: #2eb872; }
.brand-icon span:nth-child(6) { background: #e04747; }
.brand-icon span:nth-child(8) { background: #9b51e0; }

.header-divider {
  width: 1px;
  height: 22px;
  background-color: var(--border-subtle);
  margin: 0 4px;
}

/* Dimension Badge Button */
.dimension-badge {
  background: var(--bg-input);
  border: 1px solid var(--border-subtle);
  color: var(--text-secondary);
  padding: 5px 10px;
  border-radius: var(--radius-sm);
  font-family: var(--font-mono);
  font-size: 12px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 6px;
  transition: all 0.15s ease;
}

.dimension-badge:hover {
  border-color: var(--border-strong);
  color: var(--text-primary);
  background: var(--bg-surface-elevated);
}

.dimension-badge .edit-hint {
  font-size: 10px;
  color: var(--text-muted);
  text-transform: uppercase;
}

/* Header Action Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  background: var(--bg-surface-elevated);
  border: 1px solid var(--border-subtle);
  color: var(--text-primary);
  padding: 6px 12px;
  border-radius: var(--radius-sm);
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  user-select: none;
  transition: all 0.15s ease;
  white-space: nowrap;
}

.btn:hover {
  background: var(--bg-surface-hover);
  border-color: var(--border-strong);
}

.btn:active {
  transform: translateY(1px);
}

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

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

.btn-danger {
  background: var(--danger-bg);
  border-color: rgba(224, 71, 71, 0.4);
  color: #ff7878;
}

.btn-danger:hover {
  background: var(--danger-primary);
  color: #ffffff;
  border-color: var(--danger-primary);
}

.btn-sm {
  padding: 4px 8px;
  font-size: 11px;
}

.btn-icon-only {
  padding: 6px;
  width: 32px;
  height: 32px;
}

/* ==========================================================================
   Left Toolbar
   ========================================================================== */
#app-toolbar {
  grid-area: toolbar;
  background-color: var(--bg-surface);
  border-right: 1px solid var(--border-subtle);
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 10px 0;
  gap: 6px;
  z-index: 15;
  overflow-y: auto;
}

.tool-group {
  display: flex;
  flex-direction: column;
  gap: 4px;
  width: 100%;
  align-items: center;
  padding: 0 6px;
}

.toolbar-divider {
  width: 32px;
  height: 1px;
  background-color: var(--border-subtle);
  margin: 4px 0;
}

.tool-btn {
  width: 38px;
  height: 38px;
  background: transparent;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  color: var(--text-secondary);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  position: relative;
  transition: all 0.12s ease;
}

.tool-btn svg {
  width: 18px;
  height: 18px;
  fill: currentColor;
}

.tool-btn:hover {
  background: var(--bg-surface-elevated);
  color: var(--text-primary);
}

.tool-btn.active {
  background: var(--accent-primary);
  color: #ffffff;
  border-color: var(--accent-hover);
}

.tool-btn[disabled] {
  opacity: 0.35;
  cursor: not-allowed;
  pointer-events: none;
}

/* Native Tooltips */
.tooltip-wrap {
  position: relative;
}

/* ==========================================================================
   Center Viewport / Canvas
   ========================================================================== */
#canvas-viewport-container {
  grid-area: canvas;
  position: relative;
  background-color: var(--bg-primary);
  overflow: hidden;
  width: 100%;
  height: 100%;
}

/* Floating Viewport Controls (Zoom in / out / fit) */
.viewport-floating-controls {
  position: absolute;
  bottom: 16px;
  left: 16px;
  display: flex;
  align-items: center;
  background: rgba(26, 29, 36, 0.88);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-md);
  padding: 4px;
  gap: 2px;
  box-shadow: var(--shadow-md);
  z-index: 10;
}

.zoom-btn {
  background: transparent;
  border: none;
  color: var(--text-secondary);
  width: 28px;
  height: 28px;
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  font-size: 14px;
  font-weight: bold;
  transition: all 0.15s ease;
}

.zoom-btn:hover {
  background: var(--bg-surface-hover);
  color: var(--text-primary);
}

.zoom-label {
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--text-primary);
  padding: 0 6px;
  min-width: 48px;
  text-align: center;
  user-select: none;
}

/* ==========================================================================
   Right Sidebar (Palettes & Guide Overlay)
   ========================================================================== */
#app-sidebar {
  grid-area: sidebar;
  background-color: var(--bg-surface);
  border-left: 1px solid var(--border-subtle);
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  z-index: 15;
}

.sidebar-tab-header {
  display: flex;
  border-bottom: 1px solid var(--border-subtle);
  background: var(--bg-surface);
  position: sticky;
  top: 0;
  z-index: 5;
}

.tab-btn {
  flex: 1;
  background: transparent;
  border: none;
  border-bottom: 2px solid transparent;
  padding: 12px 10px;
  color: var(--text-secondary);
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  text-align: center;
  transition: all 0.15s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
}

.tab-btn:hover {
  color: var(--text-primary);
}

.tab-btn.active {
  color: var(--text-accent);
  border-bottom-color: var(--accent-primary);
  background: rgba(59, 116, 230, 0.05);
}

.sidebar-content {
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.sidebar-panel {
  display: none;
  flex-direction: column;
  gap: 16px;
}

.sidebar-panel.active {
  display: flex;
}

/* Panel Sections */
.panel-section {
  display: flex;
  flex-direction: column;
  gap: 8px;
  background: var(--bg-surface-elevated);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  padding: 12px;
}

.panel-section-title {
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-muted);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

/* Active Color Display */
.active-color-row {
  display: flex;
  align-items: center;
  gap: 12px;
}

.color-preview-box {
  width: 48px;
  height: 48px;
  border-radius: var(--radius-sm);
  border: 2px solid var(--border-strong);
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.3);
  position: relative;
  cursor: pointer;
  flex-shrink: 0;
}

.color-input-hidden {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
  cursor: pointer;
}

.color-details {
  display: flex;
  flex-direction: column;
  gap: 4px;
  flex: 1;
}

.color-hex-input {
  background: var(--bg-input);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  font-family: var(--font-mono);
  font-size: 13px;
  font-weight: 600;
  padding: 6px 8px;
  text-transform: uppercase;
  width: 100%;
}

.color-hex-input:focus {
  outline: none;
  border-color: var(--border-focus);
}

/* Palette Select & Actions */
.palette-select-wrapper {
  display: flex;
  gap: 6px;
}

.select-input {
  flex: 1;
  background: var(--bg-input);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  padding: 6px 8px;
  font-size: 12px;
  cursor: pointer;
}

.select-input:focus {
  outline: none;
  border-color: var(--border-focus);
}

/* Swatches Grid */
.palette-swatches-grid {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  gap: 4px;
  max-height: 220px;
  overflow-y: auto;
  padding: 2px;
}

.color-swatch {
  aspect-ratio: 1;
  border-radius: 4px;
  border: 1px solid rgba(255, 255, 255, 0.1);
  cursor: pointer;
  position: relative;
  transition: transform 0.1s ease, border-color 0.1s ease;
}

.color-swatch:hover {
  transform: scale(1.15);
  z-index: 2;
  border-color: #ffffff;
}

.color-swatch.active {
  outline: 2px solid #ffffff;
  outline-offset: 1px;
  z-index: 3;
}

/* Recent Swatches */
.recent-swatches-grid {
  display: flex;
  gap: 4px;
  overflow-x: auto;
  padding-bottom: 2px;
}

.recent-swatches-grid .color-swatch {
  width: 20px;
  height: 20px;
  flex-shrink: 0;
}

/* Form Controls in Guide Overlay */
.control-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

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

.range-input {
  flex: 1;
  accent-color: var(--accent-primary);
  cursor: pointer;
}

.number-input {
  background: var(--bg-input);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  font-family: var(--font-mono);
  font-size: 12px;
  padding: 4px 6px;
  width: 60px;
  text-align: right;
}

.number-input:focus {
  outline: none;
  border-color: var(--border-focus);
}

.guide-preview-card {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--bg-input);
  border-radius: var(--radius-sm);
  padding: 8px;
  border: 1px solid var(--border-subtle);
}

.guide-thumb {
  width: 48px;
  height: 48px;
  object-fit: contain;
  background: var(--bg-surface);
  border-radius: 4px;
  border: 1px solid var(--border-subtle);
}

.guide-info {
  flex: 1;
  overflow: hidden;
}

.guide-name {
  font-size: 12px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.guide-meta {
  font-size: 10px;
  color: var(--text-muted);
  font-family: var(--font-mono);
}

/* ==========================================================================
   Footer Status Bar
   ========================================================================== */
#app-footer {
  grid-area: footer;
  background-color: var(--bg-surface);
  border-top: 1px solid var(--border-subtle);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  font-size: 11px;
  color: var(--text-muted);
  font-family: var(--font-mono);
  user-select: none;
  z-index: 20;
}

.footer-item {
  display: flex;
  align-items: center;
  gap: 6px;
}

.status-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background-color: var(--success-primary);
}

/* ==========================================================================
   Modal Dialogs
   ========================================================================== */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: rgba(0, 0, 0, 0.7);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
}

.modal-overlay.open {
  opacity: 1;
  pointer-events: auto;
}

.modal-card {
  background: var(--bg-surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  width: 90%;
  max-width: 440px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  transform: translateY(10px);
  transition: transform 0.2s ease;
}

.modal-overlay.open .modal-card {
  transform: translateY(0);
}

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

.modal-title {
  font-size: 15px;
  font-weight: 700;
  color: var(--text-primary);
}

.modal-close-btn {
  background: transparent;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 18px;
  padding: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius-sm);
}

.modal-close-btn:hover {
  color: var(--text-primary);
  background: var(--bg-surface-hover);
}

.modal-body {
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.modal-footer {
  padding: 12px 20px;
  border-top: 1px solid var(--border-subtle);
  background: var(--bg-input);
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 8px;
}

.modal-input-group {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.modal-input-group label {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-secondary);
}

.modal-text-input {
  background: var(--bg-input);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  padding: 8px 10px;
  font-size: 13px;
}

.modal-text-input:focus {
  outline: none;
  border-color: var(--border-focus);
}

.presets-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
}

.preset-btn {
  background: var(--bg-surface-elevated);
  border: 1px solid var(--border-subtle);
  color: var(--text-secondary);
  padding: 6px;
  border-radius: var(--radius-sm);
  font-family: var(--font-mono);
  font-size: 11px;
  cursor: pointer;
  text-align: center;
}

.preset-btn:hover {
  border-color: var(--border-strong);
  color: var(--text-primary);
  background: var(--bg-surface-hover);
}

/* ==========================================================================
   Mobile & Tablet Responsive Adjustments
   ========================================================================== */
@media (max-width: 900px) {
  #app-container {
    grid-template-columns: var(--toolbar-width) 1fr;
    grid-template-areas:
      "header header"
      "toolbar canvas"
      "footer footer";
  }

  #app-sidebar {
    position: fixed;
    top: var(--header-height);
    right: 0;
    width: 320px;
    height: calc(100vh - var(--header-height) - var(--footer-height));
    height: calc(100dvh - var(--header-height) - var(--footer-height));
    box-shadow: var(--shadow-lg);
    transform: translateX(100%);
    transition: transform 0.25s cubic-bezier(0.4, 0, 0.2, 1);
  }

  #app-sidebar.mobile-open {
    transform: translateX(0);
  }

  .mobile-sidebar-toggle {
    display: inline-flex !important;
  }
}

@media (min-width: 901px) {
  .mobile-sidebar-toggle {
    display: none !important;
  }
}
