/* ---------- theme ---------- */
:root {
  --accent: #e0533d;
  --bg: #0e0f12;
  --fg: #f2f3f5;
  --dim: #8b9099;
  --face: #16181d;
  --line: #2a2e36;
  --panel-bg: #14161a;
  --panel-line: #282c34;
  --field: #1c1f26;
  color-scheme: dark;

  /* Sky palette. Night is always the darkest tile and midday the lightest, in
     both themes, so a wall of clocks reads asleep/awake at a glance. */
  --sky-night-bg: #060d1a; --sky-night-face: #101c33; --sky-night-line: #24344f;
  --sky-night-fg: #e7edfa;  --sky-night-dim: #8695b8;
  --sky-dawn-bg: #36262f;  --sky-dawn-face: #453240;  --sky-dawn-line: #5e4455;
  --sky-dawn-fg: #f9f0ea;   --sky-dawn-dim: #c2a59f;
  --sky-day-bg: #2c4675;   --sky-day-face: #3d5c93;  --sky-day-line: #587cba;
  --sky-day-fg: #f4f8ff;    --sky-day-dim: #b3c6e6;
  --sky-dusk-bg: #3a2229;  --sky-dusk-face: #4b2f38;  --sky-dusk-line: #66404c;
  --sky-dusk-fg: #faefe9;   --sky-dusk-dim: #c8a49f;
}
:root[data-theme="light"] {
  --bg: #f4f5f7;
  --fg: #16181d;
  --dim: #6b7280;
  --face: #ffffff;
  --line: #d8dbe0;
  --panel-bg: #ffffff;
  --panel-line: #e2e5ea;
  --field: #f0f2f5;
  color-scheme: light;

    --sky-night-bg: #121b2f; --sky-night-face: #1c2743; --sky-night-line: #31405f;
    --sky-night-fg: #eef2fa;  --sky-night-dim: #93a1c0;
    --sky-dawn-bg: #fbe0cd;  --sky-dawn-face: #fff4ec;  --sky-dawn-line: #e7c2a8;
    --sky-dawn-fg: #33232b;   --sky-dawn-dim: #8a6f6a;
    --sky-day-bg: #e3eefb;   --sky-day-face: #ffffff;   --sky-day-line: #bed3ee;
    --sky-day-fg: #142031;    --sky-day-dim: #5d7392;
    --sky-dusk-bg: #f8dac6;  --sky-dusk-face: #fff0e6;  --sky-dusk-line: #e8bca1;
    --sky-dusk-fg: #33222a;   --sky-dusk-dim: #8b6c66;
}
@media (prefers-color-scheme: light) {
  :root[data-theme="auto"] {
    --bg: #f4f5f7;
    --fg: #16181d;
    --dim: #6b7280;
    --face: #ffffff;
    --line: #d8dbe0;
    --panel-bg: #ffffff;
    --panel-line: #e2e5ea;
    --field: #f0f2f5;
    color-scheme: light;

    --sky-night-bg: #121b2f; --sky-night-face: #1c2743; --sky-night-line: #31405f;
    --sky-night-fg: #eef2fa;  --sky-night-dim: #93a1c0;
    --sky-dawn-bg: #fbe0cd;  --sky-dawn-face: #fff4ec;  --sky-dawn-line: #e7c2a8;
    --sky-dawn-fg: #33232b;   --sky-dawn-dim: #8a6f6a;
    --sky-day-bg: #e3eefb;   --sky-day-face: #ffffff;   --sky-day-line: #bed3ee;
    --sky-day-fg: #142031;    --sky-day-dim: #5d7392;
    --sky-dusk-bg: #f8dac6;  --sky-dusk-face: #fff0e6;  --sky-dusk-line: #e8bca1;
    --sky-dusk-fg: #33222a;   --sky-dusk-dim: #8b6c66;
  }
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  overflow: hidden;
  background: var(--bg);
  color: var(--fg);
  font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}

body.idle { cursor: none; }

/* ---------- grid ---------- */
#grid {
  display: grid;
  gap: var(--gap, 12px);
  width: 100%;
  height: 100%;
  padding: var(--gap, 12px);
  grid-template-columns: repeat(var(--subcols, 2), 1fr);
  grid-template-rows: repeat(var(--rows, 1), 1fr);
}

/* layout() assigns the real row and column; this is the pre-layout default. */
.clock { grid-column: span 2; }

/* With the panel open the clocks shrink aside it, so edits are visible live. */
@media (min-width: 481px) {
  body.panel-open #grid { width: calc(100% - min(420px, 100vw)); }
}

#empty[hidden] { display: none; }

#empty {
  position: fixed;
  inset: 0;
  display: grid;
  place-content: center;
  margin: 0;
  color: var(--dim);
  font-size: 1.1rem;
}

/* ---------- day / night ---------- */
/* Every clock paints from --cell-*, which is the theme by default and the sky
   palette once a phase class is on. */
.clock {
  --cell-bg: transparent;
  --cell-face: var(--face);
  --cell-line: var(--line);
  --cell-fg: var(--fg);
  --cell-dim: var(--dim);
  background: var(--cell-bg);
  color: var(--cell-fg);
  border-radius: clamp(8px, calc(var(--ch) * 0.045), 22px);
  padding: calc(var(--ch) * 0.03) calc(var(--cw) * 0.02);
  transition: background-color 1.2s ease, color 1.2s ease;
}

.clock.night {
  --cell-bg: var(--sky-night-bg); --cell-face: var(--sky-night-face);
  --cell-line: var(--sky-night-line); --cell-fg: var(--sky-night-fg); --cell-dim: var(--sky-night-dim);
}
.clock.dawn {
  --cell-bg: var(--sky-dawn-bg); --cell-face: var(--sky-dawn-face);
  --cell-line: var(--sky-dawn-line); --cell-fg: var(--sky-dawn-fg); --cell-dim: var(--sky-dawn-dim);
}
.clock.day {
  --cell-bg: var(--sky-day-bg); --cell-face: var(--sky-day-face);
  --cell-line: var(--sky-day-line); --cell-fg: var(--sky-day-fg); --cell-dim: var(--sky-day-dim);
}
.clock.dusk {
  --cell-bg: var(--sky-dusk-bg); --cell-face: var(--sky-dusk-face);
  --cell-line: var(--sky-dusk-line); --cell-fg: var(--sky-dusk-fg); --cell-dim: var(--sky-dusk-dim);
}

.sub .phase {
  width: 1.2em;
  height: 1.2em;
  vertical-align: -0.25em;
  margin-right: 0.15em;
}
.sub .phase .disc { fill: currentColor; }
.sub .phase .ray { stroke: currentColor; stroke-width: 1.4; stroke-linecap: round; }
.clock.day .phase { color: #f6b73c; }
.clock.dawn .phase, .clock.dusk .phase { color: #ef8b4e; }
.clock.night .phase { color: #c3d2f2; }

.clock {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  gap: calc(var(--ch) * 0.02);
}

.clock .name {
  font-size: min(calc(var(--cw) * 0.11), calc(var(--ch) * 0.085), 2.6rem);
  font-weight: 600;
  letter-spacing: 0.02em;
  line-height: 1.15;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
}

.clock .sub {
  color: var(--cell-dim);
  font-size: min(calc(var(--cw) * 0.055), calc(var(--ch) * 0.055), 1.2rem);
  line-height: 1.3;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
}

.clock .sub .offset { color: var(--accent); }

/* ---------- analog ---------- */
/* The SVG fits itself to whatever box is left over, so the face can never
   push the label out of the cell. */
.clock.analog .face {
  flex: 1 1 0;
  display: block;
  width: 100%;
  height: 100%;
  min-height: 0;
  min-width: 0;
}

.face .dial { fill: var(--cell-face); stroke: var(--cell-line); stroke-width: 1; }
.face .tick { stroke: var(--cell-dim); stroke-linecap: round; }
.face .tick.minor { stroke-width: 0.7; opacity: 0.55; }
.face .tick.major { stroke-width: 2.2; opacity: 1; stroke: var(--cell-fg); }
.face .num { fill: var(--cell-fg); font-size: 8px; font-weight: 600; opacity: 0.85;
             text-anchor: middle; dominant-baseline: central;
             font-family: ui-sans-serif, system-ui, sans-serif; }
.face .hand { stroke: var(--cell-fg); stroke-linecap: round; }
.face .hand.hour { stroke-width: 5; }
.face .hand.minute { stroke-width: 3.2; }
.face .hand.second { stroke: var(--accent); stroke-width: 1.2; }
.face .cap { fill: var(--cell-fg); }
.face .cap.pin { fill: var(--accent); }

/* ---------- digital ---------- */
.clock.digital .time {
  font-variant-numeric: tabular-nums;
  font-feature-settings: "tnum" 1;
  font-weight: 250;
  letter-spacing: -0.02em;
  line-height: 1;
  white-space: nowrap;
  font-size: min(calc(var(--cw) * var(--digital-fit, 0.28)), calc(var(--ch) * 0.52));
}
.clock.digital .time .sec { color: var(--cell-dim); font-size: 0.55em; }
.clock.digital .time .mer { color: var(--cell-dim); font-size: 0.4em; margin-left: 0.25em; }

/* ---------- toolbar ---------- */
.bar {
  position: fixed;
  right: max(12px, env(safe-area-inset-right));
  bottom: max(12px, env(safe-area-inset-bottom));
  display: flex;
  gap: 8px;
  transition: opacity 0.35s ease;
  z-index: 5;
}
body.idle .bar { opacity: 0; pointer-events: none; }

.bar button {
  width: 42px;
  height: 42px;
  font-size: 18px;
  line-height: 1;
  border-radius: 999px;
  border: 1px solid var(--panel-line);
  background: var(--panel-bg);
  color: var(--fg);
  cursor: pointer;
  opacity: 0.85;
}
.bar button:hover { opacity: 1; border-color: var(--accent); }

/* ---------- panel ---------- */
.panel {
  position: fixed;
  top: 0;
  right: 0;
  z-index: 10;
  width: min(420px, 100vw);
  height: 100%;
  overflow-y: auto;
  padding: 0 20px 24px;
  background: var(--panel-bg);
  border-left: 1px solid var(--panel-line);
  box-shadow: -20px 0 50px rgba(0, 0, 0, 0.35);
  font-size: 14px;
}
.panel[hidden] { display: none; }

.panel-head {
  position: sticky;
  top: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 0 12px;
  background: var(--panel-bg);
  border-bottom: 1px solid var(--panel-line);
}
.panel-head h1 { margin: 0; font-size: 16px; letter-spacing: 0.04em; text-transform: uppercase; color: var(--dim); }

.panel section { padding: 18px 0; border-bottom: 1px solid var(--panel-line); }
.panel h2 { margin: 0 0 10px; font-size: 12px; letter-spacing: 0.08em; text-transform: uppercase; color: var(--dim); font-weight: 600; }
.panel-foot { padding-top: 16px; }

.hint { margin: 8px 0 0; color: var(--dim); font-size: 12px; line-height: 1.5; }
.error { margin: 8px 0 0; color: var(--accent); font-size: 12px; }

label { display: block; font-size: 12px; color: var(--dim); }
label.check { display: flex; align-items: center; gap: 8px; color: var(--fg); font-size: 13px; cursor: pointer; }

input[type="text"], input:not([type]), select, input[list] {
  width: 100%;
  margin-top: 4px;
  padding: 7px 9px;
  border: 1px solid var(--panel-line);
  border-radius: 6px;
  background: var(--field);
  color: var(--fg);
  font: inherit;
  font-size: 13px;
}
input:focus-visible, select:focus-visible, button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}
input[type="checkbox"] { accent-color: var(--accent); width: 15px; height: 15px; margin: 0; }
input[type="color"] { width: 100%; height: 30px; margin-top: 4px; padding: 2px; border: 1px solid var(--panel-line);
                      border-radius: 6px; background: var(--field); }

.add { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; align-items: end; }
.add label:first-of-type { grid-column: 1 / -1; }
.add button { grid-column: 1 / -1; }

.opts { display: grid; gap: 10px; }

button {
  padding: 8px 12px;
  border: 1px solid var(--panel-line);
  border-radius: 6px;
  background: var(--field);
  color: var(--fg);
  font: inherit;
  font-size: 13px;
  cursor: pointer;
}
button:hover { border-color: var(--accent); }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
button.primary:hover { filter: brightness(1.1); }
button.danger { color: var(--accent); }
button.icon { border: 0; background: none; font-size: 22px; padding: 0 6px; line-height: 1; }

.row { display: flex; flex-wrap: wrap; gap: 8px; }

.clock-tools {
  display: grid;
  gap: 10px;
  margin-bottom: 12px;
}
.clock-tools .row button { flex: 1; }

#order-hint[hidden] { display: none; }
#order-hint { margin: 0 0 10px; }

.clock-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.clock-list li {
  display: grid;
  grid-template-columns: 1fr auto auto auto auto;
  gap: 6px;
  align-items: center;
  padding: 8px;
  border: 1px solid var(--panel-line);
  border-radius: 8px;
  background: var(--field);
}
.clock-list .zone { grid-column: 1 / -1; color: var(--dim); font-size: 11px; }
.clock-list input, .clock-list select { margin: 0; background: var(--panel-bg); }
.clock-list button { padding: 6px 8px; min-width: 30px; }
.clock-list button:disabled { opacity: 0.3; cursor: default; border-color: var(--panel-line); }

@media (max-width: 480px) {
  .panel { width: 100vw; border-left: 0; }
}
