/* ============================================================
   Japan Travel Tracker — restyled to the personal Design System (v1.2)
   Tokens come from vendor/design-system/tokens.css. This file maps
   those tokens to the app's surfaces and components. No raw hex except
   the two fixed map-label contrast values (flagged below).
   Swiss bones: radius 0, 1px hairlines, no shadows. Rust = interaction
   signal only (hover / focus / selected). Light + Espresso dark.
   The map workspace opts into the v1.2 VIVID data tier (data-viz="vivid"
   in index.html) so prefecture categories read as distinct data ink;
   chrome stays on the muted brand tokens.
   ============================================================ */

/* ── App-level semantic variables (compose tokens; never sprinkle hex) ── */
:root {
  --map-empty:   var(--s-input);     /* never been — empty paper */
  /* Category data ink. These are the MUTED (fallback) values; they are
     re-declared under [data-viz="vivid"] below so the map workspace resolves
     them to the v1.2 vivid ramp (chart-3 = green, chart-4 = gold). */
  --map-visited: var(--chart-3);     /* slate-blue (muted) → green (vivid) */
  --map-stayed:  var(--chart-4);     /* ochre      (muted) → gold  (vivid) */
  --map-stroke:  var(--border-strong);

  /* Fixed (non-theming) ink/paper for kanji contrast against the chart fills,
     which do NOT flip with theme the way --color-ink does. Per design-system
     §5, a value tokens can't express is defined once here and flagged, rather
     than scattered as literals. These mirror the design-system ink/paper. */
  --map-fixed-ink:   #15140e;        /* = design-system ink */
  --map-fixed-paper: #faf9f5;        /* = design-system paper-elevated */

  --map-label-empty:   var(--color-ink);
  --map-label-visited: var(--map-fixed-paper); /* paper — reads on dark slate-blue */
  --map-label-stayed:  var(--map-fixed-ink);   /* ink  — reads on light ochre */

  --map-font: 'Hiragino Sans', 'Yu Gothic', 'Noto Sans JP', var(--font-zh), sans-serif;
}
[data-theme="dark"] {
  /* dark chart-3/4 are light tints (#7e9bca / #d2a05c) → ink labels read best */
  --map-label-visited: var(--map-fixed-ink);
  --map-label-stayed:  var(--map-fixed-ink);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --map-label-visited: var(--map-fixed-ink);
    --map-label-stayed:  var(--map-fixed-ink);
  }
}
/* Vivid tier (v1.2). NOTE on CSS custom-property resolution: a property
   substitutes its var() at its DECLARATION site, then inherits the resolved
   value. So --map-visited: var(--chart-3) declared on :root resolves against
   :root's MUTED --chart-3 and inherits muted — even though --chart-3 is vivid
   on this element. We must therefore RE-DECLARE the category vars here so their
   var(--chart-*) re-resolves against the vivid ramp in this scope. */
[data-viz="vivid"] {
  --map-visited: var(--chart-3);              /* re-resolved → vivid green */
  --map-stayed:  var(--chart-4);              /* re-resolved → vivid gold  */
  /* The bright green fill needs an INK kanji label, not paper — ink ~5.9:1 vs
     paper ~3.0:1 on chart-3 green (WCAG AA). Coupled to the tier here, so paper
     returns automatically if the map ever reverts to muted slate-blue. "Stayed"
     is already ink in every theme, so only "visited" flips. */
  --map-label-visited: var(--map-fixed-ink);
}

/* ── Base ── */
html, body { background: var(--s-window); }
body {
  color: var(--t1);
  font-family: var(--font-sans);
  font-size: var(--text-base);
  line-height: var(--leading-normal);
  min-height: 100vh;
}
:lang(zh-Hant), :lang(zh-TW) { letter-spacing: var(--tracking-wide); }

.muted { color: var(--t3); }
.small { font-size: var(--text-sm); }

a { color: var(--color-ink); text-decoration: underline; text-decoration-color: var(--color-accent); text-underline-offset: 3px; }
a:hover { color: var(--color-accent); }

/* Icon sprite <defs> holder — out of flow (was an inline style attribute,
   moved here so the CSP can drop style-src 'unsafe-inline') */
.icon-sprite { position: absolute; }

/* Icons — true 1.5px stroke at every size (matches the UI hairline) */
.ic {
  width: 18px; height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.5;
  stroke-linecap: square;
  stroke-linejoin: miter;
  vector-effect: non-scaling-stroke;
  flex-shrink: 0;
}
.ic circle { fill: none; }

/* Global focus: ink ring + inset rust underline (never a glow) */
:focus-visible {
  outline: 2px solid var(--t1);
  outline-offset: 2px;
  box-shadow: inset 0 -2px 0 var(--color-accent);
}

/* ============================================================
   Top bar
   ============================================================ */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  gap: var(--space-4) var(--space-6);
  flex-wrap: wrap;
  padding: var(--space-3) var(--container-padding);
  background: var(--s-surface);
  border-bottom: var(--border-width) solid var(--border);
}
.brand { display: inline-flex; align-items: center; gap: var(--space-2); text-decoration: none; color: var(--t1); }
.brand:hover { color: var(--t1); }
.brand .ic { width: 22px; height: 22px; color: var(--color-accent); }
.brand-name { font-weight: var(--weight-semibold); font-size: var(--text-lg); letter-spacing: var(--tracking-tight); }
.topbar-actions { display: flex; align-items: center; gap: var(--space-2); flex-wrap: wrap; }

/* ── Buttons (from product-ds patterns) ── */
.btn {
  font-family: var(--font-sans); font-weight: var(--weight-medium);
  font-size: var(--text-sm); line-height: 1;
  height: 36px; padding: 0 var(--space-4);
  display: inline-flex; align-items: center; justify-content: center; gap: var(--space-2);
  border: var(--border-width) solid transparent; border-radius: var(--radius);
  background: none; color: var(--t1); cursor: pointer; white-space: nowrap;
  transition: background var(--duration-fast) var(--ease-out),
              border-color var(--duration-fast) var(--ease-out),
              color var(--duration-fast) var(--ease-out);
}
.btn .ic { width: 16px; height: 16px; }
.btn.primary { background: var(--primary-bg); color: var(--primary-fg); }
.btn.primary:hover { background: var(--primary-hover); }
.btn.secondary { border-color: var(--border-strong); color: var(--t1); background: var(--s-elevated); }
.btn.secondary:hover { border-color: var(--t1); background: var(--s-hover); }
.btn.ghost { color: var(--t2); }
.btn.ghost:hover { background: var(--s-hover); color: var(--t1); }
.btn.danger { color: var(--error); border-color: var(--error-border); background: var(--error-tint); }
.btn.danger:hover { border-color: var(--error); }
.btn.sm { height: 30px; padding: 0 var(--space-3); }

.icon-btn {
  width: 36px; height: 36px; display: grid; place-items: center;
  border: var(--border-width) solid transparent; border-radius: var(--radius);
  background: none; color: var(--t2); cursor: pointer;
  transition: background var(--duration-fast) var(--ease-out), color var(--duration-fast) var(--ease-out);
}
.icon-btn:hover { background: var(--s-hover); color: var(--t1); }

/* ── Segmented language toggle ── */
.seg { display: inline-flex; border: var(--border-width) solid var(--border-strong); background: var(--s-input); }
.seg button {
  font-family: var(--font-mono); font-size: var(--text-xs); letter-spacing: var(--tracking-wide);
  padding: 0 var(--space-3); height: 36px;
  background: none; border: none; border-right: var(--border-width) solid var(--border);
  color: var(--t3); cursor: pointer;
  transition: background var(--duration-fast) var(--ease-out), color var(--duration-fast) var(--ease-out);
}
.seg button:last-child { border-right: none; }
.seg button[aria-pressed="true"] { background: var(--t1); color: var(--primary-fg); }
.lang-toggle button[data-lang="zh-TW"] { font-family: var(--font-zh); letter-spacing: var(--tracking-wide); }

/* Theme toggle — show the icon for the action available */
.theme-toggle .i-sun { display: none; }
.theme-toggle .i-moon { display: block; }
[data-theme="dark"] .theme-toggle .i-sun { display: block; }
[data-theme="dark"] .theme-toggle .i-moon { display: none; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .theme-toggle .i-sun { display: block; }
  :root:not([data-theme="light"]) .theme-toggle .i-moon { display: none; }
}

/* ============================================================
   Layout
   ============================================================ */
.layout {
  display: grid;
  grid-template-columns: minmax(0, 2fr) minmax(280px, 1fr);
  gap: var(--space-6);
  max-width: var(--container-max);
  margin: 0 auto;
  padding: var(--space-8) var(--container-padding) var(--space-12);
  animation: rise var(--duration-slow) var(--ease-out) both;
}
.sidecol { display: flex; flex-direction: column; gap: var(--space-6); }

/* one orchestrated page-load entrance (no scattered micro-interactions) */
@keyframes rise { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }
@media (prefers-reduced-motion: reduce) { .layout { animation: none; } }

/* ── Panels (depth by tone + hairline; never shadow) ── */
.panel { background: var(--s-surface); border: var(--border-width) solid var(--border); }
.panel-head { padding: var(--space-3) var(--space-4); border-bottom: var(--border-width) solid var(--border); }
.panel-title {
  font-family: var(--font-mono); font-weight: var(--weight-medium);
  font-size: var(--text-xs); letter-spacing: var(--tracking-wider); text-transform: uppercase;
  color: var(--t3);
}
.panel-body { padding: var(--space-4); }

/* ============================================================
   Map
   ============================================================ */
#japan-map-container {
  text-align: center;
  background: var(--s-elevated);
  border: var(--border-width) solid var(--border);
  overflow: hidden;
  min-height: 640px;
  display: flex; align-items: center; justify-content: center;
}
#japan-svg { width: 100%; height: auto; max-height: 760px; }
#background rect { fill: transparent; }

/* Prefecture shapes — the `.prefecture` is the shape itself for rects/polys,
   or a <g> wrapper whose children are shapes (hyogo / osaka / niigata). Target
   both so CSS fill overrides the SVG presentation attributes. */
#area .prefecture,
#area .prefecture > polygon,
#area .prefecture > rect {
  fill: var(--map-empty);
  stroke: var(--map-stroke);
  stroke-width: 1.5px;
  vector-effect: non-scaling-stroke;
  stroke-linejoin: miter;
  cursor: pointer;
  transition: fill var(--duration-fast) var(--ease-out), stroke var(--duration-fast) var(--ease-out);
}
#area .prefecture[data-level="1"],
#area .prefecture[data-level="1"] > polygon,
#area .prefecture[data-level="1"] > rect { fill: var(--map-visited); }
#area .prefecture[data-level="2"],
#area .prefecture[data-level="2"] > polygon,
#area .prefecture[data-level="2"] > rect { fill: var(--map-stayed); }

/* Road trip — dashed ink border (a property, not interaction) */
#area .prefecture[data-roadtrip="true"],
#area .prefecture[data-roadtrip="true"] > polygon,
#area .prefecture[data-roadtrip="true"] > rect {
  stroke: var(--color-ink);
  stroke-dasharray: 5 3;
  stroke-width: 1.5px;
}

/* Hover / keyboard focus / selected — the only place rust appears on the map */
#area .prefecture:hover,
#area .prefecture:hover > polygon,
#area .prefecture:hover > rect,
#area .prefecture:focus-visible,
#area .prefecture:focus-visible > polygon,
#area .prefecture:focus-visible > rect,
#area .prefecture[data-selected="true"],
#area .prefecture[data-selected="true"] > polygon,
#area .prefecture[data-selected="true"] > rect {
  stroke: var(--color-accent);
  stroke-width: 3px;
}
#area .prefecture:focus { outline: none; }
#area .prefecture:focus-visible { outline: none; box-shadow: none; }

/* Kanji labels */
.pref-label { fill: var(--map-label-empty); font-family: var(--map-font); font-weight: var(--weight-semibold); }
.pref-label[data-level="1"] { fill: var(--map-label-visited); }
.pref-label[data-level="2"] { fill: var(--map-label-stayed); }

/* Tooltip — elevated tone + hairline, no shadow */
.prefecture-tooltip {
  position: absolute;
  background: var(--s-elevated);
  color: var(--t1);
  border: var(--border-width) solid var(--border-strong);
  padding: var(--space-1) var(--space-2);
  font-size: var(--text-sm);
  pointer-events: none; z-index: 1000;
  opacity: 0; transition: opacity var(--duration-fast) var(--ease-out);
}
.prefecture-tooltip.show { opacity: 1; }

/* ============================================================
   Legend
   ============================================================ */
.legend-item { display: flex; align-items: center; gap: var(--space-3); font-size: var(--text-sm); padding: var(--space-1) 0; }
.legend-swatch { width: 18px; height: 18px; flex-shrink: 0; border: var(--border-width) solid var(--map-stroke); }
.legend-swatch.never-been { background: var(--map-empty); }
.legend-swatch.visited { background: var(--map-visited); border-color: var(--map-visited); }
.legend-swatch.stayed { background: var(--map-stayed); border-color: var(--map-stayed); }
.legend-swatch.road-trip { background: var(--map-empty); border: var(--border-width) dashed var(--color-ink); }

/* ============================================================
   Statistics rows (numbers right-aligned, mono tabular)
   ============================================================ */
.stat-item {
  display: flex; justify-content: space-between; align-items: baseline; gap: var(--space-3);
  font-size: var(--text-sm); color: var(--t2);
  padding: var(--space-2) 0; border-top: var(--border-width) solid var(--border);
}
.stat-item:first-child { border-top: none; }
.stat-number, .stat-item strong {
  font-family: var(--font-mono); font-variant-numeric: tabular-nums;
  font-weight: var(--weight-medium); color: var(--t1); white-space: nowrap;
}

/* ============================================================
   Selected prefecture info
   ============================================================ */
#prefecture-info { min-height: 96px; }
#prefecture-info h3 { font-size: var(--text-lg); font-weight: var(--weight-semibold); margin-bottom: var(--space-3); }
.prefecture-visits { border: var(--border-width) solid var(--border); background: var(--s-elevated); }
.visit-item {
  display: flex; justify-content: space-between; align-items: center; gap: var(--space-3);
  padding: var(--space-2) var(--space-3); border-top: var(--border-width) solid var(--border);
}
.prefecture-visits .visit-item:first-child { border-top: none; }
.visit-info { display: flex; flex-wrap: wrap; align-items: center; gap: var(--space-2); flex: 1; }
.visit-date { font-family: var(--font-mono); font-size: var(--text-sm); color: var(--t3); white-space: nowrap; }
.visit-notes { padding: 0 var(--space-3) var(--space-2); color: var(--t3); font-size: var(--text-sm); }

/* Level + road-trip pills (color + word) */
.pill {
  display: inline-flex; align-items: center; gap: var(--space-1);
  font-size: var(--text-xs); font-weight: var(--weight-medium);
  padding: 2px var(--space-2); border: var(--border-width) solid;
}
.pill .dot { width: 7px; height: 7px; flex-shrink: 0; }
.pill.visited { color: var(--t1); border-color: var(--map-visited); background: var(--s-surface); }
.pill.visited .dot { background: var(--map-visited); }
.pill.stayed { color: var(--t1); border-color: var(--map-stayed); background: var(--s-surface); }
.pill.stayed .dot { background: var(--map-stayed); }
.pill.road-trip { color: var(--t2); border-color: var(--border-strong); background: var(--s-elevated); }

/* ============================================================
   Timeline
   ============================================================ */
.timeline { position: relative; padding-left: var(--space-6); }
.timeline::before { content: ''; position: absolute; left: 7px; top: 4px; bottom: 4px; width: var(--border-width); background: var(--border-strong); }
.timeline-item { position: relative; margin-bottom: var(--space-4); background: var(--s-elevated); border: var(--border-width) solid var(--border); padding: var(--space-3) var(--space-4); }
.timeline-item::before { content: ''; position: absolute; left: calc(-1 * var(--space-6) + 3px); top: var(--space-4); width: 9px; height: 9px; background: var(--map-visited); border: var(--border-width) solid var(--s-surface); }
.timeline-item.stayed::before { background: var(--map-stayed); }
.timeline-item.road-trip::before { background: var(--color-ink); }
.timeline-date { font-family: var(--font-mono); font-size: var(--text-sm); color: var(--t3); margin-bottom: var(--space-1); }
.timeline-prefecture { font-size: var(--text-lg); font-weight: var(--weight-medium); margin-bottom: var(--space-1); }
.timeline-details { font-size: var(--text-sm); color: var(--t2); }
.timeline-details em { color: var(--t3); }

/* ============================================================
   Statistics modal grid
   ============================================================ */
.stats-grid { display: grid; grid-template-columns: 1fr 1fr; gap: var(--space-6) var(--space-8); }
.stats-grid h3 { font-family: var(--font-mono); font-size: var(--text-xs); letter-spacing: var(--tracking-wider); text-transform: uppercase; color: var(--t3); margin-bottom: var(--space-2); }
.stats-block { margin-bottom: var(--space-4); }

/* ============================================================
   Dialog modals — scrim + hairline + elevated tone, NO shadow
   ============================================================ */
/* margin:auto re-centers the dialog — the tokens.css reset zeroes the UA default */
.modal { margin: auto; padding: 0; border: var(--border-width) solid var(--border-strong); background: var(--s-elevated); color: var(--t1); width: min(440px, calc(100vw - 2 * var(--space-4))); }
.modal.modal-lg { width: min(720px, calc(100vw - 2 * var(--space-4))); }
.modal::backdrop { background: var(--scrim); }
.modal-content { display: flex; flex-direction: column; }
.modal-head { display: flex; align-items: center; justify-content: space-between; gap: var(--space-4); padding: var(--space-4); border-bottom: var(--border-width) solid var(--border); }
.modal-title { font-size: var(--text-lg); font-weight: var(--weight-semibold); }
.modal-body { padding: var(--space-4); max-height: 70vh; overflow: auto; }
.modal-foot { display: flex; justify-content: flex-end; gap: var(--space-2); padding: var(--space-4); border-top: var(--border-width) solid var(--border); }

/* ── Form fields ── */
.field { display: flex; flex-direction: column; gap: var(--space-2); margin-bottom: var(--space-4); }
.field > label { font-size: var(--text-sm); font-weight: var(--weight-medium); color: var(--t1); }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: var(--space-4); }
.inp {
  font-family: var(--font-sans); font-size: var(--text-base); color: var(--t1);
  background: var(--s-input); border: var(--border-width) solid var(--border); border-radius: var(--radius);
  min-height: 38px; padding: var(--space-2) var(--space-3); width: 100%;
  transition: border-color var(--duration-fast) var(--ease-out), box-shadow var(--duration-fast) var(--ease-out);
}
textarea.inp { resize: vertical; }
.inp:hover { border-color: var(--border-strong); }
.inp:focus { outline: none; border-color: var(--t1); box-shadow: inset 0 -2px 0 var(--color-accent); }
.selwrap { position: relative; }
.selwrap .chev { position: absolute; right: var(--space-3); top: 50%; transform: translateY(-50%); width: 16px; height: 16px; color: var(--t3); pointer-events: none; }
select.inp { appearance: none; -webkit-appearance: none; padding-right: var(--space-8); cursor: pointer; }

/* checkbox → switch */
.choice { display: inline-flex; align-items: center; gap: var(--space-3); font-size: var(--text-base); color: var(--t1); cursor: pointer; margin-bottom: var(--space-4); }
.choice input { position: absolute; opacity: 0; pointer-events: none; }
.switch { width: 38px; height: 22px; background: var(--s-input); border: var(--border-width) solid var(--border-strong); flex-shrink: 0; position: relative; transition: background var(--duration-fast) var(--ease-out), border-color var(--duration-fast) var(--ease-out); }
.switch .knob { position: absolute; top: 2px; left: 2px; width: 16px; height: 16px; background: var(--t3); transition: transform 180ms var(--ease-out), background var(--duration-fast) var(--ease-out); }
.choice input:checked + .switch { background: var(--color-accent); border-color: var(--color-accent); }
.choice input:checked + .switch .knob { transform: translateX(16px); background: var(--on-solid); }
.choice input:focus-visible + .switch { outline: 2px solid var(--t1); outline-offset: 2px; }

/* ============================================================
   Footer
   ============================================================ */
.site-footer { border-top: var(--border-width) solid var(--border); background: var(--s-surface); margin-top: var(--space-12); }
.footer-inner { max-width: var(--container-max); margin: 0 auto; padding: var(--space-8) var(--container-padding); display: flex; justify-content: space-between; gap: var(--space-8); flex-wrap: wrap; }
.footer-h { font-family: var(--font-mono); font-size: var(--text-xs); letter-spacing: var(--tracking-wider); text-transform: uppercase; color: var(--t3); margin-bottom: var(--space-3); }
.footer-inner p { margin-bottom: var(--space-2); max-width: 60ch; }
.footer-right { text-align: right; }
.link-icon + .ic { width: 13px; height: 13px; vertical-align: middle; color: var(--t3); }

/* ============================================================
   Responsive
   ============================================================ */
@media (max-width: 900px) {
  .layout { grid-template-columns: 1fr; }
  #japan-map-container { min-height: 380px; }
}
@media (max-width: 600px) {
  /* Stack the bar: brand on its own row, then a 2-col control grid.
     Labels stay visible (icon-only was unclear); the grid keeps the
     language + theme controls together on the last row, never orphaned. */
  .topbar { flex-direction: column; align-items: stretch; gap: var(--space-3); padding: var(--space-3) var(--space-4); }
  .topbar-actions { width: 100%; display: grid; grid-template-columns: 1fr 1fr; gap: var(--space-2); }
  .topbar-actions .btn { width: 100%; justify-content: flex-start; height: 40px; }
  .lang-toggle { justify-self: start; }
  .theme-toggle { justify-self: end; }
  .layout { padding: var(--space-4); gap: var(--space-4); }
  .field-row { grid-template-columns: 1fr; }
  .stats-grid { grid-template-columns: 1fr; }
  .footer-inner { flex-direction: column; gap: var(--space-6); }
  .footer-right { text-align: left; }
}
