/* ==========================================================================
   Glassify styles
   Theme tokens live on :root. Dark tokens are defined twice (system dark
   without an explicit choice, and explicit data-theme="dark") because plain
   CSS cannot share one block between a media query and an attribute selector.
   ========================================================================== */

:root {
  color-scheme: light;
  --bg: #eef2f5;
  --surface: #ffffff;
  --surface-2: #f4f7f9;
  --text: #14202b;
  --text-muted: #5a6a78;
  --border: #d3dce3;
  --accent: #1d6fa5;
  --accent-contrast: #ffffff;
  --accent-soft: #e2eef6;
  --error: #b3261e;
  --error-soft: #fdecea;
  --focus: #2b8fd6;

  /* Header is a dark brand bar in both themes (keeps iOS status-bar text readable). */
  --header-bg: #0f1b2d;
  --header-text: #eef4fa;
  --header-muted: #9fb3c8;

  /* Diagram tokens */
  --glass-fill: rgba(90, 150, 190, 0.22);
  --glass-stroke: #3a7aa3;
  --dg-box: #7d8e9c;
  --dg-dim: #4b5b69;
  --dg-diag: #8fa2b1;
  --dg-arc: #3a7aa3;
  --dg-text: #14202b;
  --dg-angle: #0f5a8a;
  --dg-stair: #a2aeb8;

  --radius: 16px;
  --shadow: 0 1px 2px rgba(15, 27, 45, 0.06), 0 4px 16px rgba(15, 27, 45, 0.06);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #0a1017;
    --surface: #131d27;
    --surface-2: #1a2733;
    --text: #e5edf4;
    --text-muted: #93a5b5;
    --border: #283847;
    --accent: #5db3e8;
    --accent-contrast: #06131d;
    --accent-soft: #16354b;
    --error: #ff8a80;
    --error-soft: #3a1a18;
    --focus: #7cc4f0;
    --glass-fill: rgba(120, 185, 230, 0.2);
    --glass-stroke: #7cc2ea;
    --dg-box: #6c7f8f;
    --dg-dim: #a9b9c6;
    --dg-diag: #5f7485;
    --dg-arc: #7cc2ea;
    --dg-text: #e5edf4;
    --dg-angle: #9ad3f5;
    --dg-stair: #4f6070;
    --shadow: none;
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0a1017;
  --surface: #131d27;
  --surface-2: #1a2733;
  --text: #e5edf4;
  --text-muted: #93a5b5;
  --border: #283847;
  --accent: #5db3e8;
  --accent-contrast: #06131d;
  --accent-soft: #16354b;
  --error: #ff8a80;
  --error-soft: #3a1a18;
  --focus: #7cc4f0;
  --glass-fill: rgba(120, 185, 230, 0.2);
  --glass-stroke: #7cc2ea;
  --dg-box: #6c7f8f;
  --dg-dim: #a9b9c6;
  --dg-diag: #5f7485;
  --dg-arc: #7cc2ea;
  --dg-text: #e5edf4;
  --dg-angle: #9ad3f5;
  --dg-stair: #4f6070;
  --shadow: none;
}

/* ---------- Base ---------- */

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

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial,
    "PingFang SC", "Hiragino Sans GB", "Noto Sans CJK SC", "Microsoft YaHei", sans-serif;
  font-size: 16px;
  line-height: 1.45;
  -webkit-tap-highlight-color: transparent;
  padding-left: env(safe-area-inset-left, 0px);
  padding-right: env(safe-area-inset-right, 0px);
}

[hidden] { display: none !important; }

h1, h2, h3 { margin: 0; line-height: 1.2; }
h2 { font-size: 1.2rem; }

button { font: inherit; color: inherit; }

:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }

/* ---------- Header ---------- */

.app-header {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  background: var(--header-bg);
  color: var(--header-text);
  padding: calc(env(safe-area-inset-top, 0px) + 10px) 16px 10px;
}

.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand img { flex: none; border-radius: 9px; }
.brand-text { min-width: 0; }
.brand h1 { font-size: 1.25rem; letter-spacing: 0.01em; }
.brand p {
  margin: 0;
  font-size: 0.78rem;
  color: var(--header-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.header-actions { display: flex; gap: 6px; flex: none; }

.hdr-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 44px;
  padding: 0 12px;
  border: 1px solid rgba(255, 255, 255, 0.18);
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.06);
  color: var(--header-text);
  font-size: 0.9rem;
  cursor: pointer;
}
.hdr-btn:active { background: rgba(255, 255, 255, 0.14); }
.hdr-icon { display: inline-flex; width: 18px; height: 18px; }
.hdr-icon svg { width: 18px; height: 18px; }

.lang-btn span[data-lang] { color: var(--header-muted); }
.lang-btn span[data-lang].active { color: var(--header-text); font-weight: 700; }
.lang-btn .sep { color: var(--header-muted); opacity: 0.6; }

/* On very narrow phones, keep the theme button icon-only. */
@media (max-width: 370px) {
  #themeLabel { display: none; }
  .brand p { display: none; }
}

/* ---------- Layout ---------- */

.container {
  max-width: 600px;
  margin: 0 auto;
  padding: 16px 12px;
  display: grid;
  gap: 16px;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
  min-width: 0;
}
.card > h2 { margin-bottom: 12px; }

.app-footer {
  text-align: center;
  color: var(--text-muted);
  font-size: 0.85rem;
  padding: 4px 16px calc(env(safe-area-inset-bottom, 0px) + 20px);
}
.app-footer p { margin: 0; }

/* ---------- How to measure ---------- */

.howto {
  margin: 0 0 12px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface-2);
}
.howto summary {
  cursor: pointer;
  padding: 12px 14px;
  min-height: 44px;
  font-weight: 600;
  color: var(--accent);
}
.howto p { margin: 0; padding: 0 14px 14px; color: var(--text-muted); font-size: 0.92rem; }
.howto-svg { display: block; width: 100%; max-width: 360px; margin: 0 auto 8px; padding: 0 8px; }
.sk-stair { fill: none; stroke: var(--text-muted); stroke-width: 2.5; stroke-linejoin: round; }
.sk-glass { fill: var(--glass-fill); stroke: var(--glass-stroke); stroke-width: 2; }
.sk-slope { stroke: var(--dg-stair); stroke-width: 1.5; stroke-dasharray: 5 4; }
.sk-dim { stroke: var(--accent); stroke-width: 2; }
.sk-ext { stroke: var(--dg-box); stroke-width: 1; }
.sk-dot { fill: var(--accent); }
.sk-label { fill: var(--accent); font-size: 20px; font-weight: 700; text-anchor: middle; }

/* ---------- Inputs ---------- */

.fields { display: grid; gap: 14px; }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
@media (max-width: 400px) {
  .field-row { grid-template-columns: 1fr; }
}

.field { min-width: 0; }
.field label {
  display: block;
  font-weight: 600;
  margin-bottom: 6px;
}
.field label b { color: var(--accent); margin-left: 2px; }

.input-wrap { position: relative; }

.field input {
  width: 100%;
  height: 58px;
  padding: 0 64px 0 14px;
  border: 2px solid var(--border);
  border-radius: 12px;
  background: var(--surface-2);
  color: var(--text);
  /* ≥ 16px prevents iOS zoom-on-focus; bigger is easier with gloves. */
  font-size: 1.5rem;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  -webkit-appearance: none;
  appearance: none;
}
.field input:focus { outline: none; border-color: var(--focus); background: var(--surface); }
.field input::placeholder { color: var(--text-muted); opacity: 0.6; }

.unit {
  position: absolute;
  right: 14px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-muted);
  font-size: 1rem;
  pointer-events: none;
}

.hint { margin: 6px 2px 0; font-size: 0.85rem; color: var(--text-muted); }
.error {
  margin: 6px 0 0;
  padding: 6px 10px;
  border-radius: 8px;
  background: var(--error-soft);
  color: var(--error);
  font-size: 0.92rem;
  font-weight: 600;
}
.error:empty { display: none; }
.field.invalid input { border-color: var(--error); }
.field.invalid .hint { display: none; }

.stepper { display: flex; gap: 8px; }
.stepper input { padding: 0 8px; text-align: center; min-width: 0; }
.step-btn {
  flex: none;
  width: 58px;
  height: 58px;
  border: 2px solid var(--border);
  border-radius: 12px;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 1.8rem;
  font-weight: 700;
  line-height: 1;
  cursor: pointer;
  touch-action: manipulation;
}
.step-btn:active { background: var(--accent); color: var(--accent-contrast); }

/* ---------- Results ---------- */

.results-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 12px;
}

.badge {
  display: inline-block;
  padding: 4px 10px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 700;
  font-size: 0.9rem;
}

.placeholder { margin: 0; color: var(--text-muted); }

.tiles {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.tile {
  padding: 12px;
  border-radius: 12px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  min-width: 0;
}
.tile-label { font-size: 0.9rem; font-weight: 600; color: var(--text-muted); }
.tile-value {
  font-size: clamp(1.9rem, 9vw, 2.5rem);
  font-weight: 800;
  line-height: 1.1;
  margin: 2px 0;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.01em;
}
.tile-value small { font-size: 0.45em; font-weight: 600; color: var(--text-muted); margin-left: 3px; }
.tile-sub { font-size: 0.82rem; color: var(--text-muted); }

.details-title { font-size: 1rem; margin: 18px 0 6px; }
.details { margin: 0 0 16px; }
.details > div {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
}
.details > div:last-child { border-bottom: 0; }
.details dt { color: var(--text-muted); }
.details dd { margin: 0; font-weight: 700; text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }

/* ---------- Buttons ---------- */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 52px;
  padding: 0 18px;
  border-radius: 12px;
  border: 2px solid var(--border);
  background: var(--surface-2);
  font-weight: 700;
  cursor: pointer;
  touch-action: manipulation;
}
.btn-primary {
  width: 100%;
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-contrast);
  font-size: 1.05rem;
}
.btn-primary:active { filter: brightness(0.92); }
.btn-small { min-height: 44px; padding: 0 12px; font-size: 0.92rem; }
.btn-small[aria-pressed="true"] { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }

/* ---------- Diagram ---------- */

.diagram-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 8px;
}

.diagram { width: 100%; }
.diagram svg { display: block; width: 100%; height: auto; overflow: visible; }

.note { margin: 8px 0 0; font-size: 0.85rem; color: var(--text-muted); font-style: italic; }
.overview-title { font-size: 1rem; margin: 20px 0 8px; }

/* SVG classes (drawn by app.js) */
.dg-box { fill: none; stroke: var(--dg-box); stroke-width: 1.5; stroke-dasharray: 2 4; stroke-linecap: round; }
.dg-glass { fill: var(--glass-fill); stroke: var(--glass-stroke); stroke-width: 2.5; stroke-linejoin: round; }
.dg-shine { stroke: #fff; stroke-opacity: 0.35; stroke-width: 5; stroke-linecap: round; }
.dg-diag-line { stroke: var(--dg-diag); stroke-width: 1; stroke-dasharray: 6 4; }
.dg-dim-line { stroke: var(--dg-dim); stroke-width: 1.2; }
.dg-ext-line { stroke: var(--dg-box); stroke-width: 1; opacity: 0.7; }
.dg-arc { fill: none; stroke: var(--dg-arc); stroke-width: 1.5; }
.dg-stair { stroke: var(--dg-stair); stroke-width: 1.5; stroke-dasharray: 5 4; }

.dg-text {
  fill: var(--dg-text);
  font-family: inherit;
  font-variant-numeric: tabular-nums;
  text-anchor: middle;
  /* halo so labels stay readable where they cross lines */
  paint-order: stroke;
  stroke: var(--surface);
  stroke-width: 4px;
  stroke-linejoin: round;
}
.dg-side { font-weight: 700; }
.dg-angle { fill: var(--dg-angle); font-weight: 800; }
.dg-dim { fill: var(--dg-dim); font-weight: 600; }
.dg-diag { fill: var(--dg-dim); font-weight: 500; }
.dg-num { fill: var(--dg-angle); font-weight: 800; }
