:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --ink: #0f172a;
  --muted: #64748b;
  --line: #e2e8f0;
  --teal: #0d9488;
  --blue: #2563eb;
  --purple: #7c3aed;
  --green: #16a34a;
  --amber: #d97706;
  --slate: #64748b;
  --shadow: 0 1px 3px rgba(15,23,42,.06), 0 1px 2px rgba(15,23,42,.04);
  --radius: 12px;
}
* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: "Prompt", "Sukhumvit Set", "Noto Sans Thai", system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--ink);
  font-size: 14px;
  line-height: 1.5;
}
.ic { width: 18px; height: 18px; flex: none; vertical-align: -3px; }

/* layout */
.app { display: flex; min-height: 100vh; }
.side {
  width: 240px; background: var(--panel); border-right: 1px solid var(--line);
  padding: 20px 14px; position: sticky; top: 0; height: 100vh; flex: none;
  display: flex; flex-direction: column; gap: 4px;
}
.brand { display: flex; align-items: center; gap: 10px; padding: 4px 8px 18px; }
.brand b { font-size: 15px; letter-spacing: -.2px; }
.brand small { display: block; color: var(--muted); font-weight: 400; font-size: 11px; }
.logo {
  width: 34px; height: 34px; border-radius: 9px; flex: none;
  background: linear-gradient(135deg, var(--teal), var(--blue));
  display: grid; place-items: center; color: #fff;
}
.nav a {
  display: flex; align-items: center; gap: 10px; padding: 9px 12px;
  border-radius: 9px; color: var(--muted); text-decoration: none; font-weight: 500;
  cursor: pointer; transition: .12s;
}
.nav a:hover { background: var(--bg); color: var(--ink); }
.nav a.active { background: #eef2ff; color: var(--blue); }
.side-foot { margin-top: auto; color: var(--muted); font-size: 11px; padding: 8px; }

.main { flex: 1; min-width: 0; padding: 24px 28px 60px; }
.head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 18px; gap: 16px; flex-wrap: wrap; }
.head h1 { font-size: 20px; margin: 0; letter-spacing: -.3px; }
.head p { margin: 2px 0 0; color: var(--muted); font-size: 13px; }

/* buttons */
.btn {
  display: inline-flex; align-items: center; gap: 7px; border: 1px solid var(--line);
  background: var(--panel); color: var(--ink); padding: 8px 13px; border-radius: 9px;
  font-weight: 600; font-size: 13px; cursor: pointer; transition: .12s; font-family: inherit;
}
.btn:hover { border-color: #cbd5e1; }
.btn.primary { background: var(--blue); border-color: var(--blue); color: #fff; }
.btn.primary:hover { background: #1d4ed8; }
.btn.ghost { border-color: transparent; background: transparent; color: var(--muted); padding: 6px 8px; }
.btn.ghost:hover { background: var(--bg); color: var(--ink); }
.btn.sm { padding: 5px 9px; font-size: 12px; }
.btn.danger:hover { border-color: #fca5a5; color: #dc2626; }

/* cards / kpis */
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 14px; margin-bottom: 22px; }
.kpi { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); }
.kpi .label { color: var(--muted); font-size: 12px; font-weight: 500; }
.kpi .val { font-size: 24px; font-weight: 700; margin-top: 6px; letter-spacing: -.5px; }
.kpi .sub { color: var(--muted); font-size: 12px; margin-top: 2px; }

.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; }
@media (max-width: 900px){ .grid2 { grid-template-columns: 1fr; } }

.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); margin-bottom: 18px; }
.card h3 { margin: 0; padding: 14px 18px; border-bottom: 1px solid var(--line); font-size: 14px; }
.card .body { padding: 14px 18px; }

/* table */
.tablewrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { padding: 9px 12px; text-align: left; border-bottom: 1px solid var(--line); white-space: nowrap; }
th { color: var(--muted); font-weight: 600; font-size: 11px; text-transform: uppercase; letter-spacing: .4px; position: sticky; top: 0; background: var(--panel); }
tr:hover td { background: #fafbfc; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
.row-actions { display: flex; gap: 4px; opacity: 0; }
tr:hover .row-actions { opacity: 1; }
tr.grp td { background: #f1f5f9; border-top: 2px solid #cbd5e1; padding: 8px 12px; position: sticky; }
tr.grp:hover td { background: #f1f5f9; }
.grp-label { font-weight: 700; font-size: 13px; color: var(--ink); }
.grp-meta { float: right; color: var(--muted); font-size: 12px; font-weight: 500; }
.grp-meta b { color: var(--ink); }

/* inline-editable cells (spreadsheet style) */
td.cell { padding: 3px 5px; }
td.drag { width: 26px; text-align: center; color: #cbd5e1; cursor: grab; padding: 3px 2px; }
td.drag:active { cursor: grabbing; }
td.drag .ic { width: 15px; height: 15px; }
tr[data-id]:hover td.drag { color: var(--muted); }
tr.row-dragging { opacity: .4; }
tr.row-dragging td { background: #eff6ff !important; }
.cell-input, .cell-sel {
  width: 100%; min-width: 70px; border: 1px solid transparent; background: transparent;
  padding: 5px 6px; border-radius: 6px; font: inherit; color: inherit;
  font-variant-numeric: tabular-nums;
}
.cell-input:hover, .cell-sel:hover { border-color: var(--line); background: #fff; }
.cell-input:focus, .cell-sel:focus { border-color: var(--blue); background: #fff; outline: none; box-shadow: 0 0 0 2px rgba(37,99,235,.12); }
.cell-sel.cat { min-width: 170px; }
.cell-input.num { text-align: right; }
.cell-input.amt { font-weight: 600; }
.cell-input.tiny { min-width: 40px; width: 48px; text-align: center; color: var(--muted); }
.cell-input.rate { min-width: 50px; width: 64px; }
.cell-note { width: 100%; border: 1px dashed transparent; background: transparent; font-size: 11px; color: var(--muted); padding: 2px 6px; border-radius: 5px; font-family: inherit; }
.cell-note:hover { border-color: var(--line); }
.cell-note:focus { border-color: var(--blue); background: #fff; outline: none; color: var(--ink); }
.cell-sel.st-done { color: #15803d; font-weight: 600; }
.cell-sel.st-pending { color: #b45309; font-weight: 600; }
.cell-sel.st-draft { color: #475569; }
tr.row-saved td { background: #ecfdf5 !important; transition: background .1s; }
tr[data-id] td { transition: background .6s; }
.save-flash { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%) translateY(8px); background: var(--green); color: #fff; padding: 7px 14px; border-radius: 8px; font-size: 12px; font-weight: 600; opacity: 0; transition: .18s; z-index: 80; pointer-events: none; }
.save-flash.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* pills */
.pill { display: inline-flex; align-items: center; gap: 5px; padding: 2px 9px; border-radius: 999px; font-size: 12px; font-weight: 600; }
.pill.done { background: #dcfce7; color: #15803d; }
.pill.pending { background: #fef3c7; color: #b45309; }
.pill.draft { background: #e2e8f0; color: #475569; }
.co { display: inline-flex; align-items: center; gap: 6px; }
.dot { width: 9px; height: 9px; border-radius: 50%; flex: none; }
.tag { display:inline-block; padding: 1px 8px; border-radius: 6px; background: var(--bg); border: 1px solid var(--line); font-size: 11px; color: var(--muted); }

/* filters */
.filters { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-bottom: 14px; }
select, input[type=text], input[type=number] {
  font-family: inherit; font-size: 13px; padding: 7px 10px; border: 1px solid var(--line);
  border-radius: 8px; background: var(--panel); color: var(--ink); min-width: 0;
}
select:focus, input:focus { outline: none; border-color: var(--blue); }
label.fld { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); font-weight: 500; }
label.fld span { padding-left: 2px; }

/* modal */
.modal-bg { position: fixed; inset: 0; background: rgba(15,23,42,.45); display: none; align-items: flex-start; justify-content: center; padding: 40px 16px; z-index: 50; overflow: auto; }
.modal-bg.open { display: flex; }
.modal { background: var(--panel); border-radius: 14px; width: 560px; max-width: 100%; box-shadow: 0 20px 50px rgba(15,23,42,.25); }
.modal h2 { margin: 0; padding: 16px 20px; border-bottom: 1px solid var(--line); font-size: 16px; display: flex; justify-content: space-between; align-items: center; }
.modal .form { padding: 18px 20px; display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.modal .form .full { grid-column: 1 / -1; }
.modal .foot { padding: 14px 20px; border-top: 1px solid var(--line); display: flex; justify-content: space-between; align-items: center; gap: 10px; }
.calc-hint { font-size: 12px; color: var(--teal); font-weight: 600; }

.ytabs { display: inline-flex; gap: 4px; background: var(--bg); border: 1px solid var(--line); padding: 4px; border-radius: 10px; }
.ytab { border: none; background: transparent; padding: 6px 16px; border-radius: 7px; font: inherit; font-weight: 600; font-size: 13px; color: var(--muted); cursor: pointer; transition: .12s; }
.ytab:hover { color: var(--ink); }
.ytab.active { background: var(--panel); color: var(--blue); box-shadow: var(--shadow); }

.empty { text-align: center; color: var(--muted); padding: 40px; }
.bar { height: 8px; border-radius: 999px; background: var(--bg); overflow: hidden; }
.bar > i { display: block; height: 100%; border-radius: 999px; }
.legend { display: flex; gap: 14px; flex-wrap: wrap; font-size: 12px; color: var(--muted); margin-top: 6px; }
.toast { position: fixed; bottom: 20px; right: 20px; background: var(--ink); color: #fff; padding: 11px 16px; border-radius: 10px; font-size: 13px; opacity: 0; transform: translateY(8px); transition: .2s; z-index: 99; }
.toast.show { opacity: 1; transform: none; }
