:root {
  --bg: #f6f1e7;
  --surface: #fffdf8;
  --ink: #23201b;
  --muted: #6f675b;
  --line: #e4dccb;
  --line-strong: #cbbfa8;
  --accent: #c2491f;
  --accent-ink: #ffffff;
  --accent-soft: #f7e3d8;
  --done: #2f7d5b;
  --done-soft: #dcefe5;
  --warn: #a8321d;
  --shadow: 0 1px 2px rgba(60, 45, 20, .06), 0 6px 20px rgba(60, 45, 20, .06);
  --radius: 14px;
  --display: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif;
  --body: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #171512;
    --surface: #211e1a;
    --ink: #f0eadd;
    --muted: #a39a8a;
    --line: #35302a;
    --line-strong: #51493f;
    --accent: #f0794d;
    --accent-ink: #1d1410;
    --accent-soft: #3a261d;
    --done: #63c497;
    --done-soft: #1e3329;
    --warn: #ff8f77;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 6px 20px rgba(0, 0, 0, .25);
  }
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.5 var(--body);
}

.app {
  max-width: 760px;
  margin: 0 auto;
  padding: 48px 16px 96px;
}

/* ---------- Header ---------- */

.hero {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 28px;
}

.eyebrow {
  margin: 0 0 4px;
  font-size: 12px;
  letter-spacing: .14em;
  text-transform: uppercase;
  color: var(--accent);
  font-weight: 600;
}

h1 {
  margin: 0;
  font: 600 clamp(36px, 8vw, 54px)/1.05 var(--display);
  letter-spacing: -.01em;
}

.progress {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-shrink: 0;
}

.ring { width: 56px; height: 56px; transform: rotate(-90deg); }
.ring circle { fill: none; stroke-width: 6; }
.ring-track { stroke: var(--line); }
.ring-fill {
  stroke: var(--done);
  stroke-linecap: round;
  stroke-dasharray: 163.36;
  stroke-dashoffset: 163.36;
  transition: stroke-dashoffset .5s ease;
}

.progress-text { display: flex; flex-direction: column; line-height: 1.2; }
.progress-pct { font: 600 22px var(--display); font-variant-numeric: tabular-nums; }
.progress-sub { font-size: 13px; color: var(--muted); white-space: nowrap; }

/* ---------- Controls ---------- */

input, select, textarea, button { font: inherit; color: inherit; }

input[type="text"], input[type="search"], input[type="date"], select, textarea {
  background: var(--surface);
  border: 1px solid var(--line-strong);
  border-radius: 10px;
  padding: 10px 12px;
  min-height: 44px;
  min-width: 0;
}

textarea { resize: vertical; width: 100%; }

input::placeholder, textarea::placeholder { color: var(--muted); opacity: .8; }

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.btn {
  border: 1px solid transparent;
  border-radius: 10px;
  padding: 10px 16px;
  min-height: 44px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
}

.btn.primary { background: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.07); }
.btn.ghost { background: transparent; border-color: var(--line-strong); }
.btn.ghost:hover { background: var(--accent-soft); border-color: var(--accent); }

/* ---------- Add form ---------- */

.add {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 12px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  margin-bottom: 24px;
}

.add-title { flex: 1 1 240px; }
.add input, .add select { background: var(--bg); }

/* ---------- Toolbar ---------- */

.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}

.tabs {
  display: inline-flex;
  padding: 3px;
  background: var(--line);
  border-radius: 12px;
}

.tabs button {
  border: 0;
  background: transparent;
  padding: 7px 14px;
  border-radius: 9px;
  cursor: pointer;
  font-weight: 600;
  font-size: 14px;
  color: var(--muted);
}

.tabs button span {
  font-weight: 500;
  font-variant-numeric: tabular-nums;
  opacity: .75;
  margin-left: 2px;
}

.tabs button[aria-pressed="true"] {
  background: var(--surface);
  color: var(--ink);
  box-shadow: 0 1px 2px rgba(0, 0, 0, .12);
}

.toolbar-right { display: flex; gap: 8px; flex: 1 1 260px; justify-content: flex-end; }
.toolbar-right input { flex: 1 1 120px; max-width: 220px; }
.toolbar-right input, .toolbar-right select { min-height: 38px; padding: 6px 10px; font-size: 14px; }

/* ---------- Category chips ---------- */

.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 16px; }
.chips:empty { display: none; }

.chip {
  border: 1px solid var(--line-strong);
  background: transparent;
  border-radius: 999px;
  padding: 4px 12px;
  font-size: 13px;
  cursor: pointer;
  color: var(--muted);
}

.chip:hover { border-color: var(--accent); color: var(--ink); }

.chip[aria-pressed="true"] {
  background: var(--ink);
  border-color: var(--ink);
  color: var(--bg);
}

/* ---------- List ---------- */

.list { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }

.item {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  padding: 14px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.check {
  flex-shrink: 0;
  width: 28px;
  height: 28px;
  margin-top: 1px;
  border-radius: 50%;
  border: 2px solid var(--line-strong);
  background: transparent;
  cursor: pointer;
  display: grid;
  place-items: center;
  color: transparent;
  transition: background .15s, border-color .15s;
}

.check svg { width: 14px; height: 14px; }
.check:hover { border-color: var(--done); color: var(--done); }

.item.done .check {
  background: var(--done);
  border-color: var(--done);
  color: var(--surface);
}

.item.pop .check { animation: pop .35s ease; }

@keyframes pop {
  0% { transform: scale(.7); }
  60% { transform: scale(1.2); }
  100% { transform: scale(1); }
}

.item-body { flex: 1; min-width: 0; }

.item-title {
  margin: 0;
  font: 600 19px/1.3 var(--display);
  overflow-wrap: anywhere;
}

.item.done .item-title {
  color: var(--muted);
  text-decoration: line-through;
  text-decoration-thickness: 1px;
}

.item-note {
  margin: 4px 0 0;
  font-size: 14px;
  color: var(--muted);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.meta {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px 10px;
  margin-top: 8px;
  font-size: 13px;
  color: var(--muted);
}

.tag {
  background: var(--accent-soft);
  color: var(--ink);
  border-radius: 999px;
  padding: 2px 10px;
}

.meta .overdue { color: var(--warn); font-weight: 600; }
.meta .done-on { color: var(--done); font-weight: 600; }

.actions { display: flex; gap: 2px; flex-shrink: 0; }

.icon-btn {
  width: 36px;
  height: 36px;
  border: 0;
  border-radius: 9px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  display: grid;
  place-items: center;
}

.icon-btn svg { width: 17px; height: 17px; }
.icon-btn:hover { background: var(--line); color: var(--ink); }
.icon-btn.danger:hover { color: var(--warn); }

/* ---------- Empty state ---------- */

.empty {
  text-align: center;
  padding: 56px 16px;
  border: 1.5px dashed var(--line-strong);
  border-radius: var(--radius);
}

.empty-title { margin: 0 0 4px; font: 600 22px var(--display); }
.empty-sub { margin: 0; color: var(--muted); }

/* ---------- Dialog ---------- */

dialog {
  width: min(480px, calc(100vw - 32px));
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 24px;
  background: var(--surface);
  color: var(--ink);
  box-shadow: 0 20px 60px rgba(0, 0, 0, .3);
}

dialog::backdrop { background: rgba(20, 15, 8, .5); }

dialog h2 { margin: 0 0 16px; font: 600 24px var(--display); }

dialog form { display: grid; gap: 14px; }

dialog label {
  display: grid;
  gap: 6px;
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
}

dialog input, dialog select, dialog textarea {
  background: var(--bg);
  font-weight: 400;
  color: var(--ink);
  width: 100%;
}

.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }

.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 4px; }

/* ---------- Toast ---------- */

.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 14px;
  max-width: calc(100vw - 32px);
  padding: 10px 10px 10px 16px;
  background: var(--ink);
  color: var(--bg);
  border-radius: 12px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, .3);
  font-size: 14px;
}

.toast[hidden] { display: none; }

.toast button {
  border: 0;
  border-radius: 8px;
  padding: 6px 12px;
  background: var(--bg);
  color: var(--ink);
  font-weight: 600;
  cursor: pointer;
}

/* ---------- Photos on cards ---------- */

.thumbs {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 68px));
  gap: 6px;
  margin-top: 10px;
}

.thumb {
  position: relative;
  aspect-ratio: 1;
  padding: 0;
  border: 0;
  border-radius: 10px;
  overflow: hidden;
  background: var(--line);
  cursor: zoom-in;
}

.thumb img { display: block; width: 100%; height: 100%; object-fit: cover; }
.thumb:hover img { transform: scale(1.04); }
.thumb img { transition: transform .2s ease; }

.thumb .more {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgba(15, 10, 5, .55);
  color: #fff;
  font-weight: 600;
  font-size: 15px;
}

.thumb.broken, .photo-grid li.broken { background: var(--accent-soft); }

.meta .adding { color: var(--accent); font-weight: 600; }

.icon-btn:disabled { opacity: .35; cursor: not-allowed; }
.icon-btn:disabled:hover { background: transparent; color: var(--muted); }

.item.drop-target {
  border-color: var(--accent);
  background: var(--accent-soft);
  outline: 2px dashed var(--accent);
  outline-offset: -6px;
}

/* ---------- Photos in the editor ---------- */

dialog { max-height: calc(100dvh - 32px); overflow: auto; }

.photo-field {
  display: grid;
  gap: 8px;
  padding: 2px;
  border-radius: 12px;
}

.photo-field[hidden] { display: none; }

.photo-field.drop-target { outline: 2px dashed var(--accent); outline-offset: 4px; }

.photo-field-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }

.field-label { font-size: 13px; font-weight: 600; color: var(--muted); }
.photo-count { font-weight: 500; }

.btn.small { min-height: 34px; padding: 6px 12px; font-size: 14px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }

.photo-grid {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(78px, 1fr));
  gap: 8px;
}

.photo-grid:empty { display: none; }

.photo-grid li {
  position: relative;
  aspect-ratio: 1;
  border-radius: 10px;
  overflow: hidden;
  background: var(--line);
}

.photo-grid li.placeholder {
  background: linear-gradient(100deg, var(--line) 30%, var(--line-strong) 50%, var(--line) 70%) 0 0 / 300% 100%;
  animation: shimmer 1.2s linear infinite;
}

@keyframes shimmer { to { background-position: -150% 0; } }

.photo-open {
  display: block;
  width: 100%;
  height: 100%;
  padding: 0;
  border: 0;
  background: transparent;
  cursor: zoom-in;
}

.photo-open img { display: block; width: 100%; height: 100%; object-fit: cover; }

/* The tile clips its overflow, so draw focus rings inside the edge. */
.photo-open:focus-visible { outline-offset: -3px; }
.photo-remove:focus-visible { outline: 2px solid #fff; outline-offset: 1px; }

.photo-remove {
  position: absolute;
  top: 4px;
  right: 4px;
  width: 28px;
  height: 28px;
  border: 0;
  border-radius: 50%;
  background: rgba(15, 10, 5, .65);
  color: #fff;
  display: grid;
  place-items: center;
  cursor: pointer;
}

.photo-remove svg { width: 12px; height: 12px; }
.photo-remove:hover { background: var(--warn); }

.photo-drop {
  margin: 0;
  padding: 16px;
  border: 1.5px dashed var(--line-strong);
  border-radius: 10px;
  text-align: center;
  font-size: 14px;
  color: var(--muted);
}

.photo-drop[hidden], .photo-error[hidden] { display: none; }

.photo-error { margin: 0; font-size: 13px; color: var(--warn); }

/* ---------- Photo viewer ---------- */

dialog.viewer {
  width: 100vw;
  height: 100dvh;
  max-width: none;
  max-height: none;
  margin: 0;
  padding: 0;
  border: 0;
  border-radius: 0;
  overflow: hidden;
  background: #0c0906;
  color: #fff;
  box-shadow: none;
}

dialog.viewer[open] { display: grid; grid-template-rows: auto 1fr auto; }
dialog.viewer::backdrop { background: rgba(12, 9, 6, .6); }

.viewer-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: calc(12px + env(safe-area-inset-top)) 16px 12px;
}

.viewer-caption { display: flex; align-items: baseline; gap: 10px; min-width: 0; }

.viewer-title {
  font: 600 18px var(--display);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.viewer-count { font-size: 13px; color: rgba(255, 255, 255, .65); white-space: nowrap; }

.viewer-close {
  flex-shrink: 0;
  min-height: 38px;
  padding: 6px 14px;
  border: 1px solid rgba(255, 255, 255, .3);
  border-radius: 10px;
  background: transparent;
  color: #fff;
  font-weight: 600;
  cursor: pointer;
}

.viewer-close:hover { background: rgba(255, 255, 255, .12); }

.viewer-stage { position: relative; min-height: 0; }

.viewer-stage img {
  position: absolute;
  inset: 8px 16px;
  width: calc(100% - 32px);
  height: calc(100% - 16px);
  object-fit: contain;
}

.viewer-stage img[hidden] { display: none; }

.viewer-missing {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  margin: 0;
  color: rgba(255, 255, 255, .7);
}

.viewer-missing[hidden] { display: none; }

.viewer-btn {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  width: 48px;
  height: 48px;
  border: 0;
  border-radius: 50%;
  background: rgba(255, 255, 255, .14);
  color: #fff;
  font-size: 30px;
  line-height: 1;
  cursor: pointer;
}

.viewer-btn:hover { background: rgba(255, 255, 255, .26); }
.viewer-btn[hidden] { display: none; }
.viewer-btn.prev { left: 16px; }
.viewer-btn.next { right: 16px; }

/* ---------- Sign-in, account, connection ---------- */

.loading {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  color: var(--muted);
}

.loading[hidden], .signin[hidden], .app[hidden], .banner[hidden] { display: none; }

.signin {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 24px 16px;
}

.signin-card {
  width: min(380px, 100%);
  display: grid;
  gap: 14px;
  padding: 28px 24px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 18px;
  box-shadow: var(--shadow);
}

.signin-card h1 { font-size: 40px; }
.signin-sub { margin: -4px 0 4px; color: var(--muted); }

.signin-card label {
  display: grid;
  gap: 6px;
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
}

.signin-card input {
  width: 100%;
  background: var(--bg);
  font-weight: 400;
  color: var(--ink);
}

.form-error { margin: 0; font-size: 14px; color: var(--warn); }
.form-error[hidden] { display: none; }

.account {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 10px;
  margin: -16px 0 16px;
  font-size: 13px;
  color: var(--muted);
}

.account strong { color: var(--ink); font-weight: 600; }

.link-btn {
  border: 0;
  background: none;
  padding: 4px 2px;
  color: var(--accent);
  font-weight: 600;
  font-size: 13px;
  cursor: pointer;
}

.link-btn:hover { text-decoration: underline; }

.banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 20px;
  padding: 10px 10px 10px 14px;
  border: 1px solid var(--warn);
  border-radius: 12px;
  background: var(--accent-soft);
  font-size: 14px;
}

.meta .by { margin-left: auto; font-size: 12px; }

.edit-meta { margin: 0; font-size: 12px; color: var(--muted); }

.photo-grid li.staged::after {
  content: "New";
  position: absolute;
  left: 4px;
  bottom: 4px;
  padding: 1px 6px;
  border-radius: 6px;
  background: var(--accent);
  color: var(--accent-ink);
  font-size: 11px;
  font-weight: 700;
  pointer-events: none;
}

.viewer-credit {
  margin: 0;
  padding: 0 16px calc(14px + env(safe-area-inset-bottom));
  font-size: 13px;
  color: rgba(255, 255, 255, .65);
  text-align: center;
}

/* ---------- Small screens ---------- */

@media (max-width: 520px) {
  .app { padding-top: 28px; }
  .hero { align-items: center; }
  .progress-sub { display: none; }
  .add select { flex: 1 1 120px; }
  .toolbar-right { justify-content: stretch; }
  .toolbar-right input { max-width: none; }
  .field-row { grid-template-columns: 1fr; }
  .item { padding: 12px; gap: 10px; }
  .actions { flex-direction: column; }
  .viewer-btn { width: 40px; height: 40px; font-size: 26px; }
  .viewer-btn.prev { left: 8px; }
  .viewer-btn.next { right: 8px; }
}

@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}
