:root {
  --dark: #1F3864;
  --blue: #0058A3;
  --yellow: #FFDB00;
  --green-bg: #E2EFDA;
  --green-fg: #2E7D32;
  --amber-bg: #FFF2CC;
  --amber-fg: #9C6500;
  --red-bg: #FCE4E4;
  --red-fg: #C00000;

  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #f0f1f4;
  --text: #1a1d24;
  --text-muted: #6b7280;
  --border: #e2e4e9;
  --shadow: 0 1px 2px rgba(20, 20, 40, 0.04), 0 1px 8px rgba(20, 20, 40, 0.04);
  --radius: 12px;
}

:root[data-theme="dark"] {
  --bg: #14161c;
  --surface: #1c1f28;
  --surface-2: #232733;
  --text: #eef0f4;
  --text-muted: #9aa1b0;
  --border: #2c303c;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 1px 8px rgba(0, 0, 0, 0.3);
  --green-bg: #1d3324; --green-fg: #7fd894;
  --amber-bg: #3a2f10; --amber-fg: #f0c454;
  --red-bg: #3a1e1e; --red-fg: #ff8a8a;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #14161c;
    --surface: #1c1f28;
    --surface-2: #232733;
    --text: #eef0f4;
    --text-muted: #9aa1b0;
    --border: #2c303c;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 1px 8px rgba(0, 0, 0, 0.3);
    --green-bg: #1d3324; --green-fg: #7fd894;
    --amber-bg: #3a2f10; --amber-fg: #f0c454;
    --red-bg: #3a1e1e; --red-fg: #ff8a8a;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, "Segoe UI", Roboto, Inter, Arial, sans-serif;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}
h1, h2, h3 { margin: 0; }
.muted { color: var(--text-muted); }
.container { max-width: 1080px; margin: 0 auto; padding: 0 16px 64px; }

/* ---- top bar ------------------------------------------------------------- */
.topbar { background: var(--dark); color: #fff; }
.topbar-inner {
  max-width: 1080px; margin: 0 auto; padding: 22px 16px 20px;
  display: flex; align-items: flex-start; justify-content: space-between; gap: 12px;
}
.brand h1 { font-size: 1.35rem; font-weight: 700; letter-spacing: -0.01em; }
.brand .muted { color: #b9c4dd; font-size: 0.85rem; margin-top: 4px; }
.icon-btn {
  background: rgba(255,255,255,0.1); border: 1px solid rgba(255,255,255,0.18);
  color: #fff; border-radius: 999px; width: 38px; height: 38px; cursor: pointer;
  display: flex; align-items: center; justify-content: center; font-size: 15px; flex: none;
}
.icon-btn .moon { display: none; }
:root[data-theme="dark"] .icon-btn .sun,
@media (prefers-color-scheme: dark) { .icon-btn .sun { display: none; } .icon-btn .moon { display: inline; } }
:root[data-theme="dark"] .icon-btn .sun { display: none; }
:root[data-theme="dark"] .icon-btn .moon { display: inline; }
:root[data-theme="light"] .icon-btn .sun { display: inline; }
:root[data-theme="light"] .icon-btn .moon { display: none; }

/* ---- progress bar --------------------------------------------------------- */
.progress-bar-wrap {
  background: var(--surface); border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 20; box-shadow: var(--shadow);
}
.progress-inner {
  max-width: 1080px; margin: 0 auto; padding: 10px 16px;
  display: grid; grid-template-columns: 1fr 1fr; gap: 6px 24px; align-items: center;
}
.progress-stat { display: flex; justify-content: space-between; font-size: 0.78rem; }
.progress-label { color: var(--text-muted); font-weight: 600; text-transform: uppercase; letter-spacing: 0.03em; font-size: 0.68rem; }
.progress-value { font-weight: 700; font-variant-numeric: tabular-nums; }
.progress-track { height: 7px; background: var(--surface-2); border-radius: 999px; overflow: hidden; }
.progress-fill { height: 100%; width: 0%; background: var(--blue); transition: width 0.3s ease; border-radius: 999px; }
.progress-fill.amount { background: var(--yellow); }

/* ---- cards ---------------------------------------------------------------- */
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 18px 20px; margin-top: 18px; box-shadow: var(--shadow);
}
.card h2 { font-size: 1rem; font-weight: 700; margin-bottom: 12px; }

/* ---- summary table ---------------------------------------------------------- */
.summary-table { width: 100%; border-collapse: collapse; font-size: 0.88rem; }
.summary-table td { padding: 7px 4px; border-bottom: 1px solid var(--border); }
.summary-table td:last-child { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.summary-table tr.total td { font-weight: 700; background: color-mix(in srgb, var(--yellow) 20%, transparent); }
.summary-table tr:last-child td { border-bottom: none; }
.note { font-size: 0.78rem; color: var(--text-muted); font-style: italic; margin: 4px 0 0; }

/* ---- resolve / settled lists ------------------------------------------------ */
.resolve-list { display: flex; flex-direction: column; gap: 8px; }
.resolve-item {
  display: grid; grid-template-columns: auto 130px 1fr; gap: 6px 12px;
  padding: 10px 12px; border-radius: 8px; background: var(--surface-2); align-items: start;
  font-size: 0.85rem;
}
.resolve-item input[type=checkbox] { margin-top: 3px; }
.resolve-tag {
  font-size: 0.68rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.02em;
  padding: 2px 6px; border-radius: 5px; white-space: nowrap; height: fit-content;
}
.resolve-what { font-weight: 700; }
.resolve-why { color: var(--text-muted); grid-column: 1 / -1; }
.resolve-item.checked { opacity: 0.55; }
.resolve-item.checked .resolve-what { text-decoration: line-through; }

.tag-red { background: var(--red-bg); color: var(--red-fg); }
.tag-amber { background: var(--amber-bg); color: var(--amber-fg); }
.tag-green { background: var(--green-bg); color: var(--green-fg); }

.settled-card { padding: 12px 20px; }
.collapse-toggle {
  width: 100%; background: none; border: none; color: var(--text); cursor: pointer;
  display: flex; align-items: center; justify-content: space-between; font-size: 1rem; font-weight: 700; padding: 4px 0;
}
.chevron { transition: transform 0.2s ease; color: var(--text-muted); }
.collapse-toggle[aria-expanded="true"] .chevron { transform: rotate(90deg); }
.settled-list { margin-top: 12px; }
.settled-list.collapsed { display: none; }
.settled-list .resolve-item { background: var(--green-bg); }

/* ---- controls --------------------------------------------------------------- */
.controls { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; }
#search {
  flex: 1 1 220px; padding: 9px 12px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--surface-2); color: var(--text); font-size: 0.88rem;
}
#search:focus { outline: 2px solid var(--blue); outline-offset: 1px; }
.chip-row { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  border: 1px solid var(--border); background: var(--surface-2); color: var(--text);
  border-radius: 999px; padding: 5px 12px; font-size: 0.76rem; font-weight: 600; cursor: pointer;
  white-space: nowrap;
}
.chip.active { background: var(--blue); border-color: var(--blue); color: #fff; }
.hide-checked { display: flex; align-items: center; gap: 6px; font-size: 0.82rem; color: var(--text-muted); cursor: pointer; white-space: nowrap; }
.text-btn {
  background: none; border: none; color: var(--blue); font-size: 0.8rem; font-weight: 600;
  cursor: pointer; text-decoration: underline; padding: 4px 0; margin-left: auto;
}

/* ---- category sections -------------------------------------------------------- */
.category {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  margin-top: 14px; overflow: hidden; box-shadow: var(--shadow);
}
.category-header {
  width: 100%; display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 14px 18px; background: var(--dark); color: #fff; border: none; cursor: pointer; font-size: 0.95rem;
}
.category-header .cat-name { font-weight: 700; }
.category-header .cat-meta { display: flex; align-items: center; gap: 14px; font-size: 0.78rem; color: #c3cde3; }
.category-header .cat-progress { font-variant-numeric: tabular-nums; }
.category-header .chevron { color: #c3cde3; }
.category-body { padding: 4px 0; }
.category-body.collapsed { display: none; }

.item-row {
  display: grid;
  grid-template-columns: 24px 1fr auto auto auto;
  gap: 4px 14px;
  padding: 11px 18px;
  border-top: 1px solid var(--border);
  align-items: start;
  font-size: 0.86rem;
}
.item-row:hover { background: var(--surface-2); }
.item-row.hidden { display: none; }
.item-row input[type=checkbox] { margin-top: 3px; width: 16px; height: 16px; accent-color: var(--blue); cursor: pointer; }
.item-main { min-width: 0; }
.item-ref { font-size: 0.68rem; color: var(--text-muted); font-weight: 700; letter-spacing: 0.02em; }
.item-desc { font-weight: 700; }
.item-spec { color: var(--text-muted); font-size: 0.8rem; margin-top: 1px; }
.item-note {
  margin-top: 5px; font-size: 0.78rem; padding: 6px 9px; border-radius: 6px;
  background: var(--amber-bg); color: var(--amber-fg);
}
.item-qty { color: var(--text-muted); font-size: 0.8rem; white-space: nowrap; text-align: right; align-self: center; }
.item-amount { font-weight: 700; text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; align-self: center; }
.item-rate { display: block; font-weight: 400; color: var(--text-muted); font-size: 0.72rem; }
.status-pill {
  justify-self: end; font-size: 0.68rem; font-weight: 700; padding: 3px 8px; border-radius: 999px;
  white-space: nowrap; align-self: center;
}
.item-row.checked { opacity: 0.5; }
.item-row.checked .item-desc { text-decoration: line-through; }

.item-actions { display: flex; gap: 4px; align-self: center; opacity: 0; transition: opacity 0.12s ease; }
.item-row:hover .item-actions, .item-row:focus-within .item-actions { opacity: 1; }
.icon-action {
  border: 1px solid var(--border); background: var(--surface); color: var(--text-muted);
  border-radius: 6px; width: 26px; height: 26px; cursor: pointer; font-size: 12px;
  display: flex; align-items: center; justify-content: center;
}
.icon-action:hover { background: var(--surface-2); color: var(--text); }
.delete-btn:hover { color: var(--red-fg); border-color: var(--red-fg); }

.add-item-row {
  width: 100%; text-align: left; background: none; border: none; border-top: 1px dashed var(--border);
  color: var(--blue); font-size: 0.82rem; font-weight: 600; padding: 10px 18px; cursor: pointer;
}
.add-item-row:hover { background: var(--surface-2); }

.primary-btn {
  background: var(--blue); color: #fff; border: none; border-radius: 8px;
  padding: 9px 16px; font-size: 0.84rem; font-weight: 700; cursor: pointer;
}
.primary-btn:hover { filter: brightness(1.08); }
.primary-btn:disabled { opacity: 0.5; cursor: not-allowed; }
.danger-btn {
  background: var(--red-bg); color: var(--red-fg); border: 1px solid transparent; border-radius: 8px;
  padding: 9px 16px; font-size: 0.84rem; font-weight: 700; cursor: pointer;
}
.danger-btn:hover { filter: brightness(0.97); }

/* ---- add/edit item dialog -------------------------------------------------------- */
dialog#item-dialog {
  border: 1px solid var(--border); border-radius: var(--radius); padding: 22px 24px;
  background: var(--surface); color: var(--text); width: min(560px, 92vw);
  box-shadow: 0 8px 40px rgba(0,0,0,0.25);
}
dialog#item-dialog::backdrop { background: rgba(10, 12, 18, 0.55); }
dialog#item-dialog h2 { font-size: 1.05rem; margin-bottom: 14px; }
dialog#item-dialog label {
  display: flex; flex-direction: column; gap: 4px; font-size: 0.78rem; font-weight: 600;
  color: var(--text-muted); margin-bottom: 12px;
}
dialog#item-dialog label.grow { flex: 1; }
dialog#item-dialog input, dialog#item-dialog select, dialog#item-dialog textarea {
  font-family: inherit; font-size: 0.88rem; font-weight: 400; color: var(--text);
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 7px; padding: 8px 10px;
}
dialog#item-dialog input:focus, dialog#item-dialog select:focus, dialog#item-dialog textarea:focus {
  outline: 2px solid var(--blue); outline-offset: 1px;
}
dialog#item-dialog textarea { resize: vertical; font-family: inherit; }
.form-row { display: flex; gap: 12px; }
.form-row > label { flex: 1; min-width: 0; }
.dialog-actions { display: flex; align-items: center; gap: 10px; margin-top: 6px; }
.dialog-actions .spacer { flex: 1; }

@media (max-width: 640px) {
  .item-row { grid-template-columns: 20px 1fr; }
  .item-qty, .item-amount, .status-pill, .item-actions { grid-column: 2; justify-self: start; text-align: left; }
  .status-pill { margin-top: 4px; }
  .item-actions { opacity: 1; margin-top: 4px; }
  .progress-inner { grid-template-columns: 1fr; }
  .resolve-item { grid-template-columns: auto 1fr; }
  .resolve-tag { grid-row: 1; }
  .form-row { flex-direction: column; gap: 0; }
}

/* ---- footer -------------------------------------------------------------------- */
.foot { margin-top: 32px; padding-top: 16px; border-top: 1px solid var(--border); }
.foot p { font-size: 0.76rem; color: var(--text-muted); }

@media print {
  .progress-bar-wrap, .controls-card, #theme-toggle, .text-btn, .item-actions, .add-item-row { display: none !important; }
  .category-body.collapsed { display: block !important; }
  .settled-list.collapsed { display: block !important; }
  body { background: #fff; }
}
