:root {
  --bg: #eef0f3;
  --card: #ffffff;
  --ink: #1d2230;
  --muted: #687083;
  --line: #e3e6eb;
  --head: #1d2230;
  --head-ink: #ffffff;
  --accent: #0f766e;
  --accent-soft: #e2f3f1;
  --danger: #c2410c;
  --radius: 22px;
  --shadow: 0 1px 2px rgba(20, 24, 36, .05), 0 6px 20px rgba(20, 24, 36, .05);
  font-family: 'IBM Plex Sans Thai', system-ui, sans-serif;
  color: var(--ink);
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); min-height: 100vh; font-size: 15px; }
svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }
a { color: var(--accent); }

/* Header */
.topbar {
  background: var(--head); color: var(--head-ink);
  display: flex; align-items: center; gap: 16px;
  padding: 18px 28px; position: sticky; top: env(safe-area-inset-top, 0px); z-index: 20;
}
.brand { flex: 1; text-align: center; }
.brand h1 { margin: 0; font-size: 30px; font-weight: 700; letter-spacing: .3px; line-height: 1.1; }
.brand-sub { font-size: 12px; letter-spacing: 3px; text-transform: uppercase; opacity: .6; }
.topbar-right { display: flex; gap: 10px; align-items: center; font-size: 13px; opacity: .9; min-width: 140px; justify-content: flex-end; }
.mode-badge { background: #f59e0b; color: #1d2230; padding: 3px 10px; border-radius: 99px; font-weight: 600; font-size: 12px; }
.menu-btn { display: none; background: none; border: 0; color: inherit; padding: 6px; cursor: pointer; }
.menu-btn svg { width: 24px; height: 24px; }

/* Layout */
.layout { display: grid; grid-template-columns: 230px 1fr; gap: 20px; padding: 20px; align-items: start; }
.card { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); }
.sidebar { padding: 14px 10px; position: sticky; top: 98px; display: flex; flex-direction: column; min-height: calc(100vh - 118px); }
.sidebar ul { list-style: none; margin: 0; padding: 0; display: grid; gap: 4px; }
.sidebar li a {
  display: flex; align-items: center; gap: 12px; padding: 11px 14px; border-radius: 14px;
  color: var(--ink); text-decoration: none; font-weight: 500;
}
.sidebar li a:hover { background: #f3f4f7; }
.sidebar li a.active { background: var(--accent); color: #fff; }
.sidebar .nav-label { font-size: 11px; color: var(--muted); letter-spacing: 1.5px; text-transform: uppercase; padding: 14px 14px 4px; }
.sidebar-foot { margin-top: auto; padding: 10px 4px 2px; border-top: 1px solid var(--line); }
.sidebar-foot a { display: flex; gap: 10px; align-items: center; padding: 10px; font-size: 13px; text-decoration: none; color: var(--muted); border-radius: 12px; }
.sidebar-foot a:hover { background: #f3f4f7; color: var(--ink); }

.panel { padding: 28px 30px; min-height: calc(100vh - 118px); min-width: 0; }
.page-head { display: flex; align-items: flex-end; gap: 16px; flex-wrap: wrap; margin-bottom: 20px; }
.page-head h2 { margin: 0; font-size: 24px; }
.page-head p { margin: 4px 0 0; color: var(--muted); font-size: 14px; }
.page-head .grow { flex: 1; min-width: 200px; }

/* Buttons & inputs */
.btn { font: inherit; border: 1px solid transparent; border-radius: 12px; padding: 9px 16px; cursor: pointer; font-weight: 600; display: inline-flex; gap: 8px; align-items: center; background: #f3f4f7; color: var(--ink); }
.btn.primary { background: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.ghost { background: transparent; border-color: var(--line); }
.btn.danger { color: var(--danger); }
.btn:disabled { opacity: .6; cursor: wait; }
.icon-btn { background: none; border: 0; font-size: 18px; cursor: pointer; color: var(--muted); padding: 4px 8px; }
input, select, textarea { font: inherit; color: var(--ink); width: 100%; border: 1px solid var(--line); border-radius: 10px; padding: 9px 12px; background: #fff; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }
textarea { min-height: 90px; resize: vertical; }

.toolbar { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 16px; }
.toolbar input[type=search] { max-width: 280px; }
.toolbar select { width: auto; min-width: 150px; }

/* Stats */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 14px; margin-bottom: 22px; }
.stat { border: 1px solid var(--line); border-radius: 16px; padding: 16px 18px; }
.stat .label { color: var(--muted); font-size: 13px; }
.stat .value { font-size: 26px; font-weight: 700; margin-top: 4px; font-variant-numeric: tabular-nums; }
.stat .hint { font-size: 12px; color: var(--muted); margin-top: 2px; }
a.stat { text-decoration: none; color: inherit; transition: border-color .15s; }
a.stat:hover { border-color: var(--accent); }

/* Table */
.table-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: 16px; }
table { border-collapse: collapse; width: 100%; font-size: 14px; }
th, td { text-align: left; padding: 11px 14px; border-bottom: 1px solid var(--line); white-space: nowrap; }
th { font-size: 12px; color: var(--muted); font-weight: 600; background: #fafbfc; position: sticky; top: 0; }
tbody tr { cursor: pointer; }
tbody tr:hover { background: #f7f9fa; }
tbody tr:last-child td { border-bottom: 0; }
td.num { text-align: right; font-variant-numeric: tabular-nums; }
th.num { text-align: right; }
td.title { font-weight: 600; max-width: 320px; overflow: hidden; text-overflow: ellipsis; }
.empty { text-align: center; color: var(--muted); padding: 48px 16px; }
.overdue { color: var(--danger); font-weight: 600; }

.badge { display: inline-block; padding: 3px 10px; border-radius: 99px; font-size: 12px; font-weight: 600; background: #eef0f3; color: #3d4455; }
.badge.s0 { background: #eef0f3; color: #3d4455; }
.badge.s1 { background: #e0ecff; color: #1e4fa8; }
.badge.s2 { background: #fff1d6; color: #8a5300; }
.badge.s3 { background: #f1e6ff; color: #6129a8; }
.badge.s4 { background: #dcf5e8; color: #126b3e; }
.badge.s5 { background: #ffe4dc; color: #a33612; }

/* Kanban-ish summary */
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; }
.box { border: 1px solid var(--line); border-radius: 16px; padding: 16px 18px; min-width: 0; }
.box h3 { margin: 0 0 12px; font-size: 16px; }
.list { list-style: none; margin: 0; padding: 0; }
.list li { display: flex; gap: 10px; align-items: center; padding: 9px 0; border-bottom: 1px dashed var(--line); font-size: 14px; }
.list li:last-child { border-bottom: 0; }
.list .when { margin-left: auto; color: var(--muted); font-size: 13px; white-space: nowrap; }
.tag { font-size: 11px; color: var(--muted); border: 1px solid var(--line); border-radius: 6px; padding: 1px 6px; }

/* Chart */
.chart { display: flex; align-items: flex-end; gap: 6px; height: 180px; padding-top: 10px; }
.chart .bar { flex: 1; background: var(--accent); border-radius: 6px 6px 2px 2px; min-height: 2px; position: relative; opacity: .85; }
.chart .bar:hover { opacity: 1; }
.chart .bar span { position: absolute; bottom: -20px; left: 50%; transform: translateX(-50%); font-size: 10px; color: var(--muted); white-space: nowrap; }
.chart-wrap { padding-bottom: 22px; }

/* Tabs */
.tabs { display: flex; gap: 6px; border-bottom: 1px solid var(--line); margin-bottom: 18px; flex-wrap: wrap; }
.tabs button { font: inherit; background: none; border: 0; padding: 10px 14px; cursor: pointer; color: var(--muted); border-bottom: 2px solid transparent; font-weight: 600; }
.tabs button.active { color: var(--accent); border-color: var(--accent); }

/* Form */
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px 16px; }
.form-grid .full { grid-column: 1 / -1; }
.field label { display: block; font-size: 13px; font-weight: 600; margin-bottom: 6px; color: #3d4455; }
.field .req { color: var(--danger); }
.field .help { font-size: 12px; color: var(--muted); margin-top: 4px; }
.lines { width: 100%; }
.lines td, .lines th { padding: 6px; white-space: normal; border: 0; background: none; }
.lines input { padding: 7px 10px; }
.lines .qty { width: 90px; }
.lines .del { width: 36px; }

/* Modal */
.modal-backdrop { position: fixed; inset: 0; background: rgba(20, 24, 36, .45); display: grid; place-items: center; z-index: 50; padding: 16px; }
.modal-backdrop[hidden] { display: none; }
.modal { background: #fff; border-radius: 20px; width: min(720px, 100%); max-height: calc(100vh - 32px); display: flex; flex-direction: column; box-shadow: 0 20px 60px rgba(0,0,0,.25); }
.modal-head { display: flex; align-items: center; padding: 18px 22px; border-bottom: 1px solid var(--line); }
.modal-head h3 { margin: 0; flex: 1; font-size: 18px; }
.modal-body { padding: 20px 22px; overflow-y: auto; }
.modal-foot { display: flex; gap: 10px; padding: 14px 22px; border-top: 1px solid var(--line); }
.spacer { flex: 1; }

.toast { position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%); background: var(--ink); color: #fff; padding: 11px 20px; border-radius: 12px; z-index: 60; font-size: 14px; box-shadow: 0 8px 30px rgba(0,0,0,.2); }
.toast.error { background: var(--danger); }
.loading { color: var(--muted); padding: 40px; text-align: center; }

/* Mobile */
@media (max-width: 860px) {
  .menu-btn { display: block; }
  .topbar { padding: 14px 16px; }
  .brand h1 { font-size: 22px; }
  .topbar-right .today { display: none; }
  .topbar-right { min-width: 0; }
  .layout { grid-template-columns: 1fr; padding: 12px 16px; }
  .sidebar { position: fixed; left: 12px; top: 76px; bottom: 12px; width: 240px; z-index: 30; min-height: 0; transform: translateX(-120%); transition: transform .2s; box-shadow: 0 10px 40px rgba(0,0,0,.2); }
  .sidebar.open { transform: none; }
  .panel { padding: 20px 16px; min-height: 0; }
  .grid-2, .form-grid { grid-template-columns: 1fr; }
  .toolbar input[type=search] { max-width: none; }
}
.list .when.overdue { color: var(--danger); }

.doc-card { display: flex; gap: 16px; align-items: center; flex-wrap: wrap; }
.doc-card .grow { flex: 1; min-width: 220px; }
.doc-card h3 { margin: 0 0 4px; }
.doc-card p { margin: 0; color: var(--muted); font-size: 14px; }
.doc-icon { width: 48px; height: 48px; border-radius: 14px; background: var(--accent-soft); color: var(--accent); display: grid; place-items: center; }
.doc-icon svg { width: 24px; height: 24px; }
.tabs .badge { font-size: 11px; padding: 1px 8px; margin-left: 4px; }
.notice { background: #fff6e5; color: #7a4b00; border-radius: 12px; padding: 10px 14px; font-size: 14px; margin-bottom: 14px; }

.embed { width: 100%; height: calc(100vh - 300px); min-height: 640px; border: 0; background: #fff; display: block; }
.embed-bar { display: flex; gap: 12px; justify-content: space-between; align-items: center; flex-wrap: wrap; font-size: 13px; color: var(--muted); margin: -4px 0 8px; }
.embed-bar a { font-weight: 600; text-decoration: none; }

.seg { display: inline-flex; flex-wrap: wrap; gap: 2px; border: 1px solid var(--line); border-radius: 12px; padding: 3px; background: #fff; }
.seg button { font: inherit; border: 0; background: none; padding: 6px 14px; border-radius: 10px; color: var(--muted); cursor: pointer; font-weight: 500; }
.seg button.on { background: var(--accent); color: #fff; }
.stat .hint .up { color: #127a45; font-weight: 600; }
.stat .hint .down { color: var(--danger); font-weight: 600; }
.shop-bars { list-style: none; margin: 0; padding: 0; display: grid; gap: 14px; }
.shop-bars li { display: grid; grid-template-columns: 110px 1fr auto; gap: 12px; align-items: center; font-size: 14px; }
.shop-bars .name { display: flex; align-items: center; gap: 8px; font-weight: 600; }
.shop-bars .name i { width: 9px; height: 9px; border-radius: 50%; }
.shop-bars .track { background: #f1f3f6; border-radius: 6px; height: 10px; overflow: hidden; }
.shop-bars .track b { display: block; height: 100%; border-radius: 6px; }
.shop-bars .val { text-align: right; font-variant-numeric: tabular-nums; font-weight: 600; min-width: 110px; }
.shop-bars .val small { display: block; font-weight: 400; color: var(--muted); font-size: 12px; }
.source-note { color: var(--muted); font-size: 12px; margin: 12px 0 0; }
.embed.tall { height: calc(100vh - 230px); min-height: 700px; border: 1px solid var(--line); border-radius: 16px; }
@media (max-width: 860px) { .shop-bars li { grid-template-columns: 90px 1fr; } .shop-bars .val { grid-column: 1 / -1; text-align: left; } }

/* ป้ายล็อกอิน */
.user-chip { display: inline-flex; align-items: center; gap: 8px; font-size: 13px; max-width: 320px; }
.user-chip span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; opacity: .9; }
.user-chip button { font: inherit; font-size: 12px; border: 1px solid rgba(255,255,255,.35); background: transparent; color: inherit; border-radius: 999px; padding: 4px 12px; cursor: pointer; white-space: nowrap; }
.user-chip #loginBtn { background: #fff; color: #1d2230; border-color: #fff; font-weight: 600; }
@media (max-width: 640px) { .user-chip span { display: none; } }
