:root {
  --bg: #f6f5f2;
  --panel: #ffffff;
  --sidebar: #efede8;
  --text: #1f2328;
  --muted: #6b6f76;
  --line: #e4e1da;
  --accent: #2f6fdb;
  --accent-soft: #e5edfb;
  --done: #8b8f96;
  --danger: #c4352b;
  --star: #d93025;
  --ok: #2f9e5b;
  --warn: #c98a12;
  --shadow: 0 8px 24px rgb(0 0 0 / 0.12);
  --radius: 10px;
  --row-h: 52px;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #15171a;
    --panel: #1c1f23;
    --sidebar: #181a1e;
    --text: #e8e9eb;
    --muted: #9298a0;
    --line: #2c3036;
    --accent: #6b9cf0;
    --accent-soft: #24324a;
    --done: #7b8088;
    --danger: #f0776e;
    --star: #ff6b61;
    --ok: #5cc68a;
    --warn: #e2b04a;
    --shadow: 0 8px 24px rgb(0 0 0 / 0.5);
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

#app { height: 100%; }

button, input, textarea, select { font: inherit; color: inherit; }
a { color: inherit; text-decoration: none; }

.icon { width: 20px; height: 20px; flex: none; }

/* ---------- layout ---------- */

.app {
  height: 100dvh;
  display: grid;
  grid-template-columns: 1fr;
}

.panel {
  min-width: 0;
  min-height: 0;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  background: var(--panel);
}

.lists-panel { background: var(--sidebar); }

/* Phones: one panel at a time, chosen by the route. */
.app[data-depth="home"] > .panel:not(.lists-panel),
.app[data-depth="list"] > .panel:not(.list-panel),
.app[data-depth="item"] > .panel:not(.item-panel),
.app[data-depth="settings"] > .panel:not(.settings-panel) { display: none; }

@media (min-width: 760px) {
  .app { grid-template-columns: 300px 1fr; }
  .app > .lists-panel { display: flex !important; border-right: 1px solid var(--line); }
  .app[data-depth="home"] > .panel.list-panel { display: flex; }
  .back { visibility: hidden; }
  .lists-panel .back, .item-panel .back { visibility: visible; }
}

@media (min-width: 1100px) {
  .app[data-depth="item"] { grid-template-columns: 300px 1fr 400px; }
  .app[data-depth="item"] > .panel.list-panel { display: flex; }
  .item-panel { border-left: 1px solid var(--line); }
  .item-panel .back { visibility: hidden; }
}

@media (max-width: 1099px) {
  .item-panel .close { display: none; }
}

/* ---------- bars ---------- */

.bar {
  display: flex;
  align-items: center;
  gap: 6px;
  min-height: 56px;
  padding: calc(8px + env(safe-area-inset-top)) max(12px, env(safe-area-inset-right)) 8px max(12px, env(safe-area-inset-left));
}
.bar h1 { font-size: 26px; margin: 0 0 0 4px; flex: 1; letter-spacing: -0.01em; }
.bar-title { flex: 1; font-weight: 600; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bar-actions { display: flex; align-items: center; gap: 4px; }

.title-input {
  flex: 1;
  min-width: 0;
  font-size: 24px;
  font-weight: 700;
  letter-spacing: -0.01em;
  border: 0;
  background: transparent;
  padding: 4px 6px;
  border-radius: 6px;
}
.title-input:focus { outline: 2px solid var(--accent); background: var(--panel); }

.icon-btn {
  display: inline-grid;
  place-items: center;
  width: 40px;
  height: 40px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  flex: none;
}
.icon-btn:hover { background: color-mix(in srgb, var(--text) 8%, transparent); color: var(--text); }
.icon-btn.subtle { width: 32px; height: 32px; opacity: 0.6; }

.sync-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 0;
  background: transparent;
  color: var(--muted);
  font-size: 13px;
  padding: 6px 8px;
  border-radius: 999px;
  cursor: pointer;
}
.sync-badge .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--ok); }
.sync-badge.busy .dot { background: var(--accent); animation: pulse 1s infinite alternate; }
.sync-badge.offline .dot { background: var(--warn); }
.sync-badge.error .dot { background: var(--danger); }
@keyframes pulse { to { opacity: 0.3; } }
.sync-badge.compact { padding: 6px; flex: none; }
@media (min-width: 760px) { .sync-badge.compact { display: none; } }

.shared-note {
  display: flex;
  align-items: center;
  gap: 6px;
  margin: -4px 0 6px;
  padding: 0 22px;
  font-size: 14px;
  color: var(--muted);
}
.shared-note .icon { width: 16px; height: 16px; }

/* ---------- scrolling content ---------- */

.scroll {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 0 max(8px, env(safe-area-inset-right)) 16px max(8px, env(safe-area-inset-left));
}

.rows { list-style: none; margin: 0; padding: 0; }

.row {
  display: flex;
  align-items: center;
  gap: 4px;
  min-height: var(--row-h);
  padding: 0 4px 0 8px;
  border-radius: var(--radius);
  background: var(--panel);
  margin-bottom: 2px;
  transition: transform 0.15s ease;
}
.lists-panel .row { background: transparent; }
.row.active { background: var(--accent-soft); }
.lists-panel .row.active { background: color-mix(in srgb, var(--text) 9%, transparent); }
.row.dragging { transition: none; box-shadow: var(--shadow); background: var(--panel); }

.item-row { border: 1px solid var(--line); margin-bottom: 6px; }
.list-panel .rows { padding-top: 4px; }

.row-main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  column-gap: 8px;
  padding: 10px 6px;
  cursor: pointer;
}
.list-row .row-main { flex-wrap: nowrap; }
.row-title { min-width: 0; overflow-wrap: anywhere; }
.list-row .row-title { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item-row .row-title { flex-basis: 100%; }
.row-meta { display: flex; gap: 10px; font-size: 13px; color: var(--muted); }
.note-flag { display: inline-flex; align-items: center; gap: 3px; }
.note-flag .icon { width: 14px; height: 14px; }
.count { color: var(--muted); font-size: 14px; min-width: 1.5em; text-align: right; }
.tag { color: var(--muted); display: inline-flex; }
.tag .icon { width: 16px; height: 16px; }

.done .row-title, .item-title-input.done { color: var(--done); text-decoration: line-through; }

.handle {
  display: grid;
  place-items: center;
  width: 36px;
  height: 44px;
  color: var(--muted);
  opacity: 0.5;
  cursor: grab;
  touch-action: none;
  flex: none;
}
.handle:active { cursor: grabbing; opacity: 1; }

.empty { color: var(--muted); text-align: center; padding: 32px 16px; }
.placeholder { justify-content: center; }

.done-toggle {
  display: flex;
  align-items: center;
  gap: 6px;
  margin: 14px 0 8px;
  padding: 6px 10px;
  border: 0;
  border-radius: 8px;
  background: color-mix(in srgb, var(--text) 7%, transparent);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}
.chev { display: inline-flex; transition: transform 0.15s; }
.chev .icon { width: 16px; height: 16px; }
.chev.open { transform: rotate(90deg); }

/* ---------- checkbox ---------- */

.check {
  flex: none;
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  padding: 0;
  border: 0;
  background: transparent;
  cursor: pointer;
}
.check svg {
  width: 22px;
  height: 22px;
  border: 2px solid var(--muted);
  border-radius: 50%;
  fill: none;
  stroke: transparent;
  stroke-width: 3;
  stroke-linecap: round;
  stroke-linejoin: round;
  transition: background 0.12s, border-color 0.12s;
}
.check:hover svg { stroke: var(--muted); }
.check.on svg { background: var(--accent); border-color: var(--accent); stroke: #fff; }
.check.small svg { width: 18px; height: 18px; stroke-width: 3.5; }

/* ---------- star ---------- */

.star {
  flex: none;
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  padding: 0;
  border: 0;
  background: transparent;
  cursor: pointer;
}
.star svg {
  width: 22px;
  height: 22px;
  fill: none;
  stroke: var(--muted);
  stroke-width: 1.8;
  stroke-linejoin: round;
  transition: fill 0.12s, stroke 0.12s;
}
.star.on svg { fill: var(--star); stroke: var(--star); }
.detail-title .star { margin-top: 2px; }

/* ---------- composer / add forms ---------- */

.composer {
  padding: 8px max(10px, env(safe-area-inset-right)) calc(10px + env(safe-area-inset-bottom)) max(10px, env(safe-area-inset-left));
  border-top: 1px solid var(--line);
  background: inherit;
}
.add {
  display: flex;
  align-items: center;
  gap: 4px;
  border-radius: var(--radius);
  background: var(--panel);
  border: 1px solid var(--line);
  padding: 0 8px;
}
.add:focus-within { border-color: var(--accent); }
.add-icon { display: grid; place-items: center; width: 28px; color: var(--accent); }
.add input { flex: 1; min-width: 0; border: 0; background: transparent; padding: 12px 4px; outline: none; }
.add-step { margin: 2px 0 20px; border-style: dashed; }

/* ---------- item detail ---------- */

.detail { padding-top: 4px; }
.detail-title { display: flex; align-items: flex-start; gap: 2px; margin-bottom: 12px; }
.item-title-input {
  flex: 1;
  border: 0;
  background: transparent;
  font-size: 22px;
  font-weight: 650;
  line-height: 1.3;
  padding: 8px 6px;
  resize: none;
  border-radius: 6px;
  overflow: hidden;
}
.item-title-input:focus { outline: 2px solid var(--accent); }

.subtask-row { min-height: 44px; padding-left: 0; }
.subtask-input { flex: 1; min-width: 0; border: 0; background: transparent; padding: 8px 4px; border-radius: 6px; }
.subtask-input:focus { outline: 2px solid var(--accent); }
.subtask-row.done .subtask-input { color: var(--done); text-decoration: line-through; }

.field { display: flex; flex-direction: column; gap: 6px; margin: 0 4px 18px; }
.field > span { font-size: 13px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }
.field input, .field select, .note-input {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--panel);
  padding: 10px 12px;
}
.note-input { min-height: 96px; resize: none; overflow: hidden; line-height: 1.45; }
.field input:focus, .field select:focus, .note-input:focus { outline: 2px solid var(--accent); border-color: transparent; }

.danger-btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  margin: 8px 4px;
  padding: 10px 14px;
  border: 1px solid color-mix(in srgb, var(--danger) 40%, transparent);
  border-radius: var(--radius);
  background: transparent;
  color: var(--danger);
  cursor: pointer;
}

/* ---------- menu ---------- */

.menu { position: relative; }
.menu-pop {
  position: absolute;
  right: 0;
  top: 44px;
  z-index: 20;
  min-width: 220px;
  padding: 6px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.menu-pop button {
  display: block;
  width: 100%;
  text-align: left;
  padding: 10px 12px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  cursor: pointer;
}
.menu-pop button:hover { background: color-mix(in srgb, var(--text) 7%, transparent); }
.menu-pop .danger { color: var(--danger); }

/* ---------- settings ---------- */

.settings-panel h2 { font-size: 15px; color: var(--muted); margin: 24px 6px 8px; }
.card { padding: 14px; border: 1px solid var(--line); border-radius: var(--radius); margin: 0 4px; }
.card p { margin: 0 0 10px; }
.devices .row { border-bottom: 1px solid var(--line); border-radius: 0; }
.devices .row-main { flex-direction: column; align-items: flex-start; cursor: default; }

.btn {
  padding: 10px 16px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--panel);
  cursor: pointer;
  font-weight: 600;
}
.btn.small { padding: 6px 10px; font-size: 14px; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; width: 100%; }
.btn:disabled { opacity: 0.6; }

.muted { color: var(--muted); }
.small { font-size: 13px; }

/* ---------- login ---------- */

.login {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 24px 16px;
}
.login-card { width: 100%; max-width: 360px; }
.login-card h1 { text-align: center; margin: 8px 0 24px; }
.login-card .field { margin: 0 0 14px; }
.login-card .muted { text-align: center; margin-top: 16px; }
.logo { display: grid; place-items: center; }
.logo svg {
  width: 56px;
  height: 56px;
  padding: 10px;
  border-radius: 16px;
  background: var(--accent);
  fill: none;
  stroke: #fff;
  stroke-width: 3;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.error { color: var(--danger); margin: 0 0 12px; }

@media (prefers-reduced-motion: reduce) {
  .row, .chev, .check svg { transition: none; }
  .sync-badge.busy .dot { animation: none; }
}
