:root {
  color-scheme: dark;

  /* surfaces */
  --bg: #0a0f1c;
  --panel: #101828;
  --panel2: #0d1424;
  --line: rgba(148, 163, 184, .14);
  --btn-bg: #1a2540;
  --float: #060b16;
  --float-edge: rgba(76, 195, 255, .5);
  --shadow: rgba(0, 0, 0, .55);

  /* text */
  --txt: #e8eef7;
  --dim: #93a1b8;

  /* meaning: one color, one job */
  --acc: #4cc3ff;          /* interactive only: focus, links, controls */
  --acc-dim: rgba(76, 195, 255, .14);
  --on-acc: #06121c;
  --now: #ff6b81;          /* the present moment: now line, now pill, today */
  --sun: #ffb224;          /* solar only: sun markers + sun text */
  --weekend: #c4b5fd;      /* weekend dates */
  --sel-fill: rgba(76, 195, 255, .26);
  --sel-edge: rgba(76, 195, 255, .65);
  --we-tint: rgba(255, 255, 255, .025);
  --colhl: rgba(255, 255, 255, .06);
  --colhl-edge: rgba(255, 255, 255, .22);
  --tick: rgba(255, 178, 36, .5);

  /* sun-phase ramp: luminance-ordered */
  --band-day-bg: rgba(186, 214, 255, .20);
  --band-day-ink: #ffffff;
  --band-golden-bg: rgba(255, 178, 36, .30);
  --band-golden-ink: #ffd9a0;
  --band-civil-bg: rgba(167, 139, 250, .22);
  --band-civil-ink: #c4b5fd;
  --band-nautical-bg: rgba(76, 108, 255, .16);
  --band-nautical-ink: #a9b8e8;
  --band-astro-bg: rgba(76, 108, 255, .08);
  --band-astro-ink: #9aa8d8;
  --band-night-bg: transparent;
  --band-night-ink: #7e8cab;

  /* type scale */
  --fs-2xs: 10px;
  --fs-xs: 11px;
  --fs-sm: 12px;
  --fs-base: 13px;
  --fs-md: 14px;
  --fs-title: 15px;
  --fs-lg: 19px;

  --ease-out: cubic-bezier(.23, 1, .32, 1);
  --ruler-dates-h: 26px;
  --ruler-h: 36px;
}
[data-theme="light"] {
  color-scheme: light;
  --bg: #eef2f7;
  --panel: #ffffff;
  --panel2: #f6f8fb;
  --line: rgba(15, 23, 42, .14);
  --btn-bg: #e2e8f0;
  --float: #ffffff;
  --float-edge: rgba(3, 105, 161, .45);
  --shadow: rgba(15, 23, 42, .18);
  --txt: #0f172a;
  --dim: #475569;
  --acc: #0369a1;
  --acc-dim: rgba(2, 132, 201, .12);
  --on-acc: #ffffff;
  --now: #e11d48;
  --sun: #b45309;
  --weekend: #6d28d9;
  --sel-fill: rgba(2, 132, 201, .18);
  --sel-edge: rgba(2, 132, 201, .65);
  --we-tint: rgba(15, 23, 42, .05);
  --colhl: rgba(2, 132, 201, .10);
  --colhl-edge: rgba(2, 132, 201, .35);
  --tick: rgba(180, 83, 9, .55);
  --band-day-bg: #ffffff;
  --band-day-ink: #0f172a;
  --band-golden-bg: #e9d7c2;
  --band-golden-ink: #92400e;
  --band-civil-bg: #e0dcf6;
  --band-civil-ink: #5b21b6;
  --band-nautical-bg: #d5def3;
  --band-nautical-ink: #1e40af;
  --band-astro-bg: #cdd5e6;
  --band-astro-ink: #172f6e;
  --band-night-bg: #1e293b;
  --band-night-ink: #cbd5e1;
}
* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--txt);
  font: var(--fs-base)/1.5 Inter, ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif;
  -webkit-font-smoothing: antialiased;
  display: flex; flex-direction: column; overflow: hidden;
}
.tnum { font-variant-numeric: tabular-nums; }

/* skip link */
.skip {
  position: fixed; top: -48px; left: 12px; z-index: 100;
  background: var(--acc); color: var(--on-acc); font-weight: 700;
  padding: 8px 14px; border-radius: 8px; text-decoration: none;
  transition: top 160ms var(--ease-out);
}
.skip:focus-visible { top: 8px; }

/* ---------- header ---------- */
header {
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
  padding: 10px 16px; border-bottom: 1px solid var(--line); background: var(--panel);
}
.brand { display: flex; align-items: baseline; gap: 10px; }
.brand h1 { font-size: var(--fs-title); font-weight: 800; letter-spacing: .01em; margin: 0; }
.brand small { color: var(--dim); font-size: var(--fs-xs); }
.searchwrap { position: relative; }
.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}
#search {
  width: 240px; padding: 7px 12px; border-radius: 9px; font: inherit;
  border: 1px solid var(--line); background: var(--panel2); color: var(--txt);
  transition: border-color 150ms var(--ease-out);
}
#search::placeholder { color: var(--dim); }
#search:focus { outline: none; border-color: var(--acc); }
#drop {
  position: absolute; top: calc(100% + 6px); left: 0; right: 0; z-index: 50;
  background: var(--panel); border: 1px solid var(--line); border-radius: 10px;
  overflow: hidden; box-shadow: 0 16px 40px var(--shadow);
}
#drop .opt { padding: 8px 12px; cursor: pointer; transition: background-color 120ms var(--ease-out); }
#drop .opt:hover, #drop .opt.active, #drop .opt[aria-selected="true"] { background: var(--acc-dim); }
#drop .opt span { color: var(--dim); font-size: var(--fs-xs); }
.spacer { flex: 1; }
.controls { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }

/* buttons: one language */
button {
  font: inherit; color: var(--txt); background: var(--btn-bg);
  border: 1px solid var(--line); border-radius: 9px; padding: 7px 12px; cursor: pointer;
  min-height: 32px;
  transition: transform 160ms var(--ease-out), background-color 150ms var(--ease-out),
    border-color 150ms var(--ease-out), color 150ms var(--ease-out);
}
button:hover { border-color: var(--acc); }
button:active { transform: scale(.96); }
button:focus-visible, input:focus-visible, [tabindex]:focus-visible {
  outline: 2px solid var(--acc); outline-offset: 2px;
}
.seg { display: inline-flex; background: var(--panel2); border: 1px solid var(--line); border-radius: 10px; padding: 2px; gap: 2px; }
.seg button { border: 0; background: transparent; min-height: 28px; padding: 4px 10px; color: var(--dim); }
.seg button[aria-pressed="true"] { background: var(--acc-dim); color: var(--acc); font-weight: 700; }
.seg button:active { transform: scale(.95); }
.tgl[aria-pressed="true"] { background: var(--acc-dim); border-color: var(--acc); color: var(--acc); }
.btn-primary { background: var(--acc); border-color: var(--acc); color: var(--on-acc); font-weight: 700; }
.btn-primary:hover { border-color: var(--txt); }
.btn-quiet { background: transparent; }

/* ---------- grid ---------- */
#grid { flex: 1; overflow: auto; position: relative; touch-action: pan-x pan-y; overscroll-behavior: contain; }
#sheet { display: grid; position: relative; user-select: none; min-width: max-content; }
#sheet:focus { outline: none; }
#sheet:focus-visible { outline: 2px solid var(--acc); outline-offset: -2px; }
.rhspacer { min-height: 0; }

.corner {
  position: sticky; left: 0; top: 0; z-index: 6;
  background: var(--panel); border-right: 1px solid var(--line);
  border-bottom: 1px solid var(--line); padding: 0 12px;
  display: flex; flex-direction: column; justify-content: center; gap: 0;
}
.corner-day { color: var(--now); font-weight: 700; font-size: var(--fs-sm); }
.homebadge {
  font-size: var(--fs-2xs); font-weight: 700; letter-spacing: .04em; color: var(--on-acc);
  background: var(--acc); border-radius: 99px; padding: 1px 8px;
  vertical-align: 2px; white-space: nowrap;
}

.rdate {
  position: sticky; top: 0; z-index: 6; background: var(--panel);
  border-bottom: 1px solid var(--line);
  display: flex; align-items: center; padding: 0 10px;
  min-height: var(--ruler-dates-h);
  font-size: var(--fs-xs); font-weight: 700; letter-spacing: .05em; text-transform: uppercase; color: var(--dim);
  font-variant-numeric: tabular-nums;
}
.rdate.we { color: var(--weekend); }

.rcell {
  position: sticky; top: var(--ruler-dates-h); z-index: 5; background: var(--panel);
  border-bottom: 1px solid var(--line); border-right: 1px solid var(--line);
  min-height: var(--ruler-h);
  display: flex; align-items: center; justify-content: center;
  font-size: var(--fs-sm); color: var(--txt); font-weight: 600;
  font-variant-numeric: tabular-nums;
}
.rcell.nowcol { box-shadow: inset 0 -2px 0 var(--now); }
.nowpill {
  position: absolute; top: 1px; font-size: var(--fs-2xs); font-weight: 800; letter-spacing: .08em;
  text-transform: uppercase; color: var(--now);
}

.gutter {
  position: sticky; left: 0; z-index: 4; background: var(--panel2);
  border-right: 1px solid var(--line); border-bottom: 1px solid var(--line);
  padding: 10px 12px; min-height: 76px;
  display: flex; flex-direction: column; justify-content: center; gap: 1px;
}
.grow { display: flex; align-items: flex-start; justify-content: space-between; gap: 8px; }
.gname { font-weight: 700; font-size: var(--fs-md); line-height: 1.3; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.gbtns { display: flex; gap: 4px; opacity: 0; transition: opacity 150ms var(--ease-out); }
.gutter:hover .gbtns, .gutter:focus-within .gbtns { opacity: 1; }
@media (hover: none) { .gbtns { opacity: 1; } }
.gbtns button {
  min-width: 32px; min-height: 32px; padding: 2px 6px; border-radius: 7px;
  background: transparent; color: var(--dim); font-size: var(--fs-base); line-height: 1;
}
.gbtns button:hover { color: var(--txt); background: var(--acc-dim); }
.gsub { color: var(--dim); font-size: var(--fs-xs); }
.gtitle { display: flex; align-items: baseline; gap: 8px; flex: 1; min-width: 0; }
.gclock { font-size: var(--fs-lg); font-weight: 750; letter-spacing: -.01em; margin-left: auto; padding-left: 8px; }
.gmeta { font-size: var(--fs-sm); color: var(--dim); font-weight: 600; }
.gmeta.dim { color: var(--dim); font-weight: 400; }

.cell {
  border-right: 1px solid var(--line); border-bottom: 1px solid var(--line);
  min-height: 76px; display: flex; align-items: center; justify-content: center;
  color: var(--dim); cursor: crosshair; font-size: var(--fs-sm); font-weight: 600;
  font-variant-numeric: tabular-nums;
  transition: background-color 150ms var(--ease-out), box-shadow 150ms var(--ease-out);
}
.cell.daystart { box-shadow: inset 1px 0 0 var(--tick); }
.cell.we { background-color: var(--we-tint); }
.cell.cursor { box-shadow: inset 0 0 0 2px var(--acc); position: relative; z-index: 2; }
/* sun bands: luminance steps night < astro < nautical < civil < golden < day */
.b-night { background-color: var(--band-night-bg); color: var(--band-night-ink); }
.b-astro { background-color: var(--band-astro-bg); color: var(--band-astro-ink); }
.b-nautical { background-color: var(--band-nautical-bg); color: var(--band-nautical-ink); }
.b-civil { background-color: var(--band-civil-bg); color: var(--band-civil-ink); }
.b-golden { background-color: var(--band-golden-bg); color: var(--band-golden-ink); font-weight: 700; }
.b-day { background-color: var(--band-day-bg); color: var(--band-day-ink); font-weight: 700; }
.sunmark { color: var(--sun); font-size: var(--fs-2xs); font-weight: 800; margin-left: 3px; }

/* free-range selection overlays (positioned by JS, minute precision) */
.selrange {
  position: absolute; z-index: 3; pointer-events: none;
  background-color: var(--sel-fill);
  box-shadow: inset 0 0 0 1px var(--sel-edge);
  border-radius: 2px;
}
.selrange[hidden] { display: none; }

/* overlays */
#colhl {
  position: absolute; top: 0; bottom: 0; width: 48px; z-index: 3;
  background: var(--colhl);
  border-left: 1px solid var(--colhl-edge);
  border-right: 1px solid var(--colhl-edge);
  pointer-events: none;
}
#nowline {
  position: absolute; top: 0; bottom: 0; width: 1px; z-index: 3;
  background: var(--now); opacity: .85; pointer-events: none;
}
#tip {
  position: fixed; z-index: 60; background: var(--float); border: 1px solid var(--line);
  border-left: 3px solid var(--acc);
  border-radius: 10px; padding: 10px 12px; font-size: var(--fs-sm); min-width: 210px;
  box-shadow: 0 16px 44px var(--shadow); pointer-events: none;
  opacity: 1; transform: translateY(0);
  transition: opacity 125ms var(--ease-out), transform 125ms var(--ease-out);
  transform-origin: top left;
}
#tip[hidden] { display: none; }
#tip .tiphead { color: var(--txt); font-weight: 700; margin-bottom: 5px; font-size: var(--fs-sm); }
#tip .tiprow { display: flex; justify-content: space-between; gap: 12px; padding: 1px 0; }
#tip .tiprow b { font-weight: 600; color: var(--txt); white-space: nowrap; }
#tip .tiprow span { color: var(--dim); text-align: right; }
#tip .tiprow.home b { color: var(--acc); }

/* ---------- selection bar ---------- */
#selbar {
  position: fixed; left: 50%; transform: translateX(-50%) translateY(0); bottom: 48px; z-index: 55;
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
  background: var(--float); border: 1px solid var(--float-edge); border-radius: 14px;
  padding: 10px 12px; box-shadow: 0 18px 48px var(--shadow); max-width: 94vw;
  opacity: 1;
  transition: opacity 200ms var(--ease-out), transform 200ms var(--ease-out);
}
#selbar[hidden] { display: none; }
.selsummary { font-weight: 700; color: var(--txt); }
.seltitle {
  background: var(--panel2); border: 1px solid var(--line); color: var(--txt);
  border-radius: 8px; padding: 7px 10px; width: 170px; font: inherit;
}
.seltitle:focus { outline: none; border-color: var(--acc); }

/* ---------- footer / toast ---------- */
footer {
  display: flex; align-items: center; gap: 8px 16px; flex-wrap: wrap;
  padding: 8px 16px; border-top: 1px solid var(--line); background: var(--panel);
  font-size: var(--fs-xs); color: var(--dim);
}
footer .legend { display: inline-flex; align-items: center; gap: 4px; }
.hint-touch { display: none; }
.sw { display: inline-block; width: 11px; height: 11px; border-radius: 3px; margin-right: 5px; vertical-align: -1px; }
footer kbd {
  font: var(--fs-xs) ui-monospace, SFMono-Regular, Menlo, monospace;
  border: 1px solid var(--line); border-bottom-width: 2px; border-radius: 5px; padding: 0 5px;
  background: var(--panel2); color: var(--txt);
}
#toast {
  position: fixed; left: 50%; bottom: 116px; transform: translateX(-50%) translateY(6px);
  background: var(--float); border: 1px solid var(--float-edge); border-radius: 99px;
  padding: 8px 18px; opacity: 0; transition: opacity 200ms var(--ease-out), transform 200ms var(--ease-out);
  z-index: 70; pointer-events: none; font-size: var(--fs-sm); color: var(--txt);
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* hover only where hover exists */
@media (hover: hover) and (pointer: fine) {
  .cell:hover { background-color: var(--colhl); }
}
/* touch: larger targets */
@media (pointer: coarse) {
  button { min-height: 44px; }
  .seg button { min-height: 40px; }
  .gbtns button { min-width: 44px; min-height: 44px; }
}
/* no hover: show device-appropriate hints */
@media (hover: none), (pointer: coarse) {
  .hint-desktop { display: none; }
  .hint-touch { display: inline; }
}

/* narrow viewports: compact gutter, stacked header (gutter px width set in JS) */
@media (max-width: 640px) {
  header { padding: 8px 12px; gap: 8px; }
  .brand small { display: none; }
  .brand { order: 1; }
  .controls { order: 2; margin-left: auto; }
  .searchwrap { order: 3; flex: 1 1 100%; }
  #search { width: 100%; font-size: 16px; min-height: 40px; }
  .seltitle { font-size: 16px; width: 100%; }
  #selbar { bottom: 40px; }
}
/* narrow gutter content */
#sheet.narrow .gutter { padding: 8px 10px; }
#sheet.narrow .grow { flex-direction: column; align-items: stretch; gap: 4px; }
#sheet.narrow .gtitle { flex-direction: column; align-items: flex-start; gap: 0; }
#sheet.narrow .gname { font-size: var(--fs-base); max-width: 100%; }
#sheet.narrow .gclock { margin-left: 0; padding-left: 0; font-size: var(--fs-md); }
#sheet.narrow .gbtns { opacity: 1; }
#sheet.narrow .gsub, #sheet.narrow .gmeta { display: none; }
#sheet.narrow .corner-day { font-size: var(--fs-xs); }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition-duration: 0ms !important; }
  .skip { transition: none; }
}
