:root {
  --bg: #f6f5f1;
  --surface: #ffffff;
  --surface-2: #efede7;
  --line: #e2dfd7;
  --text: #1d1e22;
  --muted: #6b6a66;
  --faint: #9a988f;
  --accent: #3b5bdb;
  --accent-ink: #ffffff;
  --accent-soft: #e6ebfc;
  --danger: #c92a2a;
  --danger-soft: #fdeaea;
  --warn: #b35c00;
  --warn-soft: #fff1df;
  --ok: #2b8a3e;
  --ok-soft: #e3f4e6;
  --st-idea: #8b5cf6;
  --st-active: #2b8a3e;
  --st-paused: #b35c00;
  --st-done: #6b6a66;
  --shadow: 0 1px 2px rgba(0,0,0,.05), 0 2px 8px rgba(0,0,0,.04);
  --r: 14px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #16171b; --surface: #202227; --surface-2: #2a2c32; --line: #34363d;
    --text: #ececea; --muted: #a3a29c; --faint: #75746f;
    --accent: #7c94f5; --accent-ink: #10131f; --accent-soft: #252c47;
    --danger: #f06a6a; --danger-soft: #3a2224; --warn: #f0a248; --warn-soft: #3a2c1b;
    --ok: #5cc278; --ok-soft: #1e3324;
    --st-idea: #a78bfa; --st-active: #5cc278; --st-paused: #f0a248; --st-done: #a3a29c;
    --shadow: 0 1px 2px rgba(0,0,0,.3);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #16171b; --surface: #202227; --surface-2: #2a2c32; --line: #34363d;
  --text: #ececea; --muted: #a3a29c; --faint: #75746f;
  --accent: #7c94f5; --accent-ink: #10131f; --accent-soft: #252c47;
  --danger: #f06a6a; --danger-soft: #3a2224; --warn: #f0a248; --warn-soft: #3a2c1b;
  --ok: #5cc278; --ok-soft: #1e3324;
  --st-idea: #a78bfa; --st-active: #5cc278; --st-paused: #f0a248; --st-done: #a3a29c;
  --shadow: 0 1px 2px rgba(0,0,0,.3);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 15px/1.5 "IBM Plex Sans Thai", system-ui, -apple-system, "Segoe UI", sans-serif;
  -webkit-tap-highlight-color: transparent;
  padding-bottom: env(safe-area-inset-bottom);
}
body.noscroll { overflow: hidden; }
a { color: inherit; text-decoration: none; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
h1, h2, h3 { margin: 0; line-height: 1.25; }
[hidden] { display: none !important; }

.wrap { max-width: 1100px; margin: 0 auto; padding: 0 16px 96px; }

/* top bar */
.top {
  position: sticky; top: 0; z-index: 5; background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
  padding-top: env(safe-area-inset-top);
  border-bottom: 1px solid transparent;
}
.top.scrolled { border-bottom-color: var(--line); }
.top-in { max-width: 1100px; margin: 0 auto; padding: 10px 16px; display: flex; align-items: center; gap: 10px; min-height: 56px; }
.top-title { flex: 1; min-width: 0; display: flex; align-items: center; gap: 10px; }
.top-title h1 { font-size: 20px; font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.top-title .em { font-size: 24px; }
.icon-btn {
  border: 0; background: transparent; width: 40px; height: 40px; border-radius: 12px;
  display: inline-grid; place-items: center; font-size: 18px; color: var(--muted); flex: none;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.sync { font-size: 12px; padding: 4px 10px; border-radius: 99px; border: 1px solid var(--line); background: var(--surface); color: var(--muted); display: inline-flex; gap: 6px; align-items: center; white-space: nowrap; }
.sync i { width: 8px; height: 8px; border-radius: 50%; background: var(--faint); display: inline-block; }
.sync.ok i { background: var(--ok); }
.sync.busy i, .sync.pending i { background: var(--accent); animation: pulse 1s infinite; }
.sync.offline i, .sync.error i { background: var(--warn); }
.sync.auth i { background: var(--danger); }
@keyframes pulse { 50% { opacity: .3; } }

/* buttons */
.btn {
  border: 1px solid var(--line); background: var(--surface); border-radius: 10px; padding: 8px 14px;
  font-weight: 500; display: inline-flex; align-items: center; gap: 6px; min-height: 40px; white-space: nowrap;
}
.btn:hover { border-color: var(--faint); }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.ghost:hover { background: var(--surface-2); }
.btn.danger { color: var(--danger); }
.btn.sm { min-height: 32px; padding: 4px 10px; font-size: 13px; }
.sp { flex: 1; }
.fab {
  position: fixed; right: max(16px, env(safe-area-inset-right)); bottom: calc(20px + env(safe-area-inset-bottom));
  z-index: 6; border: 0; border-radius: 16px; background: var(--accent); color: var(--accent-ink);
  padding: 14px 20px; font-weight: 600; box-shadow: 0 6px 20px rgba(0,0,0,.18); display: flex; gap: 8px; align-items: center;
}

/* chips / pills */
.chips { display: flex; gap: 8px; overflow-x: auto; padding: 4px 0 12px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip {
  border: 1px solid var(--line); background: var(--surface); border-radius: 99px; padding: 6px 14px;
  font-size: 14px; white-space: nowrap; color: var(--muted);
}
.chip b { font-weight: 600; margin-left: 4px; color: var(--faint); }
.chip.on { background: var(--text); color: var(--bg); border-color: var(--text); }
.chip.on b { color: inherit; opacity: .7; }
.pill { font-size: 12px; padding: 2px 9px; border-radius: 99px; font-weight: 500; white-space: nowrap; display: inline-block; }
.st-idea { color: var(--st-idea); background: color-mix(in srgb, var(--st-idea) 14%, transparent); }
.st-active { color: var(--st-active); background: color-mix(in srgb, var(--st-active) 14%, transparent); }
.st-paused { color: var(--st-paused); background: color-mix(in srgb, var(--st-paused) 14%, transparent); }
.st-done { color: var(--st-done); background: color-mix(in srgb, var(--st-done) 16%, transparent); }
.tag { font-size: 12px; padding: 2px 8px; border-radius: 6px; background: var(--surface-2); color: var(--muted); white-space: nowrap; }
.tag.late { background: var(--danger-soft); color: var(--danger); }
.tag.soon { background: var(--warn-soft); color: var(--warn); }
.tag.ok { background: var(--ok-soft); color: var(--ok); }

/* summary strip */
.strip { display: grid; grid-template-columns: repeat(4, minmax(0,1fr)); gap: 10px; margin: 8px 0 18px; }
.stat { background: var(--surface); border: 1px solid var(--line); border-radius: var(--r); padding: 12px 14px; }
.stat b { display: block; font-size: 24px; font-weight: 700; font-variant-numeric: tabular-nums; }
.stat span { font-size: 13px; color: var(--muted); }
.stat.warn b { color: var(--danger); }
@media (max-width: 560px) {
  .strip { grid-template-columns: repeat(2, minmax(0,1fr)); gap: 8px; }
  .stat { padding: 8px 12px; display: flex; align-items: baseline; gap: 8px; }
  .stat b { font-size: 20px; }
  .stat span { font-size: 12px; line-height: 1.3; }
  .money-sum .stat { display: block; }
  .money-sum .stat b { font-size: 16px; }
  .sync { padding: 4px 8px; }
  .sync span { display: none; }
  .sync.auth span, .sync.error span, .sync.offline span { display: inline; }
}

.sec-h { display: flex; align-items: center; gap: 8px; margin: 22px 0 10px; }
.sec-h h2 { font-size: 16px; font-weight: 600; }
.sec-h .n { color: var(--faint); font-size: 14px; }

/* project cards */
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 12px; }
.pcard {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--r); padding: 14px 16px 12px;
  box-shadow: var(--shadow); display: flex; flex-direction: column; gap: 8px; position: relative; overflow: hidden;
  transition: transform .12s, border-color .12s;
}
.pcard::before { content: ""; position: absolute; left: 0; top: 0; bottom: 0; width: 4px; background: var(--pc, var(--accent)); }
.pcard:hover { border-color: var(--faint); }
.pcard:active { transform: scale(.99); }
.pcard.done { opacity: .7; }
.pc-top { display: flex; gap: 10px; align-items: flex-start; }
.pc-top .em { font-size: 26px; line-height: 1; padding-top: 2px; }
.pc-t { flex: 1; min-width: 0; }
.pc-t b { display: block; font-size: 16px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pc-goal { margin: 0; color: var(--muted); font-size: 14px; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.bar { height: 6px; border-radius: 99px; background: var(--surface-2); overflow: hidden; }
.bar i { display: block; height: 100%; background: var(--pc, var(--accent)); border-radius: 99px; }
.pc-meta { font-size: 13px; color: var(--muted); display: flex; justify-content: space-between; gap: 8px; }
.pc-tags { display: flex; flex-wrap: wrap; gap: 6px; }
.pc-foot { font-size: 12px; color: var(--faint); }
.pin { color: var(--warn); font-size: 14px; }

/* list rows */
.list { background: var(--surface); border: 1px solid var(--line); border-radius: var(--r); overflow: hidden; }
.row { display: flex; align-items: center; gap: 10px; padding: 11px 14px; border-top: 1px solid var(--line); min-height: 52px; }
.row:first-child { border-top: 0; }
.row .main { flex: 1; min-width: 0; }
.row .t { display: block; overflow: hidden; text-overflow: ellipsis; }
.row .s { display: block; font-size: 13px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row.click { cursor: pointer; }
.row.click:hover { background: var(--surface-2); }
.row .proj { font-size: 12px; color: var(--muted); }
.empty { text-align: center; color: var(--muted); padding: 28px 16px; font-size: 14px; }
.empty.big { padding: 56px 16px; }
.empty.big .em { font-size: 44px; display: block; margin-bottom: 8px; }
.empty.big p { margin: 4px 0 16px; }

/* project header + tabs */
.phead { display: flex; gap: 12px; align-items: center; margin: 6px 0 4px; }
.phead .em { font-size: 36px; }
.phead h1 { font-size: 24px; font-weight: 700; }
.phead-goal { color: var(--muted); margin: 6px 0 0; white-space: pre-wrap; }
.tabs {
  display: flex; gap: 4px; overflow-x: auto; scrollbar-width: none; margin: 14px -16px 16px; padding: 0 16px;
  border-bottom: 1px solid var(--line); position: sticky; top: calc(56px + env(safe-area-inset-top)); background: var(--bg); z-index: 4;
}
.tabs::-webkit-scrollbar { display: none; }
.tab { padding: 10px 12px; white-space: nowrap; color: var(--muted); border-bottom: 2px solid transparent; font-weight: 500; font-size: 14px; }
.tab.on { color: var(--text); border-bottom-color: var(--pc, var(--accent)); }

/* overview */
.ov { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
@media (max-width: 760px) { .ov { grid-template-columns: 1fr; } }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--r); padding: 14px 16px; }
.card h3 { font-size: 15px; font-weight: 600; margin-bottom: 10px; display: flex; gap: 8px; align-items: center; }
.card h3 a { margin-left: auto; font-size: 13px; color: var(--accent); font-weight: 500; }
.mini { display: flex; flex-direction: column; gap: 8px; }
.mini-row { display: flex; gap: 8px; align-items: center; font-size: 14px; }
.mini-row .t { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.big-pct { display: flex; align-items: baseline; gap: 8px; margin-bottom: 8px; }
.big-pct b { font-size: 32px; font-weight: 700; font-variant-numeric: tabular-nums; }
.big-pct span { color: var(--muted); font-size: 14px; }

/* quick add */
.qadd { display: flex; gap: 8px; margin-bottom: 12px; }
.qadd input { flex: 1; min-width: 0; }
input[type=text], input[type=number], input[type=date], input[type=url], input[type=password], input:not([type]), select, textarea {
  border: 1px solid var(--line); background: var(--surface); border-radius: 10px; padding: 9px 12px; min-height: 42px; width: 100%;
  font-size: 16px; /* no zoom on iOS */
}
textarea { resize: vertical; line-height: 1.55; }
input:focus, select:focus, textarea:focus { outline: 2px solid color-mix(in srgb, var(--accent) 45%, transparent); outline-offset: 0; border-color: var(--accent); }
.bad { border-color: var(--danger) !important; }

/* board */
.seg { display: none; background: var(--surface-2); border-radius: 12px; padding: 4px; gap: 4px; margin-bottom: 12px; }
.seg button { flex: 1; border: 0; background: transparent; border-radius: 9px; padding: 8px 4px; font-weight: 500; color: var(--muted); font-size: 14px; }
.seg button.on { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.board { display: grid; grid-template-columns: repeat(3, minmax(0,1fr)); gap: 12px; align-items: start; }
.col { background: var(--surface-2); border-radius: var(--r); padding: 10px; min-height: 120px; }
.col-h { display: flex; align-items: center; gap: 8px; padding: 2px 4px 10px; font-weight: 600; font-size: 14px; }
.col-h .n { color: var(--faint); font-weight: 500; }
.tasks { display: flex; flex-direction: column; gap: 8px; }
@media (max-width: 720px) {
  .seg { display: flex; }
  .board { grid-template-columns: 1fr; }
  .col { background: transparent; padding: 0; }
  .col .col-h { display: none; }
  .col.hide-m { display: none; }
}
.task {
  background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 10px 10px 10px 8px;
  display: flex; gap: 8px; align-items: flex-start; box-shadow: var(--shadow);
}
.task.p2 { border-left: 3px solid var(--danger); }
.task.p1 { border-left: 3px solid var(--warn); }
.task .body { flex: 1; min-width: 0; cursor: pointer; padding-top: 2px; }
.task .title { word-break: break-word; }
.task.isdone .title { text-decoration: line-through; color: var(--muted); }
.task .tags { display: flex; flex-wrap: wrap; gap: 5px; margin-top: 6px; }
.task .tags:empty { display: none; }
.task .note { font-size: 13px; color: var(--muted); margin-top: 2px; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.chk {
  width: 24px; height: 24px; border-radius: 7px; border: 2px solid var(--faint); background: transparent; flex: none;
  display: grid; place-items: center; font-size: 14px; color: transparent; margin-top: 1px; padding: 0;
}
.chk.on { background: var(--ok); border-color: var(--ok); color: #fff; }
.mv { display: flex; flex-direction: column; gap: 2px; }
.mv button { border: 0; background: transparent; color: var(--faint); width: 30px; height: 26px; border-radius: 7px; font-size: 13px; padding: 0; }
.mv button:hover { background: var(--surface-2); color: var(--text); }

/* milestones */
.ms { display: flex; gap: 12px; align-items: center; }
.ms-date { width: 56px; text-align: center; flex: none; background: var(--surface-2); border-radius: 10px; padding: 6px 0; line-height: 1.15; }
.ms-date b { display: block; font-size: 20px; }
.ms-date span { font-size: 12px; color: var(--muted); }
.ms.isdone .t { text-decoration: line-through; color: var(--muted); }

/* notes */
.notes { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 12px; }
.note-c { background: var(--surface); border: 1px solid var(--line); border-radius: var(--r); padding: 12px 14px; cursor: pointer; min-height: 110px; display: flex; flex-direction: column; gap: 6px; }
.note-c:hover { border-color: var(--faint); }
.note-c b { font-weight: 600; }
.note-c p { margin: 0; font-size: 14px; color: var(--muted); white-space: pre-wrap; display: -webkit-box; -webkit-line-clamp: 5; -webkit-box-orient: vertical; overflow: hidden; word-break: break-word; }
.note-c small { color: var(--faint); margin-top: auto; }

/* money */
.money-sum { display: grid; grid-template-columns: repeat(3, minmax(0,1fr)); gap: 10px; margin-bottom: 14px; }
.amt-in { color: var(--ok); font-variant-numeric: tabular-nums; white-space: nowrap; }
.amt-out { color: var(--danger); font-variant-numeric: tabular-nums; white-space: nowrap; }

/* calendar */
.cal-h { display: flex; align-items: center; gap: 8px; margin-bottom: 10px; }
.cal-h h3 { flex: 1; font-size: 17px; font-weight: 600; }
.cal { display: grid; grid-template-columns: repeat(7, minmax(0,1fr)); gap: 4px; }
.cal .dow { text-align: center; font-size: 12px; color: var(--faint); padding: 4px 0; }
.day {
  aspect-ratio: 1 / .9; border: 1px solid var(--line); background: var(--surface); border-radius: 10px; padding: 4px;
  display: flex; flex-direction: column; align-items: center; gap: 3px; font-size: 14px; min-width: 0;
}
.day.out { opacity: .35; }
.day.today { border-color: var(--accent); color: var(--accent); font-weight: 700; }
.day.sel { background: var(--accent-soft); border-color: var(--accent); }
.dots { display: flex; gap: 3px; flex-wrap: wrap; justify-content: center; }
.dots i { width: 6px; height: 6px; border-radius: 50%; background: var(--accent); }
.dots i.m { background: var(--danger); }
.dots i.d { background: var(--faint); }

/* settings */
.form { display: flex; flex-direction: column; gap: 12px; }
.fld { display: flex; flex-direction: column; gap: 5px; }
.fld > span { font-size: 13px; color: var(--muted); font-weight: 500; }
.two { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.mods { display: flex; flex-direction: column; gap: 8px; }
.mod { display: flex; gap: 12px; align-items: center; padding: 10px 12px; border: 1px solid var(--line); border-radius: 12px; background: var(--surface); cursor: pointer; }
.mod .em { font-size: 22px; }
.mod .main { flex: 1; }
.mod small { display: block; color: var(--muted); font-size: 13px; }
.switch { appearance: none; -webkit-appearance: none; width: 44px; height: 26px; border-radius: 99px; background: var(--line); position: relative; flex: none; cursor: pointer; transition: background .15s; margin: 0; }
.switch::after { content: ""; position: absolute; top: 3px; left: 3px; width: 20px; height: 20px; border-radius: 50%; background: #fff; transition: left .15s; box-shadow: 0 1px 2px rgba(0,0,0,.2); }
.switch:checked { background: var(--ok); }
.switch:checked::after { left: 21px; }
.swatches { display: flex; gap: 8px; flex-wrap: wrap; }
.swatches label { width: 32px; height: 32px; border-radius: 50%; background: var(--c); cursor: pointer; display: grid; place-items: center; }
.swatches input { position: absolute; opacity: 0; pointer-events: none; }
.swatches input:checked + i { width: 12px; height: 12px; border-radius: 50%; background: #fff; }
.segr { display: flex; gap: 6px; flex-wrap: wrap; }
.segr label { position: relative; }
.segr input { position: absolute; opacity: 0; pointer-events: none; }
.segr span { display: inline-block; border: 1px solid var(--line); border-radius: 99px; padding: 6px 12px; font-size: 14px; cursor: pointer; background: var(--surface); }
.segr input:checked + span { background: var(--text); color: var(--bg); border-color: var(--text); }
.segr input:focus-visible + span { outline: 2px solid var(--accent); }

/* sheet */
#sheet { position: fixed; inset: 0; z-index: 20; display: flex; align-items: flex-end; justify-content: center; }
@media (min-width: 640px) { #sheet { align-items: center; } }
.scrim { position: absolute; inset: 0; background: rgba(0,0,0,.4); animation: fade .15s; }
.sheet {
  position: relative; background: var(--bg); width: 100%; max-width: 560px; max-height: calc(100dvh - 40px);
  border-radius: 20px 20px 0 0; display: flex; flex-direction: column; animation: up .2s ease-out;
  padding-bottom: env(safe-area-inset-bottom);
}
@media (min-width: 640px) { .sheet { border-radius: 20px; } }
.sheet-h { display: flex; align-items: center; padding: 14px 12px 6px 20px; }
.sheet-h h2 { flex: 1; font-size: 18px; font-weight: 600; }
.sheet-b { padding: 8px 20px 12px; overflow-y: auto; display: flex; flex-direction: column; gap: 12px; }
.sheet-f { display: flex; gap: 8px; padding: 12px 16px; border-top: 1px solid var(--line); }
.menu-list { display: flex; flex-direction: column; }
.menu-list button { border: 0; background: transparent; text-align: left; padding: 14px 4px; border-top: 1px solid var(--line); font-size: 16px; display: flex; gap: 12px; }
.menu-list button:first-child { border-top: 0; }
@keyframes up { from { transform: translateY(30px); opacity: 0; } }
@keyframes fade { from { opacity: 0; } }

/* login */
.login { max-width: 380px; margin: 12vh auto 0; padding: 0 16px; text-align: center; }
.login .em { font-size: 52px; }
.login h1 { font-size: 24px; margin: 8px 0 6px; }
.login p { color: var(--muted); margin: 0 0 20px; }
.login form { display: flex; flex-direction: column; gap: 10px; }
.login .err { color: var(--danger); font-size: 14px; min-height: 20px; }

#toast {
  position: fixed; left: 50%; bottom: calc(90px + env(safe-area-inset-bottom)); transform: translateX(-50%) translateY(20px);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-size: 14px; z-index: 30;
  opacity: 0; pointer-events: none; transition: .2s; max-width: calc(100vw - 32px);
}
#toast.on { opacity: 1; transform: translateX(-50%) translateY(0); }

.muted { color: var(--muted); }
.small { font-size: 13px; }
.mt { margin-top: 16px; }
.right { text-align: right; }
