:root {
  --bg: #0e1014; --bg2: #14171d; --card: #1a1e26; --card2: #212631; --line: #2b313d; --line2: #3a4252;
  --text: #eceef3; --muted: #97a0b3; --dim: #6b7385;
  --rec: #ff4757; --rec-bg: rgba(255, 71, 87, .12);
  --next: #ffb224; --next-bg: rgba(255, 178, 36, .11);
  --done: #3ecf8e; --done-bg: rgba(62, 207, 142, .1);
  --brk: #5aa9ff; --brk-bg: rgba(90, 169, 255, .1);
  --skip: #7d8596;
  --radius: 14px;
  --mono: 'JetBrains Mono', ui-monospace, monospace;
  color-scheme: dark;
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--text); }
body { font: 15px/1.5 'IBM Plex Sans Thai', system-ui, sans-serif; -webkit-font-smoothing: antialiased; min-height: 100vh; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
a { color: var(--brk); }
.mono { font-family: var(--mono); font-variant-numeric: tabular-nums; }
.muted { color: var(--muted); }
.dim { color: var(--dim); }
.hide { display: none !important; }

/* ── ปุ่ม ─────────────────────────────────── */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; border: 1px solid var(--line2); background: var(--card2);
  border-radius: 10px; padding: 9px 14px; font-weight: 600; min-height: 42px; white-space: nowrap; transition: transform .06s, background .15s; }
a.btn { color: var(--text); text-decoration: none; }
.btn:active { transform: scale(.97); }
.btn:hover { background: #2a303d; }
.btn.sm { min-height: 34px; padding: 5px 10px; font-size: 13px; border-radius: 8px; }
.btn.big { min-height: 56px; font-size: 17px; padding: 12px 20px; border-radius: 12px; }
.btn.rec { background: var(--rec); border-color: var(--rec); color: #fff; }
.btn.go { background: var(--done); border-color: var(--done); color: #0b1a12; }
.btn.warn { background: var(--next); border-color: var(--next); color: #1d1300; }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--rec); border-color: rgba(255, 71, 87, .4); background: transparent; }
.btn:disabled { opacity: .45; cursor: default; }
.btn.block { width: 100%; }

input, select, textarea { background: var(--bg2); border: 1px solid var(--line2); border-radius: 10px; padding: 10px 12px; width: 100%; min-height: 44px; }
textarea { min-height: 70px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--brk); outline-offset: -1px; }
label.f { display: block; font-size: 13px; color: var(--muted); margin: 10px 0 4px; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.row3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }

/* ── หน้าแรก ──────────────────────────────── */
.home { max-width: 560px; margin: 0 auto; padding: 32px 16px 60px; }
.brand { display: flex; align-items: center; gap: 12px; margin-bottom: 6px; }
.brand-mark { width: 44px; height: 44px; border-radius: 12px; background: var(--rec); display: grid; place-items: center; font-weight: 700; color: #fff; font-size: 20px; }
.home h1 { font-size: 26px; margin: 0; letter-spacing: -.01em; }
.home .lead { color: var(--muted); margin: 4px 0 24px; }
.panel { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px; margin-bottom: 16px; }
.panel h2 { font-size: 17px; margin: 0 0 4px; }
.join-row { display: flex; gap: 8px; margin-top: 10px; }
.join-row input { text-transform: uppercase; letter-spacing: .2em; font-family: var(--mono); font-size: 20px; text-align: center; }
.recent a { display: flex; justify-content: space-between; padding: 10px 0; border-top: 1px solid var(--line); text-decoration: none; color: var(--text); }
.recent a:first-child { border-top: 0; }

/* ── หัวกอง ───────────────────────────────── */
.top { position: sticky; top: 0; z-index: 20; background: rgba(14, 16, 20, .92); backdrop-filter: blur(10px); border-bottom: 1px solid var(--line);
  padding: calc(8px + env(safe-area-inset-top)) 16px 8px; }
.top-in { max-width: 1400px; margin: 0 auto; display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.top .title { flex: 1 1 200px; min-width: 0; }
.top .title b { font-size: 17px; display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.top .title span { font-size: 12.5px; color: var(--muted); display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.clock { font-family: var(--mono); font-size: 22px; font-weight: 700; letter-spacing: .02em; }
.tools { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.pill { display: inline-flex; align-items: center; gap: 6px; padding: 6px 10px; border-radius: 999px; background: var(--card2); border: 1px solid var(--line2); font-size: 13px; font-weight: 600; min-height: 34px; }
.pill .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--done); }
.pill.off .dot { background: var(--rec); animation: blink 1s infinite; }
.pill.ad { background: var(--next); color: #1d1300; border-color: var(--next); }

.stats { max-width: 1400px; margin: 8px auto 0; display: flex; gap: 14px; flex-wrap: wrap; font-size: 13px; color: var(--muted); }
.stats b { color: var(--text); font-weight: 600; }
.stats .late { color: var(--rec); }
.stats .early { color: var(--done); }

/* ── เลย์เอาต์หลัก ────────────────────────── */
.main { max-width: 1400px; margin: 0 auto; padding: 14px 16px 90px; display: grid; gap: 16px; }
@media (min-width: 980px) {
  .main { grid-template-columns: 420px 1fr; align-items: start; }
  .side { position: sticky; top: 110px; max-height: calc(100vh - 124px); overflow: auto; padding-bottom: 8px; }
}

/* ── การ์ด NOW / NEXT ─────────────────────── */
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; margin-bottom: 14px; }
.card.now { border-color: var(--rec); background: linear-gradient(180deg, var(--rec-bg), transparent 70%), var(--card); }
.card.standby { border-style: dashed; }
.card.next { border-color: rgba(255, 178, 36, .55); background: linear-gradient(180deg, var(--next-bg), transparent 60%), var(--card); }
.card.next.brk { border-color: rgba(90, 169, 255, .55); background: linear-gradient(180deg, var(--brk-bg), transparent 60%), var(--card); }
.tag { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; font-weight: 700; letter-spacing: .08em; text-transform: uppercase; }
.tag.rec { color: var(--rec); }
.tag.rec::before { content: ''; width: 9px; height: 9px; border-radius: 50%; background: var(--rec); animation: blink 1.2s infinite; }
.tag.nx { color: var(--next); }
.tag.bk { color: var(--brk); }
@keyframes blink { 50% { opacity: .25; } }
.sc-big { font-size: 30px; font-weight: 700; line-height: 1.15; margin: 6px 0 2px; }
.sc-big small { font-size: 15px; font-weight: 500; color: var(--muted); margin-left: 6px; }
.meta { display: flex; gap: 6px; flex-wrap: wrap; margin: 6px 0; }
.chip { font-size: 12px; font-weight: 600; padding: 2px 8px; border-radius: 6px; background: var(--card2); border: 1px solid var(--line2); color: var(--muted); }
.chip.ie { color: var(--text); }
.chip.n { color: #c6b4ff; }
.chip.d { color: #ffd479; }
.desc { color: var(--text); white-space: pre-line; }
.desc.clamp { display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.cast { font-size: 13.5px; color: var(--muted); margin-top: 6px; }
.cast b { color: var(--text); font-weight: 600; }
.timer { display: flex; align-items: baseline; gap: 10px; margin: 12px 0 6px; }
.timer .t { font-family: var(--mono); font-size: 48px; font-weight: 700; line-height: 1; font-variant-numeric: tabular-nums; }
.timer .p { color: var(--muted); font-size: 14px; }
.timer.over .t { color: var(--rec); }
.bar { height: 6px; background: var(--card2); border-radius: 99px; overflow: hidden; }
.bar i { display: block; height: 100%; background: var(--done); border-radius: 99px; transition: width .5s; }
.bar.over i { background: var(--rec); }
.actions { display: flex; gap: 8px; margin-top: 14px; flex-wrap: wrap; }
.actions .btn.big { flex: 1 1 160px; }

.prep { margin-top: 12px; display: grid; gap: 6px; }
.prep-row { display: grid; grid-template-columns: 86px 1fr auto; gap: 8px; align-items: start; padding: 8px 10px; border-radius: 10px; background: var(--bg2); border: 1px solid var(--line); font-size: 13.5px; }
.prep-row .d { font-weight: 700; font-size: 12.5px; color: var(--muted); padding-top: 1px; }
.prep-row .x { white-space: pre-line; }
.prep-row.mine { border-color: var(--next); background: var(--next-bg); }
.prep-row.mine .d { color: var(--next); }
.ack { font-size: 12px; font-weight: 700; white-space: nowrap; padding-top: 1px; }
.ack.ok { color: var(--done); }
.ack.wait { color: var(--dim); }

/* ── แท็บ ─────────────────────────────────── */
.tabs { display: flex; gap: 4px; background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 4px; margin-bottom: 12px; overflow-x: auto; }
.tabs button { flex: 1; border: 0; background: transparent; padding: 9px 12px; border-radius: 9px; font-weight: 600; color: var(--muted); white-space: nowrap; min-height: 40px; }
.tabs button.on { background: var(--card2); color: var(--text); }
.tabs .badge { background: var(--rec); color: #fff; border-radius: 99px; font-size: 11px; padding: 0 6px; margin-left: 4px; }

/* ── รายการเบรคดาว ────────────────────────── */
.ghead { font-size: 12.5px; font-weight: 700; letter-spacing: .06em; color: var(--muted); padding: 14px 4px 6px; display: flex; align-items: center; gap: 8px; }
.ghead::after { content: ''; flex: 1; height: 1px; background: var(--line); }
.row { display: grid; grid-template-columns: 58px 1fr auto; gap: 10px; background: var(--card); border: 1px solid var(--line); border-radius: 12px;
  padding: 10px 12px; margin-bottom: 6px; position: relative; }
.row.shooting { border-color: var(--rec); background: linear-gradient(90deg, var(--rec-bg), transparent 60%), var(--card); }
.row.is-next { border-color: rgba(255, 178, 36, .6); }
.row.done { opacity: .62; }
.row.done .rsc { text-decoration: none; }
.row.skip { opacity: .4; }
.row.skip .rsc { text-decoration: line-through; }
.row.brk { background: var(--brk-bg); border-color: rgba(90, 169, 255, .3); }
.row .when { font-family: var(--mono); font-size: 13px; line-height: 1.3; }
.row .when b { display: block; font-size: 15px; }
.row .when .dur { color: var(--muted); font-size: 11.5px; }
.row .when .dur.late { color: var(--rec); }
.row .when .dur.early { color: var(--done); }
.row .body { min-width: 0; cursor: pointer; }
.row .rsc { font-weight: 700; font-size: 16px; }
.row .rsc .st { font-size: 11px; font-weight: 700; letter-spacing: .06em; margin-left: 6px; vertical-align: 2px; }
.row .setn { font-size: 13px; color: var(--muted); }
.row .ops { display: flex; flex-direction: column; gap: 4px; align-items: flex-end; }
.handle { cursor: grab; touch-action: none; width: 40px; height: 34px; display: grid; place-items: center; color: var(--dim); border-radius: 8px; font-size: 18px; user-select: none; }
.handle:active { cursor: grabbing; background: var(--card2); }
.sortable-ghost { opacity: .35; }
.sortable-chosen { box-shadow: 0 10px 30px rgba(0, 0, 0, .5); }
.prep-mini { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 6px; }
.prep-mini .chip.mine { color: var(--next); border-color: var(--next); }
.expand { margin-top: 8px; }
.add-bar { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 12px; }

/* ── ตารางเวลา ────────────────────────────── */
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 8px; margin-bottom: 12px; }
.kpi { background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 12px; }
.kpi .k { font-size: 12px; color: var(--muted); }
.kpi .v { font-family: var(--mono); font-size: 22px; font-weight: 700; }
table.tt { width: 100%; border-collapse: collapse; font-size: 13.5px; }
.tt th { text-align: left; color: var(--muted); font-weight: 600; font-size: 12px; padding: 8px 6px; border-bottom: 1px solid var(--line); }
.tt td { padding: 8px 6px; border-bottom: 1px solid var(--line); vertical-align: top; }
.tt td.n { font-family: var(--mono); text-align: right; white-space: nowrap; }
.tt .late { color: var(--rec); }
.tt .early { color: var(--done); }
.scroll-x { overflow-x: auto; background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 4px 10px; }

.feed div { padding: 8px 0; border-bottom: 1px solid var(--line); font-size: 14px; display: flex; gap: 10px; }
.feed .ts { font-family: var(--mono); color: var(--dim); font-size: 12.5px; flex: none; padding-top: 1px; }

.alert-item { background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 12px; margin-bottom: 8px; }
.alert-item.unacked { border-color: var(--next); }
.alert-item .x { white-space: pre-line; margin-top: 4px; }

/* ── โมดัล ────────────────────────────────── */
.modal-bg { position: fixed; inset: 0; background: rgba(4, 5, 8, .72); z-index: 100; display: grid; place-items: center; padding: 16px; animation: fade .15s; }
.modal { background: var(--card); border: 1px solid var(--line2); border-radius: 18px; width: min(560px, 100%); max-height: calc(100vh - 32px); overflow: auto; padding: 20px; }
.modal h3 { margin: 0 0 6px; font-size: 19px; }
.modal .foot { display: flex; gap: 8px; justify-content: flex-end; margin-top: 18px; flex-wrap: wrap; }
@keyframes fade { from { opacity: 0; } }
.access-opt { display: flex; gap: 10px; align-items: flex-start; padding: 10px 12px; border: 1px solid var(--line2); border-radius: 12px; margin-top: 6px; cursor: pointer; font-size: 14px; }
.access-opt input { width: auto; min-height: 0; margin-top: 4px; }
.access-opt:has(input:checked) { border-color: var(--next); background: var(--next-bg); }
.dept-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; margin-top: 8px; }
.dept-grid button { border: 1px solid var(--line2); background: var(--bg2); border-radius: 12px; padding: 12px; text-align: left; font-weight: 600; min-height: 52px; }
.dept-grid button.on { border-color: var(--next); background: var(--next-bg); color: var(--next); }

.popup { border: 2px solid var(--next); box-shadow: 0 0 0 6px rgba(255, 178, 36, .15); }
.popup.swap { border-color: var(--rec); box-shadow: 0 0 0 6px rgba(255, 71, 87, .18); }
.popup .big-t { font-size: 22px; font-weight: 700; line-height: 1.3; margin: 4px 0 10px; }
.popup .mine-box { background: var(--bg2); border: 1px solid var(--line2); border-radius: 12px; padding: 14px; font-size: 17px; white-space: pre-line; }

.qr-wrap { background: #fff; border-radius: 14px; padding: 14px; width: min(320px, 100%); margin: 12px auto; }
.qr-wrap svg { width: 100%; height: auto; display: block; }
.code-big { font-family: var(--mono); font-size: 34px; font-weight: 700; letter-spacing: .25em; text-align: center; }

#toasts { position: fixed; left: 50%; bottom: calc(16px + env(safe-area-inset-bottom)); transform: translateX(-50%); z-index: 200; display: grid; gap: 8px; width: min(420px, calc(100% - 32px)); }
.toast { background: #2b3140; border: 1px solid var(--line2); border-radius: 12px; padding: 12px 14px; font-weight: 600; box-shadow: 0 10px 30px rgba(0, 0, 0, .4); animation: fade .2s; }
.toast.err { background: #3a1c21; border-color: var(--rec); }
.toast.ok { background: #16301f; border-color: var(--done); }

.empty { text-align: center; color: var(--muted); padding: 30px 10px; }

@media (max-width: 560px) {
  .clock { font-size: 18px; }
  .sc-big { font-size: 26px; }
  .timer .t { font-size: 42px; }
  .row { grid-template-columns: 50px 1fr auto; gap: 8px; padding: 10px; }
  .row2, .row3 { grid-template-columns: 1fr 1fr; }
  .prep-row { grid-template-columns: 70px 1fr; }
  .prep-row .ack { grid-column: 2; }
}

/* ── พิมพ์เบรคดาว (มี QR) ─────────────────── */
#print-sheet { display: none; }
@media print {
  @page { size: A4 landscape; margin: 10mm; }
  body { background: #fff; color: #000; font-size: 10.5px; }
  #app, #toasts, .modal-bg { display: none !important; }
  #print-sheet { display: block; }
  .ps-head { display: flex; gap: 16px; align-items: flex-start; border-bottom: 2px solid #000; padding-bottom: 8px; margin-bottom: 8px; }
  .ps-head .qr { width: 110px; flex: none; }
  .ps-head .qr svg { width: 100%; height: auto; }
  .ps-head h1 { margin: 0; font-size: 22px; }
  .ps-head .info { flex: 1; white-space: pre-line; }
  .ps-table { width: 100%; border-collapse: collapse; }
  .ps-table th, .ps-table td { border: 1px solid #555; padding: 3px 5px; vertical-align: top; text-align: left; }
  .ps-table th { background: #ddd; }
  .ps-table tr.brk td { background: #eee; font-weight: 700; text-align: center; }
  .ps-table td.desc { white-space: pre-line; }
}
