:root {
  --bg: #05091e;
  --surface: #080f27;
  --surface-2: #0d1732;
  --ink: #f4f0df;
  --muted: #929bb3;
  --line: rgba(128, 160, 203, .16);
  --cyan: #24bbdf;
  --green: #4acb91;
  --orange: #f6a13a;
  --magenta: #d281db;
  --country-color: #4acb91;
  --serif: "DM Serif Display", Georgia, serif;
  --sans: Inter, system-ui, sans-serif;
  --mono: "DM Mono", monospace;
  color-scheme: dark;
}

html, body { min-width: 320px; min-height: 100%; margin: 0; }
body {
  color: var(--ink);
  font-family: var(--sans);
  background:
    linear-gradient(rgba(124, 151, 197, .055) 1px, transparent 1px),
    linear-gradient(90deg, rgba(124, 151, 197, .055) 1px, transparent 1px),
    radial-gradient(circle at 72% 38%, rgba(27, 93, 138, .16), transparent 36%),
    var(--bg);
  background-size: 72px 72px, 72px 72px, auto, auto;
}
button:focus-visible, input:focus-visible { outline: 2px solid var(--magenta); outline-offset: 2px; }

.fourier-app { min-height: 100vh; display: flex; flex-direction: column; }
.topbar {
  min-height: 72px;
  padding: 0 clamp(20px, 3vw, 48px);
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 30px;
  border-bottom: 1px solid var(--line);
  background: rgba(5, 9, 30, .8);
  backdrop-filter: blur(18px);
  position: relative;
  z-index: 10;
}
.brand { display: flex; align-items: center; gap: 9px; color: var(--ink); text-decoration: none; min-width: 0; }
.brand img { flex: none; filter: drop-shadow(0 0 12px rgba(46,189,220,.3)); }
.brand b { font: 400 22px/1 var(--serif); white-space: nowrap; }
.tag { padding: 3px 8px; border: 1px solid var(--line); border-radius: 999px; color: var(--cyan); font: 500 9px/1 var(--mono); letter-spacing: .12em; }
.header-copy { text-align: center; display: flex; flex-direction: column; gap: 5px; }
.header-copy span { color: var(--cyan); font: 500 9px/1 var(--mono); letter-spacing: .15em; }
.header-copy strong { color: #bec4d5; font: 400 12px/1.3 var(--sans); }
.language-btn { justify-self: end; height: 38px; padding: 0 14px; border: 1px solid var(--line); border-radius: 999px; background: rgba(255,255,255,.035); color: #bbc2d4; font: 600 10px/1 var(--mono); letter-spacing: .07em; }
.language-btn:hover { border-color: rgba(36,187,223,.45); color: var(--ink); }

.workspace {
  width: min(1740px, 100%);
  height: calc(100vh - 72px);
  min-height: 650px;
  margin: 0 auto;
  display: grid;
  grid-template-columns: minmax(340px, .78fr) minmax(610px, 1.22fr);
  overflow: hidden;
}
.world-panel, .drawing-panel { min-width: 0; min-height: 0; display: flex; flex-direction: column; overflow: hidden; }
.world-panel { border-right: 1px solid var(--line); background: rgba(7, 13, 35, .38); }
.drawing-panel { background: rgba(5, 9, 30, .2); }
.panel-heading {
  min-height: 105px;
  padding: 22px clamp(20px, 2.4vw, 38px);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  border-bottom: 1px solid var(--line);
}
.panel-heading > div:first-child { min-width: 0; display: flex; align-items: flex-start; gap: 16px; }
.section-index { margin-top: 3px; color: #52617d; font: 500 10px/1 var(--mono); }
.eyebrow { margin: 0 0 8px; color: var(--cyan); font: 500 9px/1 var(--mono); letter-spacing: .14em; }
.panel-heading h1, .panel-heading h2 { margin: 0; font: 400 25px/1.1 var(--serif); letter-spacing: -.02em; }
.panel-heading h2 { display: flex; align-items: baseline; flex-wrap: wrap; gap: 10px; font-size: 30px; }
.panel-heading h2 small { color: var(--muted); font: 400 12px/1 var(--sans); }
.selected-code { padding: 7px 9px; border: 1px solid color-mix(in srgb, var(--country-color) 35%, transparent); border-radius: 3px; color: var(--country-color); background: color-mix(in srgb, var(--country-color) 9%, transparent); font: 500 10px/1 var(--mono); letter-spacing: .08em; }
.math-chip { border: 1px solid var(--line); background: rgba(255,255,255,.025); padding: 10px 14px; border-radius: 3px; color: #aeb8ca; font: 400 12px/1 var(--mono); white-space: nowrap; }
.math-chip sub, .math-chip sup { font-size: .65em; }

.globe-wrap { height: clamp(280px, 40vh, 440px); min-height: 280px; position: relative; overflow: hidden; border-bottom: 1px solid var(--line); background: radial-gradient(circle at 50% 46%, rgba(36,187,223,.1), rgba(4,8,24,.1) 42%, transparent 70%); }
#globe { display: block; width: 100%; height: 100%; touch-action: none; cursor: grab; }
#globe:active { cursor: grabbing; }
.sphere { fill: url("#ocean-gradient"); stroke: rgba(36,187,223,.22); stroke-width: 1; }
.graticule { fill: none; stroke: rgba(114,151,195,.13); stroke-width: .7; }
.country-shape { stroke-width: .65; cursor: pointer; transition: fill .2s, stroke .2s, filter .2s; }
.country-shape:hover { filter: brightness(1.35); }
.country-shape.selected { stroke-width: 1.25; }
.globe-grain { position: absolute; inset: 0; pointer-events: none; opacity: .3; background-image: radial-gradient(rgba(255,255,255,.3) .45px, transparent .55px); background-size: 4px 4px; mask-image: radial-gradient(circle, #000, transparent 74%); }
.globe-tip { position: absolute; bottom: 10px; left: 50%; transform: translateX(-50%); margin: 0; padding: 5px 9px; border-radius: 999px; color: #7785a0; background: rgba(5,9,30,.66); font: 400 9px/1 var(--mono); white-space: nowrap; pointer-events: none; }

.country-browser { min-height: 280px; padding: 22px clamp(20px, 2.4vw, 38px) 24px; display: flex; flex: 1; flex-direction: column; overflow: hidden; }
.search { height: 42px; display: flex; align-items: center; gap: 10px; padding: 0 12px; border: 1px solid var(--line); background: rgba(7,15,37,.8); border-radius: 3px; }
.search:focus-within { border-color: rgba(36,187,223,.55); box-shadow: 0 0 0 3px rgba(36,187,223,.07); }
.search svg { width: 17px; height: 17px; color: var(--muted); flex: none; }
.search input { width: 100%; min-width: 0; height: 100%; border: 0; outline: 0; background: transparent; color: var(--ink); font-size: 12px; }
.search input::placeholder { color: #68738d; }
.search kbd { padding: 2px 5px; border: 1px solid var(--line); border-radius: 3px; color: #68738d; font: 9px/1 var(--mono); }
.country-list-meta { margin: 18px 0 10px; display: flex; justify-content: space-between; color: #68758f; font: 500 9px/1 var(--mono); letter-spacing: .1em; }
.country-list { min-height: 0; flex: 1; overflow: auto; scrollbar-color: rgba(104,137,179,.35) transparent; scrollbar-width: thin; display: grid; align-content: start; grid-template-columns: 1fr 1fr; gap: 6px; }
.country-option { min-width: 0; height: 42px; padding: 0 11px; display: flex; align-items: center; gap: 8px; border: 1px solid transparent; border-radius: 3px; background: rgba(255,255,255,.018); color: #aab2c5; font: 400 11px/1.2 var(--sans); text-align: left; }
.country-option:hover { background: rgba(36,187,223,.07); color: var(--ink); }
.country-option[aria-selected="true"] { border-color: rgba(74,203,145,.28); background: rgba(74,203,145,.09); color: #e8fff3; }
.country-swatch { width: 7px; height: 7px; flex: none; border-radius: 50%; background: var(--country-color); box-shadow: 0 0 8px var(--country-color); }
.country-option span { min-width: 0; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.country-option small { flex: none; margin-left: auto; color: #586682; font: 500 8px/1 var(--mono); }
.country-option[aria-selected="true"] small { color: var(--green); }
.country-empty { grid-column: 1/-1; padding: 24px 0; text-align: center; color: var(--muted); font-size: 12px; }

.canvas-wrap {
  min-height: 0;
  flex: 1;
  position: relative;
  overflow: hidden;
  background:
    linear-gradient(rgba(124, 151, 197, .055) 1px, transparent 1px),
    linear-gradient(90deg, rgba(124, 151, 197, .055) 1px, transparent 1px),
    radial-gradient(circle at 50% 48%, rgba(29,95,134,.12), transparent 42%);
  background-size: 40px 40px, 40px 40px, auto;
  border-bottom: 1px solid var(--line);
}
#drawing { width: 100%; height: 100%; display: block; }
.target-path { fill: none; stroke: rgba(148,163,190,.2); stroke-width: 1.4; stroke-dasharray: 4 7; transition: opacity .2s; }
.trace-path { fill: none; stroke: url("#trace-gradient"); stroke-width: 2.4; stroke-linecap: round; stroke-linejoin: round; filter: url("#trace-glow"); }
.epicycle-circle { fill: none; stroke: rgba(36,187,223,.24); stroke-width: .8; }
.epicycle-vector { stroke: rgba(114,229,241,.55); stroke-width: .85; }
.epicycle-dot { fill: #4acb91; }
.endpoint-halo { fill: color-mix(in srgb, var(--country-color) 15%, transparent); stroke: color-mix(in srgb, var(--country-color) 50%, transparent); }
.endpoint-core { fill: var(--country-color); filter: drop-shadow(0 0 6px var(--country-color)); }
.drawing-grid line { stroke: rgba(124,151,197,.07); stroke-width: 1; }
.drawing-grid circle { fill: none; stroke: rgba(124,151,197,.07); stroke-width: 1; }
.canvas-stats { position: absolute; top: 16px; left: 18px; right: 18px; display: flex; align-items: center; justify-content: space-between; color: #78859e; font: 400 10px/1 var(--mono); pointer-events: none; }
.canvas-stats b { color: #bdc8d9; font-weight: 500; }
.play-hint { position: absolute; left: 50%; bottom: 18px; transform: translateX(-50%); padding: 8px 12px; display: flex; align-items: center; gap: 8px; border: 1px solid rgba(36,187,223,.14); border-radius: 999px; background: rgba(5,9,30,.8); color: #8290aa; font: 400 10px/1 var(--mono); white-space: nowrap; transition: opacity .3s, transform .3s; }
.play-hint.hidden { opacity: 0; transform: translate(-50%, 8px); pointer-events: none; }
.play-hint-icon { color: var(--cyan); font-size: 8px; }

.controls { padding: 18px clamp(20px, 2.5vw, 38px) 22px; background: rgba(7,13,35,.74); }
.play-control { display: flex; align-items: center; gap: 13px; }
.play-button { width: 54px; height: 54px; flex: none; display: grid; place-items: center; border: 0; border-radius: 50%; color: #04121a; background: linear-gradient(135deg, var(--cyan), var(--green)); box-shadow: 0 8px 26px rgba(36,187,223,.22); transition: transform .18s, box-shadow .18s; }
.play-button:hover { transform: translateY(-1px); box-shadow: 0 10px 32px rgba(36,187,223,.32); }
.play-button svg { grid-area: 1/1; width: 23px; height: 23px; }
.pause-icon, .play-button[aria-pressed="true"] .play-icon { display: none; }
.play-button[aria-pressed="true"] .pause-icon { display: block; }
.play-control > div { min-width: 0; flex: 1; display: flex; flex-direction: column; gap: 5px; }
.play-control strong { font-size: 13px; }
.play-control span { color: var(--muted); font-size: 10px; }
.reset-button { width: 38px; height: 38px; display: grid; place-items: center; border: 1px solid var(--line); border-radius: 50%; background: transparent; color: #8d98af; }
.reset-button:hover { color: var(--ink); background: rgba(255,255,255,.04); }
.reset-button svg { width: 18px; height: 18px; }
.control-grid { margin-top: 20px; display: grid; grid-template-columns: repeat(3, 1fr); gap: 20px; }
.range-control > span { display: flex; align-items: center; justify-content: space-between; gap: 8px; color: #8995ac; font-size: 10px; }
.range-control b { color: #b7c0d2; font-weight: 500; }
.range-control output { color: var(--cyan); font: 500 10px/1 var(--mono); }
.range-control input { width: 100%; height: 18px; margin: 8px 0 0; accent-color: var(--cyan); cursor: pointer; }
.render-options { margin-top: 15px; padding-top: 14px; display: flex; align-items: center; flex-wrap: wrap; gap: 18px; border-top: 1px solid var(--line); }
.render-options label { display: flex; align-items: center; gap: 6px; color: #8995aa; font-size: 10px; cursor: pointer; }
.render-options input { width: 14px; height: 14px; margin: 0; accent-color: var(--cyan); }
.random-button { margin-left: auto; height: 32px; padding: 0 11px; display: flex; align-items: center; gap: 7px; border: 1px solid var(--line); border-radius: 3px; background: rgba(255,255,255,.025); color: #a7b2c6; font-size: 10px; }
.random-button:hover { border-color: rgba(246,161,58,.35); color: var(--ink); }
.random-button svg { width: 15px; height: 15px; color: var(--orange); }

.loading { position: fixed; inset: 0; z-index: 30; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 18px; background: rgba(5,9,30,.94); color: #aeb8ca; font: 400 11px/1 var(--mono); letter-spacing: .06em; transition: opacity .3s; }
.loading.done { opacity: 0; pointer-events: none; }
.loading-ring { width: 44px; height: 44px; border: 1px solid rgba(36,187,223,.2); border-top-color: var(--cyan); border-radius: 50%; animation: spin .8s linear infinite; box-shadow: 0 0 22px rgba(36,187,223,.12); }
.error-message { position: fixed; z-index: 40; left: 50%; bottom: 30px; transform: translateX(-50%); max-width: min(520px, calc(100% - 32px)); padding: 12px 16px; border: 1px solid rgba(255,143,181,.35); background: #201027; color: #ffd6e6; border-radius: 4px; font-size: 12px; text-align: center; }
@keyframes spin { to { transform: rotate(360deg); } }

@media (max-width: 1080px) {
  .header-copy { display: none; }
  .topbar { grid-template-columns: 1fr auto; }
  .workspace { grid-template-columns: minmax(320px, .72fr) minmax(500px, 1.28fr); }
  .country-list { grid-template-columns: 1fr; }
  .control-grid { gap: 12px; }
  .math-chip { display: none; }
}

@media (max-width: 820px) {
  .topbar { min-height: 64px; padding: 0 18px; }
  .brand b { font-size: 19px; }
  .brand img { width: 29px; height: 29px; }
  .tag { display: none; }
  .workspace { height: auto; min-height: 0; display: flex; flex-direction: column; overflow: visible; }
  .world-panel { border-right: 0; border-bottom: 1px solid var(--line); }
  .panel-heading { min-height: 92px; padding: 18px 20px; }
  .panel-heading h1 { font-size: 22px; }
  .panel-heading h2 { font-size: 25px; }
  .globe-wrap { height: 320px; }
  .country-browser { min-height: 0; max-height: 310px; padding: 18px 20px; }
  .country-list { min-height: 100px; grid-template-columns: repeat(2, 1fr); }
  .world-panel, .drawing-panel { overflow: visible; }
  .canvas-wrap { min-height: 470px; }
  #drawing { min-height: 470px; }
}

@media (max-width: 520px) {
  .brand b { font-size: 17px; }
  .language-btn { height: 34px; padding: 0 11px; font-size: 9px; }
  .section-index { display: none; }
  .panel-heading > div:first-child { gap: 0; }
  .globe-wrap { height: 280px; min-height: 280px; }
  .country-list { grid-template-columns: 1fr; }
  .country-browser { max-height: 265px; }
  .drawing-heading { align-items: flex-start; }
  .panel-heading h2 { font-size: 26px; }
  .canvas-wrap, #drawing { min-height: 390px; }
  .control-grid { grid-template-columns: 1fr; gap: 12px; }
  .render-options { gap: 12px; }
  .random-button { width: 100%; margin-left: 0; justify-content: center; }
  .play-hint { max-width: calc(100% - 28px); white-space: normal; text-align: center; line-height: 1.35; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition: none !important; }
}
