/* ===========================================
   Custom Properties
   =========================================== */

:root {
  color-scheme: light dark;
  --sidebar-width: 452px;
  --toolbar-height: 48px;

  --bg: #f1f5f9;
  --surface: #ffffff;
  --surface-raised: #f8fafc;
  --surface-toggle: #e9eff8;
  --border: #e2e8f0;
  --border-strong: #cbd5e1;
  --text: #0f172a;
  --text-secondary: #475569;
  --text-muted: #94a3b8;
  --primary: #4f46e5;
  --primary-hover: #4338ca;
  --primary-text: #ffffff;
  --focus-ring: rgba(79, 70, 229, 0.16);
  --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.08);
  --shadow-lg: 0 8px 30px rgba(0, 0, 0, 0.12);
  --overlay-bg: rgba(15, 23, 42, 0.4);
}

/* ===========================================
   Explicit Theme Mode
   =========================================== */

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0f172a;
  --surface: #1e293b;
  --surface-raised: #334155;
  --surface-toggle: #3f5067;
  --border: #334155;
  --border-strong: #475569;
  --text: #f1f5f9;
  --text-secondary: #94a3b8;
  --text-muted: #64748b;
  --primary: #818cf8;
  --primary-hover: #6366f1;
  --primary-text: #0f172a;
  --focus-ring: rgba(129, 140, 248, 0.2);
  --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.3);
  --shadow-lg: 0 8px 30px rgba(0, 0, 0, 0.4);
  --overlay-bg: rgba(0, 0, 0, 0.55);
}

:root[data-theme="dark"] .toolbar-icon {
  filter: invert(1);
}

:root[data-theme="light"] {
  color-scheme: light;
}

/* ===========================================
   Reset & Base
   =========================================== */

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  margin: 0;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 13.5px;
  line-height: 1.4;
  color: var(--text);
  background: var(--bg);
  display: flex;
  flex-direction: column;
  height: 100dvh;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

/* ===========================================
   Toolbar
   =========================================== */

.toolbar {
  height: var(--toolbar-height);
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0 0.75rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  z-index: 100;
}

.toolbar-brand {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex: 1;
}

.toolbar-icon {
  display: block;
}

.toolbar-title {
  font-weight: 700;
  font-size: 1rem;
  letter-spacing: 0.06em;
}

/* ===========================================
   Buttons
   =========================================== */

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border: none;
  border-radius: 8px;
  background: transparent;
  color: var(--text-secondary);
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
  flex-shrink: 0;
}

.icon-btn:hover {
  background: var(--surface-raised);
  color: var(--text);
}

.icon-btn:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 1px;
}

.btn-export {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.4rem 0.75rem;
  background: var(--primary);
  color: var(--primary-text);
  text-decoration: none;
  font-weight: 600;
  font-size: 0.85rem;
  border-radius: 8px;
  transition: background 0.15s;
  white-space: nowrap;
  flex-shrink: 0;
}

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

.btn-export:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
}

/* Hide hamburger on desktop */
#sidebar-toggle {
  display: none;
}

/* ===========================================
   Overlay (mobile)
   =========================================== */

#sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: var(--overlay-bg);
  z-index: 998;
}

#sidebar-overlay.visible {
  display: block;
}

/* ===========================================
   Container Layout
   =========================================== */

#container {
  flex: 1;
  display: flex;
  min-height: 0;
  overflow: hidden;
}

/* ===========================================
   Sidebar
   =========================================== */

#controls-section {
  width: var(--sidebar-width);
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  font-size: 12px;
  background: var(--surface);
  border-right: 1px solid var(--border);
  overflow: hidden;
}

.sidebar-header {
  display: none;
}

.sidebar-title {
  font-weight: 700;
  font-size: 1rem;
}

.controls-scroll {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 0.3rem;
}

.controls-scroll::-webkit-scrollbar {
  width: 6px;
}

.controls-scroll::-webkit-scrollbar-track {
  background: transparent;
}

.controls-scroll::-webkit-scrollbar-thumb {
  background: var(--border-strong);
  border-radius: 3px;
}

.controls-scroll::-webkit-scrollbar-thumb:hover {
  background: var(--text-muted);
}

.sidebar-footer {
  flex-shrink: 0;
  padding: 0.45rem 0.65rem;
  border-top: 1px solid var(--border);
  font-size: 0.7rem;
  color: var(--text-muted);
  text-align: center;
}

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

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

/* ===========================================
   Panels (Details / Summary)
   =========================================== */

.panel {
  margin-bottom: 0.2rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  background: var(--surface);
}

.panel:last-child {
  margin-bottom: 0;
}

.panel-toggle {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.3rem 0.5rem;
  font-weight: 600;
  font-size: 0.78rem;
  color: var(--text);
  cursor: pointer;
  user-select: none;
  list-style: none;
  background: var(--surface-toggle);
  transition: background 0.15s;
}

.panel-toggle::-webkit-details-marker {
  display: none;
}

.panel-toggle:hover {
  background: var(--border);
}

.panel-toggle:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: -2px;
}

.panel-toggle .chevron {
  transition: transform 0.2s ease;
  flex-shrink: 0;
  color: var(--text-muted);
}

.panel[open]>.panel-toggle .chevron {
  transform: rotate(180deg);
}

.panel-body {
  padding: 0.35rem;
  border-top: 1px solid var(--border);
}

/* ===========================================
   Field Layout
   =========================================== */

.field-row {
  display: flex;
  gap: 0.5rem;
  margin-bottom: 0.34rem;
}

.field-row:last-child {
  margin-bottom: 0;
}

.field {
  display: grid;
  grid-template-columns: 82px minmax(0, 1fr);
  column-gap: 0.45rem;
  align-items: center;
  flex: 1;
  min-width: 0;
}

.field-label {
  font-size: 0.63rem;
  font-weight: 600;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.02em;
  display: flex;
  align-items: center;
  gap: 0.25rem;
  margin: 0;
  white-space: nowrap;
}

.field-grow {
  flex: 1.5;
}

.field-color {
  flex: 1;
  grid-template-columns: 82px minmax(0, 1fr);
  min-width: 0;
}

.field-row .field-color+.field-grow {
  flex: 1;
}

.field-check {
  display: grid;
  grid-template-columns: 86px auto minmax(0, 1fr);
  column-gap: 0.25rem;
  align-items: center;
  cursor: pointer;
  font-size: 0.63rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.02em;
  color: var(--text-secondary);
}

.field-check input[type="checkbox"] {
  grid-column: 2;
}

.field-check span {
  grid-column: 3;
}

/* ===========================================
   Form Controls
   =========================================== */

input,
select {
  font: inherit;
  font-size: 0.9rem;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: 5px;
  padding: 0.26rem 0.4rem;
  min-height: 30px;
  outline: none;
  transition: border-color 0.15s, box-shadow 0.15s;
  min-width: 0;
  width: 100%;
}

input:not([type="checkbox"]):focus,
select:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 2px var(--focus-ring);
}

input:disabled,
select:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

select {
  cursor: pointer;
}

input[type="checkbox"] {
  width: 14px;
  height: 14px;
  margin: 0;
  padding: 0;
  min-height: 14px;
  border-radius: 2px;
  accent-color: var(--primary);
  cursor: pointer;
  flex-shrink: 0;
}

input[type="checkbox"]:focus,
input[type="checkbox"]:focus-visible {
  outline: none;
  box-shadow: 0 0 0 2px var(--focus-ring);
}

input[type="color"] {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 30px;
  padding: 2px;
  border-radius: 5px;
  cursor: pointer;
}

input[type="color"]::-webkit-color-swatch-wrapper {
  padding: 0;
}

input[type="color"]::-webkit-color-swatch,
input[type="color"]::-moz-color-swatch {
  border: none;
  border-radius: 3px;
}

input[type="range"] {
  width: 100%;
  height: 5px;
  padding: 0;
  border: none;
  border-radius: 3px;
  background: var(--border-strong);
  accent-color: var(--primary);
  cursor: pointer;
  margin-top: 0;
}

input[type="range"]:focus {
  box-shadow: none;
}

.range-value {
  font-weight: 400;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}

/* ===========================================
   Sub-sections (nested groups)
   =========================================== */

.sub-section {
  border: 1px solid var(--border);
  border-radius: 7px;
  padding: 0.28rem;
  margin-top: 0.22rem;
  margin-bottom: 0.25rem;
  background: var(--surface-raised);
}

.sub-section:last-child {
  margin-bottom: 0;
}

/* Reset fieldset chrome when used as sub-section */
fieldset.sub-section {
  margin-left: 0;
  margin-right: 0;
}

fieldset.sub-section:disabled {
  opacity: 0.5;
}

fieldset.sub-section:disabled input,
fieldset.sub-section:disabled select {
  opacity: 1;
}

.sub-section-title {
  font-size: 0.62rem;
  font-weight: 600;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.03em;
  margin-bottom: 0.24rem;
}

/* ===========================================
   Canvas Area
   =========================================== */

#map-section {
  flex: 1;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 1.25rem;
  overflow: auto;
  min-height: 0;
  background-color: var(--bg);
  background-image: radial-gradient(circle, var(--border) 1px, transparent 1px);
  background-size: 24px 24px;
}

.canvas-wrap {
  background: #ffffff;
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 0.75rem;
  box-shadow: var(--shadow-md);
  max-width: 100%;
  overflow: auto;
}

#codon-canvas {
  display: block;
  max-width: 100%;
  height: auto;
}

/* ===========================================
   Mobile Responsive
   =========================================== */

@media (max-width: 960px) {
  #sidebar-toggle {
    display: inline-flex;
  }

  #controls-section {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: min(var(--sidebar-width), 92vw);
    z-index: 1000;
    transform: translateX(-100%);
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    border-right: none;
  }

  #controls-section.open {
    transform: translateX(0);
    box-shadow: var(--shadow-lg);
  }

  .sidebar-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.6rem 0.65rem;
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
  }

  #map-section {
    padding: 0.75rem;
    justify-content: flex-start;
  }

  .btn-export-label {
    display: none;
  }

  .btn-export {
    padding: 0.4rem;
  }
}

@media (max-width: 480px) {
  #map-section {
    padding: 0.5rem;
  }

  .canvas-wrap {
    padding: 0.4rem;
    border-radius: 8px;
  }
}

/* ===========================================
   Reduced Motion
   =========================================== */

@media (prefers-reduced-motion: reduce) {

  *,
  *::before,
  *::after {
    transition-duration: 0.01ms !important;
  }
}
