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

html, body {
  height: 100%;
}

/* ─── Layout variables (never change with theme) ──────────────── */
:root {
  --sheet-peek:       36px;
  --toolbar-h:        52px;
  --sidebar-w:        320px;
  --minimap-w:        280px;
  --minimap-h:        200px;
  --minimap-offset:   16px;
  --sheet-h:          70vh;
  --minimap-w-mobile: 160px;
  --minimap-h-mobile: 120px;
  --map-tiles-filter: brightness(0.6) invert(1) contrast(3) hue-rotate(200deg) saturate(0.3) brightness(0.7);
}

/* ─── Dark theme (default) ────────────────────────────────────── */
:root {
  --bg-base:          #1C2218;
  --bg-panel:         #1C2218;
  --bg-hover:         #2A3A20;
  --bg-input:         #242C1E;
  --bg-btn-primary:   #4A6A28;
  --bg-btn-primary-hover: #3A5A1A;
  --bg-btn-secondary: #1C2218;
  --bg-btn-secondary-hover: #2A3A20;

  --text-primary:     #D8CFBA;
  --text-secondary:   #A8A090;
  --text-muted:       #5A5A4A;

  --border:           rgba(58, 74, 42, 0.31);
  --border-strong:    rgba(58, 74, 42, 0.5);
  --border-btn-secondary: #7A9E4A;
  --border-btn-secondary-hover: #5A8A32;

  --accent:           #7A9E4A;
  --accent-hover:     #9ABE6A;
  --shadow:           rgba(10, 15, 8, 0.44);

  --toast-bg:           rgba(28, 34, 24, 0.85);
  --toast-spinner-track: rgba(122, 158, 74, 0.3);

  --legend-bg: rgba(28, 34, 24, 0.85);

  --error:        #E05555;
  --error-border: rgba(224, 85, 85, 0.5);
}

/* ─── Light theme ─────────────────────────────────────────────── */
@media (prefers-color-scheme: light) {
  :root {
    --bg-base:          #F5F2EC;
    --bg-panel:         #EDEAE3;
    --bg-hover:         #E2DDD4;
    --bg-input:         #F0EDE6;
    --bg-btn-primary:   #4A6A28;
    --bg-btn-primary-hover: #3A5A1A;
    --bg-btn-secondary: #EDEAE3;
    --bg-btn-secondary-hover: #E2DDD4;

    --text-primary:     #2C2C1E;
    --text-secondary:   #5A5A4A;
    --text-muted:       #9A9A8A;

    --border:           rgba(90, 90, 74, 0.2);
    --border-strong:    rgba(90, 90, 74, 0.35);
    --border-btn-secondary: #7A9E4A;
    --border-btn-secondary-hover: #5A8A32;

    --accent:           #4A6A28;
    --accent-hover:     #3A5A1A;
    --shadow:           rgba(44, 44, 30, 0.15);

    --toast-bg:           rgba(237, 234, 227, 0.85);
    --toast-spinner-track: rgba(74, 106, 40, 0.3);

    --legend-bg: rgba(245, 242, 236, 0.85);

    --error:        #C03030;
    --error-border: rgba(192, 48, 48, 0.35);
  }
}

/* ─── Map tile dark mode ──────────────────────────────────────── */
@media (prefers-color-scheme: dark) {
  .map-tiles {
    filter: var(--map-tiles-filter);
  }
}

/* ─── App shell ───────────────────────────────────────────────── */
#app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  background: var(--bg-base);
  color: var(--text-primary);
}

/* ─── Toolbar ─────────────────────────────────────────────────── */
#toolbar {
  width: 100%;
  height: var(--toolbar-h);
  background: var(--bg-panel);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  padding: 0 16px;
  gap: 16px;
  flex-shrink: 0;
  z-index: 10;
}

#toolbar .action-btn {
  width: auto;
  padding: 8px 20px;
  font-size: 1rem;
}

#main {
  display: flex;
  flex: 1;
  min-height: 0;
}

/* ─── Sidebar ─────────────────────────────────────────────────── */
#sidebar {
  width: var(--sidebar-w);
  flex-shrink: 0;
  background: var(--bg-panel);
  color: var(--text-primary);
  padding: 24px 16px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 28px;
  transition: width 0.25s ease, padding 0.25s ease;
  border-right: 1px solid var(--border);
}

#app.sidebar-collapsed #sidebar {
  width: 0;
  padding: 0;
  overflow: hidden;
}

#sidebar-bar {
  height: var(--toolbar-h);
  display: flex;
  align-items: center;
  padding: 0 16px;
  gap: 10px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

#sidebar-bar-label {
  font-size: 0.75rem;
  font-weight: 600;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--text-secondary);
  flex: 1;
}

#sidebar-toggle {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 36px;
  background: var(--bg-hover);
  border: 1px solid var(--border-strong);
  border-radius: 10px;
  color: var(--text-secondary);
  cursor: pointer;
  transition: color 0.15s, background 0.15s;
  flex-shrink: 0;
}

#sidebar-toggle:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}

#sidebar-content {
  display: contents;
}

.option-group {
  /* sidebar handles spacing via gap */
}



/* ─── Save dialog ─────────────────────────────────────────────── */
#save-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  backdrop-filter: blur(2px);
  z-index: 200;
  align-items: center;
  justify-content: center;
}

#save-overlay.visible {
  display: flex;
}

#save-dialog {
  background: var(--bg-panel);
  border: 1px solid var(--border-strong);
  border-radius: 14px;
  padding: 28px 24px 20px;
  width: 340px;
  box-shadow: 0 8px 40px var(--shadow);
  display: flex;
  flex-direction: column;
  gap: 16px;
}

#save-dialog h2 {
  font-size: 0.7rem;
  font-weight: 600;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--text-secondary);
}

#save-dialog input[type="text"] {
  width: 100%;
}

#save-dialog-actions {
  display: flex;
  gap: 10px;
  justify-content: flex-end;
}

#save-dialog-actions .action-btn {
  width: auto;
  padding: 8px 20px;
  font-size: 0.9rem;
}



/* ─── Loading toasts ──────────────────────────────────────────── */
#loading-toasts {
  position: absolute;
  top: 16px;
  left: 16px;
  z-index: 50;
  display: flex;
  flex-direction: column;
  gap: 8px;
  pointer-events: none;
}

.loading-toast {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 14px;
  background: var(--toast-bg);
  border: 1px solid var(--border-strong);
  border-radius: 10px;
  backdrop-filter: blur(6px);
  opacity: 0;
  transform: translateY(-6px);
  transition: opacity 0.2s ease, transform 0.2s ease;
}

.loading-toast--visible {
  opacity: 1;
  transform: translateY(0);
}

.loading-toast--hiding {
  opacity: 0;
  transform: translateY(-6px);
}

.loading-toast-spinner {
  width: 14px;
  height: 14px;
  border: 2px solid var(--toast-spinner-track);
  border-top-color: var(--accent);
  border-radius: 50%;
  flex-shrink: 0;
  animation: toast-spin 0.8s linear infinite;
}

@keyframes toast-spin {
  to { transform: rotate(360deg); }
}

.loading-toast-label {
  font-size: 1.0rem;
  font-weight: 500;
  color: var(--text-primary);
  white-space: nowrap;
}


.loading-toast--error {
  border-color: var(--error-border);
}



/* ─── Color legend ──────────────────────────────────────────── */

#color-legend {
  position: absolute;
  bottom: 16px;
  left: 16px;
  z-index: 10;
  padding: 8px 12px;
  border-radius: 8px;
  background: var(--legend-bg);
  border: 1px solid var(--border);
  backdrop-filter: blur(6px);
  display: flex;
  flex-direction: column;
  gap: 6px;
  pointer-events: none;
  display: none;
}

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

.legend-swatch {
  width: 12px;
  height: 12px;
  border-radius: 2px;
  flex-shrink: 0;
}

.legend-label {
  font-size: 1.0rem;
  color: var(--text-secondary);
  white-space: nowrap;
}


#map-container.preview-active #color-legend {
  display: flex;
}

#map-container.preview-active.map-focused #color-legend {
  display: none;
}


/* ─── Mobile handle ───────────────────────────────────────────── */
#sheet-handle {
  display: none;
}

#sheet-handle-bar {
  width: 40px;
  height: 4px;
  background: var(--border-strong);
  border-radius: 2px;
  margin: 0 auto;
}

/* ─── Option group labels ─────────────────────────────────────── */
.option-group h2 {
  font-size: 0.7rem;
  font-weight: 600;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--text-secondary);
  margin-bottom: 12px;
}

.option-group > label {
  font-size: 0.7rem;
  font-weight: 600;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--text-secondary);
  display: block;
  margin-bottom: 12px;
}

/* ─── Radio options ───────────────────────────────────────────── */
.radio-option {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-radius: 10px;
  cursor: pointer;
  margin-bottom: 6px;
  border: 1px solid transparent;
  transition: background 0.15s, border-color 0.15s;
}

.radio-option:hover {
  background: var(--bg-hover);
}

.radio-option input[type="radio"] {
  accent-color: var(--accent);
  width: 15px;
  height: 15px;
  flex-shrink: 0;
}

.radio-option input[type="radio"]:checked ~ .option-label {
  color: var(--text-primary);
}

.option-label {
  font-size: 0.9rem;
  color: var(--text-secondary);
}

/* ─── Scale slider ────────────────────────────────────────────── */
.scale-display {
  font-size: 1rem;
  color: var(--text-primary);
  margin-bottom: 10px;
}

input[type="range"] {
  width: 100%;
  accent-color: var(--accent);
  cursor: pointer;
}

.scale-range-labels {
  display: flex;
  justify-content: space-between;
  font-size: 0.7rem;
  color: var(--text-muted);
  margin-top: 6px;
}

/* ─── Checkboxes ──────────────────────────────────────────────── */
.checkbox-option {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  border-radius: 10px;
  cursor: pointer;
  margin-bottom: 4px;
  transition: background 0.15s;
}

.checkbox-option:hover {
  background: var(--bg-hover);
}

.checkbox-option input[type="checkbox"] {
  accent-color: var(--accent);
  width: 15px;
  height: 15px;
  flex-shrink: 0;
}

.checkbox-option input[type="checkbox"]:checked ~ .option-label {
  color: var(--text-primary);
}

.checkbox-option.disabled {
  opacity: 0.38;
  cursor: not-allowed;
}

.checkbox-option.disabled:hover {
  background: none;
}

/* ─── Misc ────────────────────────────────────────────────────── */
.wip-note {
  font-size: 0.72rem;
  color: var(--text-muted);
  font-style: italic;
  padding: 2px 12px 0;
  line-height: 1.4;
}

.color-swatch {
  width: 13px;
  height: 13px;
  border-radius: 50%;
  flex-shrink: 0;
  display: inline-block;
  box-sizing: border-box;
}

#color-options {
  margin-top: 4px;
  padding-left: 8px;
  border-left: 2px solid var(--border);
}

.hidden {
  display: none;
}

/* ─── Buttons ─────────────────────────────────────────────────── */
.action-btn {
  width: 100%;
  padding: 10px 16px;
  background: var(--bg-btn-secondary);
  color: var(--text-primary);
  border: 1px solid var(--border-btn-secondary);
  border-radius: 10px;
  font-size: 0.9rem;
  cursor: pointer;
  text-align: left;
  transition: background 0.15s, border-color 0.15s, color 0.15s;
}

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

.action-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
  pointer-events: none;
}

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

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

/* ─── Text inputs ─────────────────────────────────────────────── */
input[type="text"],
input[type="number"] {
  background: var(--bg-input);
  border: 1px solid var(--border-strong);
  color: var(--text-primary);
  border-radius: 10px;
  padding: 6px 10px;
  font-size: 0.9rem;
  transition: border-color 0.15s;
}

input[type="text"]:focus,
input[type="number"]:focus {
  outline: none;
  border-color: var(--accent);
}

/* ─── Map container ───────────────────────────────────────────── */
#map-container {
  position: relative;
  flex: 1;
  min-height: 0;
  overflow: hidden;
}

#map {
  position: absolute;
  inset: 0;
  transition: all 0.3s ease;
  z-index: 1;
}

#terrain-canvas {
  position: absolute;
  display: none;
  transition: all 0.3s ease;
  z-index: 2;
  cursor: pointer;
  border-radius: 10px;
  box-shadow: 0 4px 24px var(--shadow);
}

/* 3D is big, map is minimap */
#map-container.preview-active #map {
  inset: auto;
  bottom: var(--minimap-offset);
  right: var(--minimap-offset);
  width: var(--minimap-w);
  height: var(--minimap-h);
  border-radius: 10px;
  box-shadow: 0 4px 24px var(--shadow);
  cursor: pointer;
  z-index: 3;
}

#map-container.preview-active #terrain-canvas {
  display: block;
  inset: 0;
  width: 100%;
  height: 100%;
  border-radius: 0;
  box-shadow: none;
  cursor: default;
}

/* Map is big, 3D is minimap */
#map-container.preview-active.map-focused #map {
  inset: 0;
  width: 100%;
  height: 100%;
  border-radius: 0;
  box-shadow: none;
  cursor: default;
  z-index: 1;
}

#map-container.preview-active.map-focused #terrain-canvas {
  display: block;
  inset: auto;
  bottom: var(--minimap-offset);
  right: var(--minimap-offset);
  width: var(--minimap-w);
  height: var(--minimap-h);
  cursor: pointer;
  z-index: 3;
  border-radius: 10px;
}

.textbox-label {
  display: flex;
  align-items: center;
  justify-content: center;
  transform: translate(-50%, -50%);
  color: black;
  font-size: 20px;
  font-family: serif;
  pointer-events: none;
  white-space: nowrap;
}

/* ─── Mobile: bottom sheet ────────────────────────────────────── */
@media (max-width: 1024px) {

  #app {
    display: block !important;
    position: relative;
    height: 100dvh;
    overflow: hidden;
  }

  #toolbar .action-btn {
    padding: 6px 12px;
    font-size: 0.85rem;
  }

  #map-container.preview-active #map,
  #map-container.preview-active.map-focused #terrain-canvas {
    width: var(--minimap-w-mobile);
    height: var(--minimap-h-mobile);
  }

  #map-container {
    position: absolute !important;
    inset: var(--toolbar-h) 0 var(--sheet-peek) 0 !important;
    z-index: 0;
  }

  #sidebar {
    position: fixed !important;
    bottom: 0 !important;
    left: 0 !important;
    right: 0 !important;
    top: auto !important;
    width: 100% !important;
    max-height: none !important;
    height: var(--sheet-h);
    padding: 0 0 32px;
    border-radius: 14px 14px 0 0;
    border-top: 1px solid var(--border);
    border-right: none;
    overflow-y: auto;
    transform: translateY(calc(100% - var(--sheet-peek)));
    transition: transform 0.3s cubic-bezier(0.32, 0.72, 0, 1) !important;
    box-shadow: 0 -4px 32px var(--shadow);
    z-index: 100;
  }

  #sidebar-toggle {
    display: none !important;
  }

  #app.sheet-open #sidebar {
    transform: translateY(0);
  }

  #sheet-handle {
    display: flex !important;
    align-items: center;
    justify-content: center;
    height: var(--sheet-peek);
    flex-shrink: 0;
    cursor: pointer;
    position: sticky;
    top: 0;
    background: var(--bg-panel);
    z-index: 1;
    border-radius: 14px 14px 0 0;
  }

  #sheet-handle-label {
    font-size: 0.75rem;
    font-weight: 600;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--text-secondary);
  }

  #sheet-handle-inner {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 10px;
  }

  #sidebar-bar {
    display: none !important;
  }

  #sidebar-content {
    flex-direction: column;
    padding: 8px 0;
    border-bottom: none;
  }

  .option-group {
    border-right: none !important;
    border-bottom: 1px solid var(--border);
    padding: 12px 16px;
    flex: none;
    width: 100%;
  }

  .option-group:last-child {
    border-bottom: none;
  }

  #toolbar {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 101;
  }

  #main {
    height: 100%;
  }

}
