@layer reset, base, components, layout, utilities;

@layer reset {
  *, *::before, *::after { box-sizing: border-box; }
  html { color-scheme: dark; -webkit-text-size-adjust: 100%; }
  body, h1, h2, h3, p, dl, dd { margin: 0; }
  button, input, select { font: inherit; }
  button { border: 0; }
}

@layer base {
  :root {
    --bg: #071018;
    --surface: #0b1821;
    --surface-2: #10212b;
    --surface-3: #152a35;
    --line: #203945;
    --line-strong: #31505d;
    --text: #edf5f6;
    --muted: #93aab3;
    --faint: #9bb1bb;
    --accent: #62d2d0;
    --accent-strong: #8ee8e1;
    --accent-ink: #062323;
    --warm: #ffb454;
    --danger: #ff7b87;
    --radius-sm: 8px;
    --radius: 12px;
    --radius-lg: 16px;
    --shadow: 0 14px 42px rgba(0, 0, 0, .24);
    font-family: Inter, ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans JP", sans-serif;
    background: var(--bg);
    color: var(--text);
  }

  body {
    min-width: 320px;
    min-height: 100vh;
    min-height: 100dvh;
    background: var(--bg);
    color: var(--text);
    font-size: 16px;
    line-height: 1.55;
    overflow-x: hidden;
  }

  button, select, input { color: inherit; }
  button, select, input[type="range"], input[type="checkbox"] { cursor: pointer; }
  button:focus-visible, select:focus-visible, input:focus-visible, canvas:focus-visible {
    outline: 2px solid var(--accent-strong);
    outline-offset: 2px;
  }
  ::selection { color: #041819; background: var(--accent); }
  svg { width: 1.25rem; height: 1.25rem; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
  .svg-sprite { position: absolute; width: 0; height: 0; overflow: hidden; }
}

@layer components {
  .button, .icon-button {
    min-height: 48px;
    border: 1px solid transparent;
    border-radius: var(--radius-sm);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    font-weight: 650;
    font-size: .86rem;
    transition: background-color .18s ease, border-color .18s ease, color .18s ease;
    touch-action: manipulation;
  }
  .button:disabled, .icon-button:disabled { opacity: .45; cursor: not-allowed; }
  .button-primary { color: var(--accent-ink); background: var(--accent); }
  .button-primary:hover { background: var(--accent-strong); }
  .button-secondary { color: var(--text); background: var(--surface-2); border-color: var(--line); }
  .button-secondary:hover { background: var(--surface-3); border-color: var(--line-strong); }
  .button-quiet { color: var(--accent-strong); background: transparent; border-color: var(--line); }
  .button-quiet:hover { background: var(--surface-2); }
  .button.compact { min-height: 48px; padding: 0 11px; font-size: .8125rem; }
  .button.full { width: 100%; margin-top: 14px; }
  .icon-button { width: 48px; padding: 0; color: var(--text); background: rgba(11, 24, 33, .88); border-color: var(--line); }
  .icon-button:hover { background: var(--surface-3); border-color: var(--line-strong); }

  .field, .range-field { display: flex; flex-direction: column; gap: 7px; min-width: 0; }
  .field > span, .range-field b { color: var(--muted); font-size: .8125rem; font-weight: 650; letter-spacing: .01em; }
  .field input, .field select, .speed-control select, .number-suffix {
    width: 100%;
    min-height: 48px;
    border: 1px solid var(--line);
    border-radius: var(--radius-sm);
    padding: 0 11px;
    background: #0a171f;
    color: var(--text);
  }
  input[type="number"] { font-variant-numeric: tabular-nums; }
  input[type="range"] { width: 100%; height: 28px; accent-color: var(--accent); }
  .range-field { margin-top: 14px; }
  .range-field > span { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
  output { color: var(--text); font-size: .8125rem; font-variant-numeric: tabular-nums; }

  .section-heading { display: flex; justify-content: space-between; align-items: start; gap: 14px; margin-bottom: 15px; }
  .section-heading h3 { font-size: .96rem; line-height: 1.25; letter-spacing: -.01em; }
  .section-heading p { margin-top: 3px; color: var(--muted); font-size: .8125rem; line-height: 1.45; }
  .compact-heading { margin-bottom: 10px; }
  .divider { height: 1px; margin: 20px 0; background: var(--line); }
  .field-grid { display: grid; gap: 12px; }
  .field-grid.two { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); }
  .action-row { display: grid; gap: 8px; margin-top: 14px; }
  .action-row.three { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .action-row .button { min-width: 0; padding-inline: 7px; font-size: .8125rem; }
  .action-row .button svg { width: 16px; height: 16px; }

  .segmented { display: grid; grid-template-columns: repeat(3, 1fr); padding: 3px; border: 1px solid var(--line); border-radius: 10px; background: #08141c; }
  .segment { min-height: 48px; border-radius: 7px; color: var(--muted); background: transparent; font-weight: 650; }
  .segment.active { color: var(--text); background: var(--surface-3); box-shadow: inset 0 0 0 1px var(--line-strong); }

  .instruction { display: flex; align-items: flex-start; gap: 9px; margin-top: 14px; color: var(--muted); font-size: .8125rem; }
  .instruction-dot { width: 7px; height: 7px; flex: 0 0 auto; margin-top: 7px; border-radius: 50%; background: var(--accent); box-shadow: 0 0 0 4px rgba(98, 210, 208, .11); }
  .warning-card, .safety-note { margin-top: 15px; padding: 13px; border: 1px solid rgba(255, 180, 84, .32); border-radius: var(--radius); color: #f6d8ad; background: rgba(93, 61, 25, .2); font-size: .8125rem; }
  .safety-note { border-color: rgba(255, 123, 135, .32); color: #ffd7da; background: rgba(85, 31, 40, .2); }
  .safety-note p { margin-top: 5px; color: #d9b7bb; }

  .toast {
    position: fixed;
    z-index: 60;
    left: 50%;
    bottom: calc(84px + env(safe-area-inset-bottom));
    max-width: calc(100vw - 32px);
    padding: 10px 14px;
    border: 1px solid var(--line-strong);
    border-radius: 999px;
    color: var(--text);
    background: #142832;
    box-shadow: var(--shadow);
    font-size: .82rem;
    opacity: 0;
    transform: translate(-50%, 12px);
    pointer-events: none;
    transition: opacity .2s ease, transform .2s ease;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .toast.show { opacity: 1; transform: translate(-50%, 0); }
  .error-banner {
    position: fixed;
    z-index: 70;
    left: 12px;
    right: 12px;
    bottom: calc(82px + env(safe-area-inset-bottom));
    max-width: 680px;
    margin: auto;
    padding: 12px 12px 12px 15px;
    border: 1px solid rgba(255, 123, 135, .55);
    border-radius: 12px;
    color: #ffe5e7;
    background: #3a1720;
    box-shadow: var(--shadow);
    font-size: .84rem;
    align-items: center;
    gap: 12px;
  }
  .error-banner:not([hidden]) { display: flex; }
  .error-banner span { flex: 1; }
  .error-banner button { min-height: 48px; padding: 0 12px; border-radius: 7px; color: #fff; background: #692734; }
}

@layer layout {
  .app-shell { min-height: 100vh; min-height: 100dvh; padding-bottom: calc(78px + env(safe-area-inset-bottom)); }
  .app-header {
    min-height: 62px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 9px 14px;
    border-bottom: 1px solid var(--line);
    background: #08141d;
  }
  .brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
  .brand-mark { width: 39px; height: 39px; display: grid; place-items: center; border: 1px solid #2e6870; border-radius: 10px; color: var(--accent); background: #0b222a; }
  .brand-mark svg { width: 24px; height: 24px; }
  .brand h1 { font-size: .98rem; line-height: 1.18; letter-spacing: -.015em; }
  .brand p { color: var(--faint); font-size: .8125rem; line-height: 1.3; letter-spacing: .08em; text-transform: uppercase; }
  .header-actions { display: flex; align-items: center; gap: 8px; }
  .connection-badge { padding: 5px 8px; border: 1px solid rgba(255, 180, 84, .35); border-radius: 999px; color: #f2c98e; background: rgba(92, 59, 20, .24); font-size: .8125rem; }

  .workspace { width: 100%; max-width: 1450px; margin: 0 auto; }
  .visual-column { min-width: 0; }
  .canvas-card { position: relative; border-bottom: 1px solid var(--line); background: #071018; }
  .canvas-stage { position: relative; width: 100%; height: min(88vw, 390px); min-height: 285px; overflow: hidden; background: #061019; }
  #simulationCanvas { display: block; width: 100%; height: 100%; touch-action: none; cursor: grab; }
  #simulationCanvas:active { cursor: grabbing; }
  .canvas-toolbar { position: absolute; z-index: 5; top: 9px; left: 9px; right: 9px; display: flex; justify-content: space-between; align-items: center; pointer-events: none; }
  .canvas-toolbar > * { pointer-events: auto; }
  .mode-badge { min-height: 32px; display: inline-flex; align-items: center; padding: 0 10px; border: 1px solid rgba(177, 211, 219, .24); border-radius: 999px; color: #dcebed; background: rgba(7, 20, 29, .84); backdrop-filter: blur(8px); font-size: .8125rem; font-weight: 700; }
  .canvas-toolbar .icon-button { width: 48px; min-height: 48px; }
  .canvas-hint { position: absolute; z-index: 3; left: 50%; bottom: 12px; padding: 6px 9px; border-radius: 7px; color: #c4d6dc; background: rgba(5, 15, 22, .76); font-size: .8125rem; transform: translateX(-50%); white-space: nowrap; pointer-events: none; transition: opacity .25s ease; }
  .canvas-hint.faded { opacity: 0; }
  .loading-overlay { position: absolute; inset: 0; z-index: 4; display: flex; align-items: center; justify-content: center; gap: 10px; color: var(--muted); background: rgba(7, 16, 24, .9); font-size: .8125rem; transition: opacity .2s ease; }
  .loading-overlay.hidden { opacity: 0; pointer-events: none; }
  .spinner { width: 18px; height: 18px; border: 2px solid var(--line-strong); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; }
  @keyframes spin { to { transform: rotate(360deg); } }

  .legend { min-height: 48px; display: grid; grid-template-columns: auto minmax(60px, 150px) auto 1fr; align-items: center; gap: 8px; padding: 7px 12px; color: var(--faint); background: #0a151d; font-size: .8125rem; }
  .legend-gradient { height: 7px; border-radius: 99px; background: linear-gradient(90deg, #6f8bff 0%, #153c52 48%, #ffb14e 100%); }
  .legend-scale { justify-self: end; color: var(--muted); font-variant-numeric: tabular-nums; }

  .transport { min-height: 64px; display: flex; align-items: center; gap: 8px; padding: 9px 12px; border-bottom: 1px solid var(--line); background: var(--surface); }
  .transport-main { min-width: 96px; padding: 0 15px; }
  .transport-icon { flex: 0 0 48px; }
  .speed-control { min-width: 0; margin-left: auto; display: flex; align-items: center; gap: 8px; }
  .speed-control span { display: none; color: var(--muted); font-size: .8125rem; }
  .speed-control select { min-width: 82px; min-height: 48px; padding: 0 8px; border: 1px solid var(--line); border-radius: 8px; background: #0a171f; }

  .stats-panel { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); border-bottom: 1px solid var(--line); background: #0a151d; }
  .stats-panel > div { min-width: 0; padding: 9px 11px; border-right: 1px solid rgba(32, 57, 69, .7); border-bottom: 1px solid rgba(32, 57, 69, .7); }
  .stats-panel > div:nth-child(3n) { border-right: 0; }
  .stats-panel span { display: block; color: var(--faint); font-size: .8125rem; line-height: 1.3; }
  .stats-panel strong { display: block; margin-top: 2px; overflow: hidden; color: #d8e8eb; font-size: .8125rem; font-weight: 630; font-variant-numeric: tabular-nums; text-overflow: ellipsis; white-space: nowrap; }

  .control-panel { min-width: 0; background: var(--surface); }
  .panel-heading { display: flex; align-items: center; justify-content: space-between; gap: 16px; padding: 17px 16px 13px; border-bottom: 1px solid var(--line); }
  .eyebrow { color: var(--accent); font-size: .8125rem; font-weight: 760; letter-spacing: .16em; }
  .panel-heading h2 { margin-top: 2px; font-size: 1.12rem; letter-spacing: -.02em; }
  .solver-state { min-height: 29px; display: inline-flex; align-items: center; padding: 0 9px; border: 1px solid var(--line); border-radius: 999px; color: var(--muted); background: #09161e; font-size: .8125rem; }
  .solver-state.running { color: #b5f2ea; border-color: rgba(98, 210, 208, .42); background: rgba(32, 111, 110, .17); }
  .tab-panel { padding: 17px 16px 24px; }

  .preset-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 9px; }
  .preset-card { min-height: 82px; padding: 11px; border: 1px solid var(--line); border-radius: 10px; color: var(--text); background: #0b1922; text-align: left; }
  .preset-card:hover, .preset-card.active { border-color: #3f7880; background: #10262e; }
  .preset-card.active { box-shadow: inset 0 0 0 1px rgba(98, 210, 208, .25); }
  .preset-card strong { display: block; font-size: .83rem; }
  .preset-card span { display: block; margin-top: 3px; color: var(--muted); font-size: .8125rem; line-height: 1.35; }
  .insight-card { margin-top: 13px; padding: 13px; border: 1px solid #28535e; border-radius: 11px; background: #0b2028; }
  .insight-card > div { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; }
  .insight-card span { color: var(--muted); font-size: .8125rem; }
  .insight-card strong { color: var(--accent-strong); font-size: 1.02rem; font-variant-numeric: tabular-nums; }
  .insight-card p { margin-top: 3px; color: var(--faint); font-size: .8125rem; }

  .linked-control { display: grid; grid-template-columns: minmax(0, 1fr) 90px; align-items: end; gap: 10px; }
  .number-suffix { margin-bottom: 1px; }
  .position-readout { display: flex; justify-content: space-between; align-items: center; margin-top: 14px; padding: 11px; border: 1px solid var(--line); border-radius: 9px; background: #09161e; }
  .position-readout span { color: var(--muted); font-size: .8125rem; }
  .position-readout strong { font-size: .8125rem; font-variant-numeric: tabular-nums; }

  .observation-chart { display: block; width: 100%; height: 220px; border: 1px solid var(--line); border-radius: 10px; }
  .gauge-list { display: grid; gap: 7px; margin-top: 12px; }
  .gauge-item { min-height: 48px; display: grid; grid-template-columns: 10px 1fr auto auto; align-items: center; gap: 9px; padding: 6px 7px 6px 10px; border: 1px solid var(--line); border-radius: 9px; background: #0a171f; }
  .gauge-swatch { width: 8px; height: 24px; border-radius: 99px; }
  .gauge-item strong { display: block; font-size: .8125rem; }
  .gauge-item small { display: block; color: var(--faint); font-size: .8125rem; }
  .gauge-value { min-width: 72px; color: var(--text); font-size: .8125rem; font-variant-numeric: tabular-nums; text-align: right; }
  .gauge-item .icon-button { width: 48px; min-height: 48px; }
  .empty-state { padding: 28px 14px; color: var(--faint); font-size: .8125rem; text-align: center; }

  .toggle-list { display: grid; gap: 7px; margin-top: 14px; }
  .toggle-list label { min-height: 48px; display: flex; align-items: center; gap: 10px; padding: 0 10px; border: 1px solid var(--line); border-radius: 9px; color: var(--muted); background: #0a171f; font-size: .8125rem; }
  .toggle-list input { width: 18px; height: 18px; accent-color: var(--accent); }
  .info-card { padding: 14px; border: 1px solid var(--line); border-radius: 11px; background: #0a171f; }
  .info-card .formula { overflow-x: auto; padding: 5px 0; color: #d7eff1; font-family: "Times New Roman", serif; font-size: 1.02rem; white-space: nowrap; }
  .info-card > p:last-child { margin-top: 10px; color: var(--muted); font-size: .8125rem; }
  .model-facts { margin-top: 12px; border: 1px solid var(--line); border-radius: 11px; overflow: hidden; }
  .model-facts div { display: grid; grid-template-columns: 95px 1fr; gap: 12px; padding: 10px 12px; border-bottom: 1px solid var(--line); }
  .model-facts div:last-child { border-bottom: 0; }
  .model-facts dt { color: var(--faint); font-size: .8125rem; }
  .model-facts dd { color: var(--text); font-size: .8125rem; }

  .bottom-nav {
    position: fixed;
    z-index: 50;
    left: 0;
    right: 0;
    bottom: 0;
    height: calc(68px + env(safe-area-inset-bottom));
    display: grid;
    grid-template-columns: repeat(6, minmax(0, 1fr));
    padding: 5px 3px env(safe-area-inset-bottom);
    border-top: 1px solid var(--line-strong);
    background: rgba(8, 20, 29, .96);
    backdrop-filter: blur(14px);
  }
  .nav-item { min-width: 0; min-height: 56px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; border-radius: 8px; color: var(--faint); background: transparent; font-size: .8125rem; font-weight: 650; }
  .nav-item svg { width: 20px; height: 20px; }
  .nav-item.active { color: var(--accent-strong); background: rgba(98, 210, 208, .08); }
}

@layer utilities {
  [hidden] { display: none !important; }
}

@media (min-width: 600px) {
  .app-header { padding-inline: 22px; }
  .canvas-stage { height: min(70vw, 540px); }
  .speed-control span { display: inline; }
  .tab-panel, .panel-heading { padding-left: 22px; padding-right: 22px; }
  .preset-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (min-width: 980px) {
  .app-shell { padding-bottom: 86px; }
  .app-header { min-height: 68px; }
  .workspace { display: grid; grid-template-columns: minmax(0, 1fr) 390px; min-height: calc(100vh - 154px); padding: 18px; gap: 16px; align-items: start; }
  .visual-column, .control-panel { border: 1px solid var(--line); border-radius: var(--radius-lg); overflow: hidden; box-shadow: var(--shadow); }
  .canvas-card { border-bottom: 0; }
  .canvas-stage { height: min(calc(100vh - 320px), 680px); min-height: 460px; }
  .stats-panel { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .stats-panel > div { border-bottom: 0; }
  .stats-panel > div:nth-child(3n) { border-right: 1px solid rgba(32, 57, 69, .7); }
  .stats-panel > div:last-child { border-right: 0; }
  .control-panel { min-height: calc(100vh - 104px); }
  .tab-panel { max-height: calc(100vh - 186px); overflow-y: auto; }
  .bottom-nav { left: 50%; right: auto; bottom: 12px; width: 610px; height: 62px; padding: 4px; border: 1px solid var(--line-strong); border-radius: 14px; transform: translateX(-50%); box-shadow: var(--shadow); }
  .nav-item { min-height: 52px; flex-direction: row; gap: 7px; font-size: .8125rem; }
}

@media (max-width: 370px) {
  .brand p { display: none; }
  .header-actions .button { display: none !important; }
  .canvas-stage { min-height: 280px; }
  .transport { gap: 6px; padding-inline: 8px; }
  .transport-main { min-width: 86px; padding-inline: 11px; }
  .stats-panel > div { padding-inline: 8px; }
  .tab-panel { padding-inline: 13px; }
  .nav-item { font-size: .8125rem; }
  .action-row.three { grid-template-columns: 1fr; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition-duration: .01ms !important; }
}

/* Readability, Android touch targets, and compact scientific metadata. */
@layer utilities {
  .brand h1 { font-size: 20px; }
  .brand p { font-size: 13px; letter-spacing: 0; text-transform: none; }
  .app-header { flex-wrap: wrap; }
  .header-actions { margin-left: auto; }
  .field input, .field select, .number-suffix { font-size: 16px; }
  .field > span, .range-field b { font-size: 14px; }
  .section-heading h3 { font-size: 18px; }
  .section-heading p, .preset-card span, .instruction { font-size: 14px; line-height: 1.6; }
  .preset-card strong { font-size: 16px; }
  .stats-panel strong { font-size: 14px; }
  .stats-panel span, .nav-item { font-size: 13px; }
  .stats-panel > div { padding: 8px 10px; }
  .source-coordinates { margin-top: 16px; }
  .terrain-legend { display: flex; flex-wrap: wrap; gap: 12px; padding: 4px 12px 8px; font-size: 13px; color: var(--muted); }
  .terrain-legend span:nth-child(3) { color: #8accd1; }
  .terrain-legend span:nth-child(4) { color: #c2c5af; }
  .legend { grid-template-columns: auto minmax(30px, 1fr) auto; font-size: 13px; }
  .legend-scale { grid-column: 1 / -1; }
  .safety-inline { color: #b9c8ce; font-size: 13px; margin-bottom: 16px; }
  .transport-main { min-width: 112px; padding-inline: 10px; }
  .transport { gap: 6px; }
  .speed-control select { min-width: 74px; }
  .button.compact, .error-banner button { min-height: 48px; }
  input[type="range"] { height: 48px; }
  .nav-item[aria-current] { box-shadow: inset 0 -3px var(--accent); }
  .gauge-item { grid-template-columns: 8px minmax(0, 1fr) auto 48px; gap: 6px; }
  .gauge-value { min-width: 62px; }
  @media (min-width: 980px) { .control-panel { min-height: 0; } .canvas-stage { min-height: 400px; } }
}

@media (min-width: 980px) {
  .canvas-stage { height: min(calc(100dvh - 510px), 640px); min-height: 300px; }
  .control-panel { min-height: 0; }
  .speed-control span { white-space: nowrap; }
}
