:root {
  --bg: #f4f6f5;
  --card: #ffffff;
  --text: #16211b;
  --muted: #5d6b63;
  --line: #dfe5e1;
  --brand: #1f6b45;
  --brand-2: #2b8a5b;
  --brand-soft: #e3f1e9;
  --warn: #b45309;
  --warn-soft: #fdf1e3;
  --danger: #b42318;
  --done: #15803d;
  --shadow: 0 1px 2px rgba(16, 40, 28, .06), 0 2px 8px rgba(16, 40, 28, .05);
  --radius: 14px;
  --tap: 52px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1512; --card: #18211c; --text: #e8efeb; --muted: #9aaba2; --line: #2a3630;
    --brand: #3fa672; --brand-2: #54b983; --brand-soft: #1d3327; --warn: #f0a44b; --warn-soft: #3a2a17;
    --danger: #f2766b; --done: #4ade80; --shadow: none; color-scheme: dark;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; background: var(--bg); color: var(--text); }
body {
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  padding-top: calc(60px + env(safe-area-inset-top));
  padding-bottom: calc(76px + env(safe-area-inset-bottom));
  min-height: 100vh;
}
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--brand); }
[hidden] { display: none !important; }

/* ---------- Topbar ---------- */
.topbar {
  position: fixed; inset: 0 0 auto 0; z-index: 20;
  display: flex; align-items: center; gap: 6px;
  padding: env(safe-area-inset-top) 10px 0 10px; height: calc(60px + env(safe-area-inset-top));
  background: var(--brand); color: #fff;
}
@media (prefers-color-scheme: dark) { .topbar { background: #14432c; } }
.titles { flex: 1; min-width: 0; padding-left: 6px; }
.title { font-weight: 700; font-size: 19px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.subtitle { font-size: 13px; opacity: .85; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.subtitle:empty { display: none; }
.icon-btn { background: none; border: 0; color: inherit; width: 44px; height: 44px; display: grid; place-items: center; border-radius: 10px; cursor: pointer; }
.icon-btn:active { background: rgba(255,255,255,.15); }
.sync-pill {
  display: flex; align-items: center; gap: 7px; border: 0; cursor: pointer;
  background: rgba(255,255,255,.16); color: #fff; border-radius: 999px; padding: 7px 12px; font-size: 13px; font-weight: 600;
}
.sync-pill .dot { width: 9px; height: 9px; border-radius: 50%; background: #86efac; }
.sync-pill.pending .dot { background: #fcd34d; }
.sync-pill.offline .dot { background: #fca5a5; }
.sync-pill.busy .dot { animation: pulse 1s infinite; }
@keyframes pulse { 50% { opacity: .3; } }

/* ---------- Tabbar ---------- */
.tabbar {
  position: fixed; inset: auto 0 0 0; z-index: 20; display: flex;
  background: var(--card); border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
}
.tabbar a {
  flex: 1; display: flex; flex-direction: column; align-items: center; gap: 3px;
  padding: 9px 0 10px; color: var(--muted); text-decoration: none; font-size: 12px; font-weight: 600;
}
.tabbar svg { width: 26px; height: 26px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.tabbar circle { fill: currentColor; stroke: none; }
.tabbar a.active { color: var(--brand); }

/* ---------- Layout ---------- */
main { max-width: 760px; margin: 0 auto; padding: 14px 14px 24px; }
.section-title { font-size: 13px; font-weight: 700; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin: 22px 4px 8px; display: flex; align-items: center; justify-content: space-between; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.row { display: flex; gap: 10px; align-items: center; }
.grow { flex: 1; min-width: 0; }
.stack > * + * { margin-top: 10px; }

.card { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); border: 1px solid var(--line); }
.pad { padding: 14px 16px; }

.banner {
  display: flex; align-items: center; gap: 12px; padding: 12px 14px; border-radius: var(--radius);
  background: var(--brand-soft); color: var(--text); text-decoration: none; border: 1px solid transparent;
}
.banner b { display: block; }
.banner.warn { background: var(--warn-soft); }
.banner .chev { margin-left: auto; color: var(--muted); font-size: 22px; }

.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-top: 12px; }
.stat { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px; box-shadow: var(--shadow); }
.stat .num { font-size: 26px; font-weight: 800; line-height: 1.1; font-variant-numeric: tabular-nums; }
.stat .lbl { font-size: 12.5px; color: var(--muted); margin-top: 2px; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 46px; padding: 0 16px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--card); font-weight: 650; cursor: pointer; text-decoration: none; color: var(--text);
}
.btn:active { transform: scale(.98); }
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn.danger { color: var(--danger); }
.btn.block { width: 100%; }
.btn.big { min-height: 56px; font-size: 17px; }
.btn.on { background: var(--brand-soft); border-color: var(--brand); color: var(--brand); }
.btn.dashed { border-style: dashed; border-width: 2px; background: transparent; color: var(--brand); }
.btn[disabled] { opacity: .5; pointer-events: none; }
.toolbar { display: flex; gap: 8px; margin: 12px 0 10px; flex-wrap: wrap; }
.toolbar .btn { min-height: 42px; font-size: 15px; }

/* ---------- Inputs ---------- */
.field { display: block; margin-bottom: 12px; }
.field > span { display: block; font-size: 13px; font-weight: 650; color: var(--muted); margin-bottom: 5px; }
.input, select.input, textarea.input {
  width: 100%; min-height: 48px; padding: 10px 12px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--card); outline: none;
}
.input:focus { border-color: var(--brand); box-shadow: 0 0 0 3px var(--brand-soft); }
.search { position: relative; }
.search .input { padding-left: 40px; }
.search svg { position: absolute; left: 12px; top: 50%; transform: translateY(-50%); width: 20px; height: 20px; stroke: var(--muted); fill: none; stroke-width: 2; }
.chips { display: flex; gap: 8px; overflow-x: auto; padding: 10px 0 2px; scrollbar-width: none; }
.chip { flex: none; border: 1px solid var(--line); background: var(--card); border-radius: 999px; padding: 7px 13px; font-size: 14px; font-weight: 600; cursor: pointer; white-space: nowrap; }
.chip.on { background: var(--brand); color: #fff; border-color: var(--brand); }
.check { display: flex; gap: 10px; align-items: center; min-height: 44px; }
.check input { width: 22px; height: 22px; accent-color: var(--brand); }

/* ---------- Lists ---------- */
.list { display: flex; flex-direction: column; gap: 8px; }
.item {
  display: flex; align-items: center; gap: 12px; min-height: 64px; padding: 10px 14px;
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow);
  text-decoration: none; color: inherit; cursor: pointer; user-select: none;
}
.item:active { background: var(--brand-soft); }
.item .name { font-weight: 700; font-size: 16.5px; }
.item .meta { font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item .total { text-align: right; min-width: 56px; }
.item .total b { font-size: 22px; font-variant-numeric: tabular-nums; display: block; line-height: 1.1; }
.item .total span { font-size: 12px; color: var(--muted); }
.item.dim { opacity: .5; }
.item.done { border-color: color-mix(in srgb, var(--done) 45%, var(--line)); }
.handle { width: 36px; height: 44px; display: grid; place-items: center; color: var(--muted); cursor: grab; touch-action: none; flex: none; }
.handle svg { width: 24px; height: 24px; fill: currentColor; }
.mini-btn { width: 40px; height: 40px; flex: none; display: grid; place-items: center; border-radius: 10px; border: 1px solid var(--line); background: var(--card); cursor: pointer; color: var(--muted); }
.mini-btn svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.move-btns { display: flex; flex-direction: column; gap: 4px; flex: none; }
.move-btns button { width: 40px; height: 28px; border-radius: 8px; border: 1px solid var(--line); background: var(--card); color: var(--muted); font-size: 12px; cursor: pointer; }
.move-btns button:active { background: var(--brand-soft); color: var(--brand); }
.badge { display: inline-flex; align-items: center; gap: 4px; font-size: 12px; font-weight: 700; padding: 2px 8px; border-radius: 999px; background: var(--brand-soft); color: var(--brand); }
.badge.done { background: color-mix(in srgb, var(--done) 16%, transparent); color: var(--done); }
.progress { height: 5px; background: var(--line); border-radius: 99px; overflow: hidden; margin-top: 6px; }
.progress > i { display: block; height: 100%; background: var(--done); border-radius: 99px; }
.sortable-ghost { opacity: .35; }
.sortable-chosen { box-shadow: 0 8px 24px rgba(0,0,0,.18); }

/* ---------- Counting ---------- */
.count-row { display: flex; align-items: center; gap: 10px; padding: 10px 10px 10px 14px; min-height: 76px; background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
.count-row.has { border-color: color-mix(in srgb, var(--brand) 50%, var(--line)); background: color-mix(in srgb, var(--brand-soft) 45%, var(--card)); }
.count-row .name { font-weight: 750; font-size: 17px; line-height: 1.2; }
.count-row .size { font-weight: 800; color: var(--brand); }
.count-row .meta { font-size: 12.5px; color: var(--muted); margin-top: 2px; }
.count-row .note { font-size: 13px; color: var(--warn); margin-top: 3px; cursor: pointer; }
.link-btn { background: none; border: 0; padding: 4px 0 0; color: var(--muted); font-size: 13px; font-weight: 600; cursor: pointer; }
.stepper { display: flex; align-items: center; gap: 6px; flex: none; }
.stepper button {
  width: var(--tap); height: var(--tap); border-radius: 14px; border: 0; cursor: pointer;
  font-size: 28px; font-weight: 700; line-height: 1; display: grid; place-items: center;
}
.stepper .minus { background: var(--line); color: var(--text); }
.stepper .plus { background: var(--brand); color: #fff; }
.stepper button:active { transform: scale(.92); }
.stepper input {
  width: 64px; height: var(--tap); text-align: center; font-size: 24px; font-weight: 800; font-variant-numeric: tabular-nums;
  border: 2px solid var(--line); border-radius: 12px; background: var(--card); -moz-appearance: textfield;
}
.stepper input::-webkit-outer-spin-button, .stepper input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.stepper input:focus { outline: none; border-color: var(--brand); }
@media (max-width: 380px) { :root { --tap: 46px; } .stepper input { width: 54px; } }

.sticky-foot {
  position: sticky; bottom: calc(76px + env(safe-area-inset-bottom)); margin: 18px -14px -24px; padding: 12px 14px;
  background: linear-gradient(to top, var(--bg) 70%, transparent);
}
.done-box { display: flex; gap: 10px; }

/* ---------- Tables ---------- */
.table-wrap { overflow-x: auto; border-radius: var(--radius); border: 1px solid var(--line); background: var(--card); }
table { border-collapse: collapse; width: 100%; font-size: 14px; }
th, td { padding: 9px 10px; text-align: left; border-bottom: 1px solid var(--line); white-space: nowrap; }
th { font-size: 12px; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); background: color-mix(in srgb, var(--line) 35%, var(--card)); }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
tr.total td { font-weight: 800; }
tr:last-child td { border-bottom: 0; }

/* ---------- Sheet ---------- */
.sheet-back { position: fixed; inset: 0; background: rgba(0,0,0,.4); z-index: 40; animation: fade .15s; }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 41; max-height: 92vh; display: flex; flex-direction: column;
  background: var(--bg); border-radius: 20px 20px 0 0; animation: up .2s ease-out; max-width: 760px; margin: 0 auto;
  padding-bottom: env(safe-area-inset-bottom);
}
.sheet-head { display: flex; align-items: center; gap: 10px; padding: 14px 16px 10px; border-bottom: 1px solid var(--line); }
.sheet-head h2 { margin: 0; font-size: 18px; flex: 1; }
.sheet-body { overflow-y: auto; padding: 14px 16px; flex: 1; }
.sheet-foot { display: flex; gap: 10px; padding: 12px 16px; border-top: 1px solid var(--line); }
.sheet-foot .btn { flex: 1; }
@keyframes up { from { transform: translateY(40px); opacity: 0; } }
@keyframes fade { from { opacity: 0; } }

.pick { display: flex; align-items: center; gap: 12px; padding: 12px 14px; min-height: 58px; background: var(--card); border: 2px solid var(--line); border-radius: 12px; cursor: pointer; }
.pick.on { border-color: var(--brand); background: var(--brand-soft); }
.pick .tick { width: 26px; height: 26px; border-radius: 8px; border: 2px solid var(--line); display: grid; place-items: center; flex: none; color: #fff; }
.pick.on .tick { background: var(--brand); border-color: var(--brand); }
.pick .tick svg { width: 16px; height: 16px; fill: none; stroke: currentColor; stroke-width: 3; opacity: 0; }
.pick.on .tick svg { opacity: 1; }

#toast {
  position: fixed; left: 50%; bottom: calc(90px + env(safe-area-inset-bottom)); transform: translate(-50%, 20px);
  background: #1d2a23; color: #fff; padding: 11px 18px; border-radius: 12px; font-weight: 600; font-size: 15px;
  opacity: 0; pointer-events: none; transition: .2s; z-index: 60; max-width: 90vw; text-align: center;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

.empty { text-align: center; padding: 34px 20px; color: var(--muted); }
.empty b { display: block; color: var(--text); font-size: 17px; margin-bottom: 6px; }

/* ---------- Login ---------- */
.login { max-width: 400px; margin: 6vh auto 0; }
.login .logo { width: 84px; height: 84px; border-radius: 22px; display: block; margin: 0 auto 14px; }
.login h1 { text-align: center; margin: 0 0 4px; font-size: 24px; }
.login p { text-align: center; margin: 0 0 22px; }
.pin { letter-spacing: .4em; text-align: center; font-size: 24px; font-weight: 800; }
.error { color: var(--danger); font-weight: 600; margin: 4px 0 12px; }
body.login-mode .tabbar, body.login-mode .sync-pill { display: none; }
