:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #eef0f3;
  --text: #15181d;
  --muted: #5d6673;
  --border: #dde1e6;
  --accent: #0f766e;
  --accent-text: #ffffff;
  --accent-soft: #d5f0ec;
  --create: #15803d;
  --create-soft: #dcf5e3;
  --update: #b45309;
  --update-soft: #fdf0d9;
  --same: #64748b;
  --same-soft: #e8ecf1;
  --danger: #b91c1c;
  --danger-soft: #fde2e2;
  --del-bg: #fde8e8;
  --add-bg: #e3f6e8;
  --radius: 14px;
  --nav-h: 64px;
  --mono: ui-monospace, "SF Mono", "Cascadia Mono", Menlo, Consolas, monospace;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1116;
    --surface: #171b22;
    --surface-2: #1f252e;
    --text: #e7eaee;
    --muted: #9aa4b1;
    --border: #2b323c;
    --accent: #2dd4bf;
    --accent-text: #04201d;
    --accent-soft: #123a36;
    --create: #4ade80;
    --create-soft: #143222;
    --update: #fbbf24;
    --update-soft: #3a2c10;
    --same: #94a3b8;
    --same-soft: #232a34;
    --danger: #f87171;
    --danger-soft: #3b1717;
    --del-bg: #3a1a1c;
    --add-bg: #13301e;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
/* No zooming at all, so it feels like an app: no double-tap zoom, no pinch-zoom. Scrolling
   (including sideways in the plan editor and diffs) still works. Also see the viewport tag
   (maximum-scale=1, user-scalable=no) and the gesture* handler in app.js, for iOS versions
   that ignore these. */
html, body, button, a, label, summary, input, textarea, select, [role=button] { touch-action: pan-x pan-y; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  padding-bottom: calc(var(--nav-h) + env(safe-area-inset-bottom) + 16px);
}
h1, h2, h3 { margin: 0; line-height: 1.2; }
h1 { font-size: 1.1rem; font-weight: 700; letter-spacing: .01em; }
h2 { font-size: 1.15rem; }
h3 { font-size: 1rem; }
code { font-family: var(--mono); font-size: .9em; background: var(--surface-2); padding: 1px 5px; border-radius: 5px; }
[hidden] { display: none !important; }

/* ---------- header ---------- */
.top {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 10px;
  padding: calc(env(safe-area-inset-top) + 10px) 16px 10px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}
.who { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---------- connection status ---------- */
.top { flex-wrap: wrap; }
.conn {
  flex: 1; min-width: 0; display: flex; justify-content: flex-end; align-items: center; gap: 8px;
  appearance: none; background: none; border: 0; padding: 8px 0; cursor: pointer;
  color: var(--muted); font: inherit; font-size: .85rem; -webkit-tap-highlight-color: transparent;
}
.dot { width: 10px; height: 10px; border-radius: 50%; flex-shrink: 0; background: var(--same); }
.conn[data-state="ok"] .dot { background: var(--create); }
.conn[data-state="warn"] .dot { background: var(--update); }
.conn[data-state="down"] .dot { background: var(--danger); }
.conn[data-state="down"] { color: var(--danger); }
.conn[data-state="checking"] .dot { animation: pulse 1s ease-in-out infinite alternate; }
@keyframes pulse { from { opacity: .25; } to { opacity: 1; } }
.conn-banner {
  flex-basis: 100%; appearance: none; border: 0; cursor: pointer; text-align: left;
  font: inherit; font-size: .85rem; font-weight: 600; padding: 8px 12px; border-radius: 10px; margin-top: 2px;
  background: var(--danger-soft); color: var(--danger);
}
.conn-banner.warn { background: var(--update-soft); color: var(--update); }
.icon-btn {
  width: 40px; height: 40px; border-radius: 12px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); display: grid; place-items: center; cursor: pointer;
}
.icon-btn svg { width: 22px; height: 22px; fill: currentColor; }

main { max-width: 760px; margin: 0 auto; padding: 12px 16px; }

/* ---------- cards ---------- */
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px; margin-bottom: 12px;
}
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 8px; }
.hint { color: var(--muted); font-size: .9rem; margin: 6px 0 12px; }
.hint:empty { display: none; }
.row { display: flex; gap: 10px; align-items: flex-end; }
.grow { flex: 1; }

/* ---------- inputs ---------- */
.field { display: flex; flex-direction: column; gap: 6px; font-size: .85rem; color: var(--muted); font-weight: 500; margin-bottom: 12px; }
.row .field { margin-bottom: 0; }
.field small { font-weight: 400; }
input[type=number], input[type=text], input[type=password], textarea {
  width: 100%; font: inherit; font-size: 16px; /* 16px stops iOS zoom-on-focus */
  color: var(--text); background: var(--surface-2);
  border: 1px solid var(--border); border-radius: 10px; padding: 11px 12px;
}
input:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
textarea { resize: vertical; min-height: 120px; }
textarea.mono { font-family: var(--mono); font-size: 13.5px; line-height: 1.5; white-space: pre; overflow-wrap: normal; overflow-x: auto; }
/* iOS zooms into any field under 16px when it's focused. Keep the compact size for mouse
   users; on touch screens the plan editor uses 16px so tapping into it doesn't zoom. */
@media (pointer: coarse) { textarea.mono { font-size: 16px; } }
.pw { display: flex; gap: 8px; }
.pw input { flex: 1; }

/* ---------- buttons ---------- */
.btn {
  appearance: none; border: 1px solid var(--border); background: var(--surface-2); color: var(--text);
  font: inherit; font-weight: 600; font-size: .95rem; min-height: 44px; padding: 0 16px; border-radius: 11px;
  cursor: pointer; display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  -webkit-tap-highlight-color: transparent; transition: transform .06s, opacity .15s;
}
.btn:active { transform: scale(.97); }
.btn:disabled { opacity: .5; cursor: default; transform: none; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--danger); }
.btn.small { min-height: 34px; padding: 0 12px; font-size: .85rem; }
.btn.block { width: 100%; }
.btn.busy { pointer-events: none; opacity: .7; }
.btn.busy::after {
  content: ""; width: 14px; height: 14px; border-radius: 50%;
  border: 2px solid currentColor; border-right-color: transparent; animation: spin .7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.file-btn { position: relative; overflow: hidden; }
.file-btn input { position: absolute; inset: 0; opacity: 0; cursor: pointer; }
.actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; align-items: center; }
.actions .btn { flex: 1 1 auto; }

/* ---------- push preview ---------- */
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip { font-size: .85rem; font-weight: 700; padding: 6px 12px; border-radius: 999px; }
.chip.create { background: var(--create-soft); color: var(--create); }
.chip.update { background: var(--update-soft); color: var(--update); }
.chip.same { background: var(--same-soft); color: var(--same); }
.chip.orphan { background: var(--surface-2); color: var(--muted); }
.chip.failed, .chip.delete { background: var(--danger-soft); color: var(--danger); }
.chip.replace { background: var(--update-soft); color: var(--update); }

.change {
  background: var(--surface); border: 1px solid var(--border); border-left: 4px solid var(--same);
  border-radius: var(--radius); padding: 12px 14px; margin-bottom: 10px;
}
.change.create { border-left-color: var(--create); }
.change.update, .change.replace { border-left-color: var(--update); }
.change.delete { border-left-color: var(--danger); }
.change.delete .name { text-decoration: line-through; color: var(--muted); }
.change.ok { border-left-color: var(--create); }
.change.fail { border-left-color: var(--danger); }
.change-head { display: flex; gap: 10px; align-items: baseline; flex-wrap: wrap; }
.tag { font-size: .7rem; font-weight: 800; letter-spacing: .06em; text-transform: uppercase; padding: 2px 8px; border-radius: 6px; }
.tag.create { background: var(--create-soft); color: var(--create); }
.tag.update { background: var(--update-soft); color: var(--update); }
.tag.same { background: var(--same-soft); color: var(--same); }
.tag.replace { background: var(--update-soft); color: var(--update); }
.tag.delete { background: var(--danger-soft); color: var(--danger); }
.change .date { color: var(--muted); font-size: .85rem; font-variant-numeric: tabular-nums; }
.change .name { font-weight: 600; }
.change .meta { color: var(--muted); font-size: .85rem; margin-top: 4px; }
.change .err { color: var(--danger); font-size: .85rem; margin-top: 4px; }
.diff {
  margin: 10px 0 0; padding: 8px 0; background: var(--surface-2); border-radius: 8px;
  font: 12.5px/1.5 var(--mono); overflow-x: auto; white-space: pre;
}
.diff span { display: block; padding: 0 10px; min-width: max-content; }
.diff .add { background: var(--add-bg); color: var(--create); }
.diff .del { background: var(--del-bg); color: var(--danger); }
.diff .hunk { color: var(--muted); }
.desc { margin: 10px 0 0; font: 12.5px/1.5 var(--mono); white-space: pre-wrap; color: var(--muted); }
details.group { margin-bottom: 10px; }
details.group > summary {
  cursor: pointer; list-style: none; padding: 12px 14px; border-radius: var(--radius);
  background: var(--surface); border: 1px solid var(--border); color: var(--muted); font-weight: 600; font-size: .9rem;
}
details.group > summary::-webkit-details-marker { display: none; }
details.group > summary::before { content: "▸ "; }
details.group[open] > summary::before { content: "▾ "; }
details.group[open] > summary { margin-bottom: 10px; }
details > summary { cursor: pointer; color: var(--muted); font-size: .9rem; margin-top: 12px; }
details textarea { margin-top: 8px; }

.error {
  background: var(--danger-soft); color: var(--danger); border-radius: 10px; padding: 10px 12px;
  margin-top: 10px; font-size: .9rem; font-weight: 500;
}

.sticky-bar {
  position: sticky; bottom: calc(var(--nav-h) + env(safe-area-inset-bottom) + 8px); z-index: 5;
  padding: 8px 0;
}
.sticky-bar .btn { box-shadow: 0 6px 20px rgba(0,0,0,.18); min-height: 52px; font-size: 1rem; }

/* ---------- lists ---------- */
.list { list-style: none; margin: 0; padding: 0; }
.list li {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 12px 14px; margin-bottom: 8px;
}
.run-top { display: flex; justify-content: space-between; gap: 10px; align-items: baseline; }
.run-top .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.run-top .date { color: var(--muted); font-size: .85rem; flex-shrink: 0; font-variant-numeric: tabular-nums; }
.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; margin-top: 8px; }
.stat { background: var(--surface-2); border-radius: 8px; padding: 6px 8px; min-width: 0; }
.stat b { display: block; font-size: .95rem; font-variant-numeric: tabular-nums; }
.stat small { color: var(--muted); font-size: .72rem; }
.hist-head { display: flex; justify-content: space-between; gap: 10px; align-items: center; cursor: pointer; }
.hist-head .when { font-weight: 600; }
.hist-head .rng { color: var(--muted); font-size: .85rem; }
.hist-body { margin-top: 10px; }
.hist-body textarea { margin-top: 8px; }
.empty { color: var(--muted); text-align: center; padding: 24px 8px; }

/* ---------- bottom nav ---------- */
.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  display: grid; grid-template-columns: repeat(4, 1fr);
  height: calc(var(--nav-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
  border-top: 1px solid var(--border);
}
.tabs button {
  appearance: none; border: 0; background: none; color: var(--muted); font: inherit; font-size: .72rem; font-weight: 600;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px; cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.tabs svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.tabs button.active { color: var(--accent); }

/* ---------- dialog ---------- */
dialog {
  width: min(520px, calc(100% - 24px)); border: 1px solid var(--border); border-radius: 18px;
  background: var(--surface); color: var(--text); padding: 20px;
}
dialog::backdrop { background: rgba(0,0,0,.45); }
dialog h2 { margin-bottom: 4px; }
.ok-text { color: var(--create); }
.bad-text { color: var(--danger); }

/* ---------- toast ---------- */
.toast {
  position: fixed; left: 50%; transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  bottom: calc(var(--nav-h) + env(safe-area-inset-bottom) + 16px); z-index: 50;
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-size: .9rem; font-weight: 600;
  max-width: calc(100% - 32px); transition: opacity .2s, transform .2s;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.bad { background: var(--danger); color: #fff; }

/* ---------- wider screens ---------- */
@media (min-width: 820px) {
  .tabs { position: sticky; top: 0; height: auto; max-width: 760px; margin: 0 auto; border: 0; background: none; backdrop-filter: none;
    display: flex; gap: 4px; padding: 0 16px; }
  .tabs button { flex-direction: row; font-size: .9rem; padding: 10px 14px; border-radius: 10px; }
  .tabs button.active { background: var(--accent-soft); }
  .tabs svg { width: 18px; height: 18px; }
  body { padding-bottom: 32px; display: flex; flex-direction: column; }
  .top { order: 0; } .tabs { order: 1; margin-top: 10px; } main { order: 2; width: 100%; }
  .sticky-bar { bottom: 12px; }
  .toast { bottom: 24px; }
}

/* diff gutter */
.diff i { display: inline-block; width: 1.4em; font-style: normal; font-weight: 700; user-select: none; }
.diff .ctx { color: var(--muted); }
.diff .gap { color: var(--muted); text-align: center; padding: 0; line-height: 1.1; }

/* build stamp */
.brand { display: flex; flex-direction: column; min-width: 0; }
.build {
  font: 11px/1.3 var(--mono); color: var(--muted); white-space: nowrap;
  appearance: none; background: none; border: 0; padding: 2px 0; margin: 0; text-align: left; cursor: pointer;
  text-decoration: underline dotted; text-underline-offset: 3px; -webkit-tap-highlight-color: transparent;
}
.build:empty { display: none; }

/* changelog sheet */
#changes { max-height: min(80vh, 640px); display: flex; flex-direction: column; }
#changes:not([open]) { display: none; }
.changelog { list-style: none; margin: 0; padding: 0; overflow-y: auto; border-top: 1px solid var(--border); flex: 1; }
.changelog li { padding: 12px 0; border-bottom: 1px solid var(--border); }
.cl-head { display: flex; gap: 8px; align-items: baseline; flex-wrap: wrap; font-size: .8rem; color: var(--muted); }
.cl-tag { background: var(--accent-soft); color: var(--accent); border-radius: 6px; padding: 1px 7px; font-weight: 700; font-size: .72rem; }
.cl-subject { font-weight: 600; margin-top: 4px; }
.cl-body { margin: 6px 0 0; font: 12.5px/1.5 var(--mono); white-space: pre-wrap; color: var(--muted); }
.changelog details > summary { margin-top: 6px; font-size: .82rem; }
.conn-banner.update { background: var(--accent-soft); color: var(--accent); }

/* status sheet */
.status-head { display: flex; gap: 12px; align-items: center; margin-bottom: 8px; }
.status-head .hint { margin: 2px 0 0; }
.dot.big { width: 16px; height: 16px; }
#status[data-state="ok"] .dot.big { background: var(--create); }
#status[data-state="warn"] .dot.big { background: var(--update); }
#status[data-state="down"] .dot.big { background: var(--danger); }
#status[data-state="checking"] .dot.big { animation: pulse 1s ease-in-out infinite alternate; }
.status-rows { list-style: none; margin: 0; padding: 0; border-top: 1px solid var(--border); }
.status-rows li { display: grid; grid-template-columns: 22px 1fr; gap: 2px 10px; padding: 12px 0; border-bottom: 1px solid var(--border); }
.status-rows .mark { grid-row: span 2; font-weight: 800; text-align: center; line-height: 1.4; }
.status-rows .mark.ok { color: var(--create); }
.status-rows .mark.warn { color: var(--update); }
.status-rows .mark.bad { color: var(--danger); }
.status-rows .mark.unknown { color: var(--muted); }
.status-rows .label { font-weight: 600; }
.status-rows .detail { color: var(--muted); font-size: .88rem; }
.status-rows .detail .btn { margin-top: 8px; }
.hint.small { font-size: .8rem; }

/* replace / keep-both choice */
.mode { margin-top: 14px; }
.mode-label { font-size: .85rem; color: var(--muted); font-weight: 500; margin-bottom: 6px; }
.seg { display: grid; grid-template-columns: 1fr 1fr; background: var(--surface-2); border: 1px solid var(--border); border-radius: 11px; padding: 3px; gap: 3px; }
.seg button {
  appearance: none; border: 0; background: none; color: var(--muted); font: inherit; font-weight: 600; font-size: .92rem;
  min-height: 38px; border-radius: 8px; cursor: pointer; -webkit-tap-highlight-color: transparent;
}
.seg button[aria-checked="true"] { background: var(--surface); color: var(--text); box-shadow: 0 1px 3px rgba(0,0,0,.15); }
.mode .hint { margin: 6px 0 0; }
.day-toggle { margin-top: 10px; }
