:root {
  color-scheme: light;
  --bg: #f6f4ef;
  --surface: #ffffff;
  --surface-2: #efece5;
  --text: #1d2420;
  --muted: #5c665f;
  --border: #dcd7cc;
  --accent: #1f6b45;
  --accent-text: #ffffff;
  --danger: #a9262b;
  --todo: #a9262b;
  --written: #8a5a00;
  --sent: #1f6b45;
  --received: #2556a8;
  --on-status: #ffffff;
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #000000;
  --surface: #121212;
  --surface-2: #222222;
  --text: #ececec;
  --muted: #a0a0a0;
  --border: #303030;
  --accent: #d6d6d6;
  --accent-text: #0a0a0a;
  --danger: #ff8a8a;
  /* The card stages keep their colours so they can be told apart at a glance. */
  --todo: #ff8a8a;
  --written: #e9b44c;
  --sent: #5fc08c;
  --received: #85b4ff;
  --on-status: #0a0a0a;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
h1, h2, h3 { line-height: 1.2; margin: 0 0 .6rem; }
h2 { font-size: 1.15rem; }
h3 { font-size: 1rem; margin-top: 1.2rem; }
a { color: var(--accent); }
p { margin: .4rem 0 .8rem; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.loading { text-align: center; margin-top: 4rem; }

button, .button, input, select, textarea { font: inherit; color: inherit; }
button, .button {
  display: inline-block;
  min-height: 40px;
  padding: .45rem .9rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
  text-decoration: none;
}
button:hover, .button:hover { background: var(--surface-2); }
button:disabled { opacity: .5; cursor: default; }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 600; }
button.primary:hover { filter: brightness(1.08); }
button.danger { color: var(--danger); }
button.ghost { background: transparent; }
:focus-visible { outline: 2px solid var(--received); outline-offset: 2px; }

input, select, textarea {
  width: 100%;
  min-height: 40px;
  padding: .45rem .6rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
}
input[type="checkbox"] { width: 20px; height: 20px; min-height: 0; margin: 0; flex: none; accent-color: var(--accent); }
.field { display: block; margin-bottom: .75rem; }
.field > span { display: block; font-size: .85rem; color: var(--muted); margin-bottom: .2rem; }
.check { display: flex; gap: .6rem; align-items: center; margin: .4rem 0 .8rem; cursor: pointer; }
fieldset { border: 1px solid var(--border); border-radius: 10px; margin: 0 0 1rem; padding: .8rem .8rem 0; }
legend { font-weight: 600; padding: 0 .3rem; }

.panel { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 1.1rem; margin-bottom: 1rem; }

/* sign in */
.auth { max-width: 380px; margin: 0 auto; padding: 1rem; }
.auth-top { display: flex; justify-content: flex-end; margin-bottom: 1.5rem; }
.auth .panel { text-align: center; }
.auth .field { text-align: left; }
.auth button[type="submit"] { width: 100%; }

/* header */
header { background: var(--surface); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 5; padding: env(safe-area-inset-top) 16px 0; }
header .bar, header nav { max-width: 900px; margin: 0 auto; }
header .bar { display: flex; align-items: center; gap: .6rem; padding: .6rem 0 .3rem; }
.brand { display: flex; align-items: center; gap: .5rem; margin-right: auto; font-weight: 700; color: var(--text); text-decoration: none; white-space: nowrap; }
select.year { width: auto; }
header nav { display: flex; gap: .25rem; overflow-x: auto; }
header nav a { padding: .55rem .7rem; color: var(--muted); text-decoration: none; border-bottom: 3px solid transparent; white-space: nowrap; }
header nav a.active { color: var(--text); border-color: var(--accent); font-weight: 600; }

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

/* summary */
.stats { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: .6rem; }
.stat { display: flex; flex-direction: column; align-items: center; padding: .6rem .2rem; }
.stat strong { font-size: 1.7rem; line-height: 1.1; }
.stat span { font-size: .8rem; color: var(--muted); }
.stat.todo strong { color: var(--todo); }
.stat.written strong { color: var(--written); }
.stat.sent strong { color: var(--sent); }
.stat.received strong { color: var(--received); }
.stat.active { border-color: var(--text); box-shadow: inset 0 0 0 1px var(--text); }
.progress { margin: .8rem 0 1rem; font-size: .85rem; }
progress { display: block; width: 100%; height: 8px; border: 0; border-radius: 4px; overflow: hidden; background: var(--surface-2); appearance: none; }
progress::-webkit-progress-bar { background: var(--surface-2); }
progress::-webkit-progress-value { background: var(--sent); }
progress::-moz-progress-bar { background: var(--sent); }
.banner { display: flex; gap: 1rem; align-items: center; justify-content: space-between; flex-wrap: wrap; }

.toolbar { display: flex; gap: .6rem; margin-bottom: .8rem; flex-wrap: wrap; }
.toolbar input { flex: 1 1 200px; width: auto; }
.toolbar select { flex: 0 1 230px; width: auto; }
.toolbar button { white-space: nowrap; }

/* list */
.list { list-style: none; padding: 0; margin: 0; }
.row, .empty { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: .8rem; margin-bottom: .6rem; }
.empty { text-align: center; color: var(--muted); padding: 2rem 1rem; }
.row { display: flex; gap: .8rem; align-items: center; justify-content: space-between; border-left: 5px solid var(--border); }
.row.status-todo { border-left-color: var(--todo); }
.row.status-written { border-left-color: var(--written); }
.row.status-sent { border-left-color: var(--sent); }
.who { min-width: 0; overflow-wrap: anywhere; }
button.name { background: none; border: 0; padding: 0; min-height: 0; font-weight: 600; font-size: 1.05rem; text-align: left; }
button.name:hover { background: none; text-decoration: underline; }
.who .muted, .note, .history { font-size: .85rem; }
.note { font-style: italic; }
.history { color: var(--muted); }
.badges { display: flex; gap: .35rem; flex-wrap: wrap; margin-top: .3rem; }
.badge { font-size: .75rem; padding: .05rem .5rem; border-radius: 99px; background: var(--surface-2); color: var(--muted); }
.badge.got { color: var(--received); }
.controls { display: flex; gap: .5rem; flex: none; }
.segmented { display: flex; }
.segmented button { border-radius: 0; margin-left: -1px; font-size: .9rem; padding-inline: .7rem; }
.segmented button:first-child { border-radius: 8px 0 0 8px; margin-left: 0; }
.segmented button:last-child { border-radius: 0 8px 8px 0; }
.segmented button.on, .controls button.received.on { color: var(--on-status); font-weight: 600; }
.segmented button.on.todo { background: var(--todo); border-color: var(--todo); }
.segmented button.on.written { background: var(--written); border-color: var(--written); }
.segmented button.on.sent { background: var(--sent); border-color: var(--sent); }
.controls button.received { font-size: .9rem; min-width: 7.2rem; }
.controls button.received.on { background: var(--received); border-color: var(--received); }
.controls button.add { font-size: .9rem; }

/* dialogs and messages */
dialog {
  width: min(520px, calc(100vw - 24px));
  max-height: calc(100dvh - 24px);
  overflow-y: auto;
  padding: 1.1rem;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 14px;
}
dialog::backdrop { background: rgb(0 0 0 / .6); }
.pair { display: grid; grid-template-columns: 1fr 1fr; gap: .6rem; }
.actions { display: flex; gap: .5rem; justify-content: flex-end; flex-wrap: wrap; }
.actions.start { justify-content: flex-start; margin-bottom: .6rem; }
.actions .left { margin-right: auto; }
#toast {
  position: fixed; left: 50%; bottom: calc(20px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  max-width: calc(100vw - 32px); width: max-content;
  background: var(--text); color: var(--bg); padding: .7rem 1.1rem; border-radius: 10px;
  opacity: 0; pointer-events: none; transition: opacity .2s; z-index: 10;
}
#toast.show { opacity: 1; }
#toast.error { background: var(--danger); color: var(--on-status); }
.errors { color: var(--danger); }
.users { list-style: none; padding: 0; }
.users li { display: flex; justify-content: space-between; align-items: center; gap: .6rem; flex-wrap: wrap; padding: .5rem 0; border-bottom: 1px solid var(--border); }

/* labels */
details { margin-bottom: .8rem; }
summary { cursor: pointer; padding: .4rem 0; }
.people { max-height: 260px; overflow-y: auto; }
.sheets { overflow-x: auto; }
/* Paper is always white with black text, even in dark mode. */
.sheet { position: relative; overflow: hidden; background: #fff; color: #000; margin: 0 auto 12px; border: 1px solid var(--border); font-family: Arial, Helvetica, sans-serif; }
.label { position: absolute; display: flex; align-items: center; padding: 1.5mm 3mm; overflow: hidden; outline: 1px dashed #ccc; font-size: 9.5pt; line-height: 1.2; }
.label.tight { font-size: 8pt; line-height: 1.1; }
.label .address { white-space: nowrap; }
.envelope .address { position: absolute; font-size: 13pt; line-height: 1.4; }

@media (max-width: 680px) {
  .row { flex-direction: column; align-items: stretch; }
  .controls { flex-wrap: wrap; }
  .segmented { flex: 1 1 100%; }
  .segmented button { flex: 1; }
  .controls button.received, .controls button.add { flex: 1; }
  .brand span { display: none; }
  header nav { gap: 0; justify-content: space-between; }
  header nav a { padding-inline: .3rem; font-size: .92rem; }
  .toolbar select { flex: 1; }
}
@media screen and (max-width: 900px) { .sheet { zoom: .75; } }
@media screen and (max-width: 640px) { .sheet { zoom: .44; } }

@media print {
  :root, :root[data-theme="dark"] { color-scheme: light; }
  body { background: #fff; }
  header, .controls-panel, #toast, dialog { display: none !important; }
  main { max-width: none; margin: 0; padding: 0; }
  .sheets { overflow: visible; }
  .sheet { margin: 0; border: 0; break-after: page; zoom: 1; }
  .sheet:last-child { break-after: auto; }
  .label { outline: 0; }
}
