:root {
  --bg: #f6f6f3;
  --surface: #ffffff;
  --surface-2: #f0f0ec;
  --ink: #1d2126;
  --muted: #646a72;
  --line: #dcdcd6;
  --line-strong: #bfc0b8;
  --accent: #2544c7;
  --accent-soft: #e7ebfa;
  --danger: #b3261e;
  --danger-soft: #fbe9e7;
  --warn: #8a5a00;
  --warn-soft: #fbf1d9;
  --ok: #1c6b43;
  --ok-soft: #e1f2e8;
  --review: #5b3aa8;
  --review-soft: #eee8f9;
  --idle-soft: #eceded;
  --radius: 6px;
  --control-h: 34px;
}

* { box-sizing: border-box; }

html, body { margin: 0; }

body {
  background: var(--bg);
  color: var(--ink);
  font-family: "Noto Sans Thai", "Leelawadee UI", Tahoma, sans-serif;
  font-size: 14px;
  line-height: 1.45;
}

h1, h2, h3, p { margin: 0; }

button, input, select, textarea { font: inherit; color: inherit; }

button {
  min-height: var(--control-h);
  padding: 0 12px;
  background: var(--surface);
  border: 1px solid var(--line-strong);
  border-radius: var(--radius);
  cursor: pointer;
}
button:hover { background: var(--surface-2); }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.primary:hover { background: #1c37a6; }
button.danger { color: var(--danger); border-color: #e0b3af; }
button.danger:hover { background: var(--danger-soft); }
button.link {
  min-height: 0;
  padding: 2px 0;
  background: none;
  border: 0;
  color: var(--accent);
  text-align: left;
}
button.link:hover { text-decoration: underline; background: none; }

input, select {
  width: 100%;
  min-height: var(--control-h);
  padding: 4px 8px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}
input:hover, select:hover { border-color: var(--line-strong); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
input:disabled { background: transparent; border-color: transparent; color: var(--muted); }
input[type="date"]:disabled::-webkit-datetime-edit { opacity: 0; }

label.field { display: grid; gap: 3px; min-width: 0; color: var(--muted); font-size: 12px; }
label.field > * { color: var(--ink); font-size: 14px; }

/* ---------- ส่วนบน ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  gap: 24px;
  padding: 0 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: baseline; gap: 10px; flex: none; padding: 10px 0; }
.brand h1 { font-size: 18px; font-weight: 700; }
.brand p { color: var(--muted); font-size: 12px; white-space: nowrap; }

.tabs { display: flex; gap: 2px; min-width: 0; overflow-x: auto; scrollbar-width: none; }
.tabs::-webkit-scrollbar { display: none; }
.tabs button {
  flex: none;
  min-height: 48px;
  padding: 0 12px;
  background: none;
  border: 0;
  border-bottom: 3px solid transparent;
  border-radius: 0;
  color: var(--muted);
}
.tabs button:hover { color: var(--ink); }
.tabs button[aria-selected="true"] { color: var(--accent); border-bottom-color: var(--accent); font-weight: 600; }

.banner {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 14px;
  padding: 8px 20px;
  background: var(--warn-soft);
  border-bottom: 1px solid #e8d8ad;
  color: #5e3e00;
}
.banner.is-error { background: var(--danger-soft); border-color: #eec7c3; color: #7e1b15; }
.banner button { min-height: 30px; }

main { padding: 16px 20px 40px; }

/* ---------- ตัวกรอง ---------- */
.filters {
  display: grid;
  grid-template-columns: minmax(180px, 2fr) repeat(3, minmax(120px, 1fr)) auto;
  gap: 10px;
  align-items: end;
  margin-bottom: 12px;
}
.filters .count { padding-bottom: 7px; color: var(--muted); font-size: 12px; white-space: nowrap; }

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

/* ---------- ตารางงาน ---------- */
.table-wrap {
  overflow: auto;
  max-height: calc(100vh - 190px);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}
.task-table { width: 100%; min-width: 1800px; border-collapse: separate; border-spacing: 0; }
.task-table th {
  position: sticky;
  top: 0;
  z-index: 2;
  padding: 8px;
  background: var(--surface-2);
  border-bottom: 1px solid var(--line-strong);
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
  text-align: left;
  white-space: nowrap;
}
.task-table td { padding: 3px; border-bottom: 1px solid var(--line); vertical-align: middle; }
.task-table tr:last-child td { border-bottom: 0; }
.task-table td input, .task-table td select { border-color: transparent; background-color: transparent; }
.task-table td input:hover, .task-table td select:hover { border-color: var(--line); }
.task-table td input:focus, .task-table td select:focus { background: var(--surface); }

.task-table .c-title { width: 280px; }
.task-table .c-date { width: 136px; }
.task-table .c-name { width: 156px; }
.task-table .c-project { width: 210px; }
.task-table .c-status { width: 132px; }
.task-table .c-left { width: 78px; text-align: right; }
.task-table .c-progress { width: 168px; }
.task-table .c-diff { width: 72px; }
.task-table .c-note { width: 210px; }
.task-table .c-del { width: 58px; }

td.c-left { padding-right: 10px; font-variant-numeric: tabular-nums; white-space: nowrap; }
.is-late { color: var(--danger); font-weight: 600; }

tr.is-done .t-title, tr.is-done .c-note input { text-decoration: line-through; }
tr.is-done td { background: #fafaf8; }
tr.is-done input, tr.is-done select, tr.is-done .c-left { color: var(--muted); }
tr.is-rejected input.t-title { color: var(--muted); text-decoration: line-through; }
tr.blank-row td { background: var(--accent-soft); }
tr.blank-row input::placeholder { color: #47558f; }

.progress-cell { display: grid; grid-template-columns: 1fr 58px; gap: 6px; align-items: center; padding-left: 6px; }
.progress-cell input { padding-right: 2px; text-align: right; font-variant-numeric: tabular-nums; }
.bar { height: 8px; overflow: hidden; background: var(--idle-soft); border-radius: 4px; }
.bar > span { display: block; height: 100%; background: var(--accent); }
.is-done .bar > span, .bar.is-complete > span { background: var(--ok); }

.del { min-height: 30px; padding: 0 8px; color: var(--muted); }

/* ---------- ป้ายสถานะ/ความสำคัญ ---------- */
.s-ยังไม่เริ่ม { background-color: var(--idle-soft) !important; }
.s-กำลังทำ { background-color: var(--accent-soft) !important; color: #1a2f8d !important; }
.s-ตรวจสอบ { background-color: var(--review-soft) !important; color: var(--review) !important; }
.s-แก้ไข { background-color: var(--warn-soft) !important; color: var(--warn) !important; }
.s-เสร็จแล้ว { background-color: var(--ok-soft) !important; color: var(--ok) !important; }
.s-ไม่อนุมัติ { background-color: var(--danger-soft) !important; color: var(--danger) !important; }

.tag {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-size: 12px;
  white-space: nowrap;
}
.dot { width: 9px; height: 9px; flex: none; border-radius: 50%; background: #9aa0a6; }
.p-ด่วนมาก { background: var(--danger); }
.p-ด่วน { background: #d9730d; }
.p-ปกติ { background: var(--accent); }
.p-ไม่รีบ { background: #9aa0a6; }

/* ---------- ลำดับความสำคัญ ---------- */
.quad-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
.quad { min-width: 0; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); }
.quad-head { display: flex; align-items: center; gap: 14px; padding: 12px 14px; border-bottom: 1px solid var(--line); }
.quad-head h2 { font-size: 16px; }
.quad-head p { color: var(--muted); font-size: 12px; }
.quad-head .quad-count { margin-left: auto; text-align: right; font-size: 12px; color: var(--muted); }
.quad-head .quad-count b { display: block; color: var(--ink); font-size: 22px; line-height: 1.1; font-variant-numeric: tabular-nums; }
.ring { position: relative; width: 64px; height: 64px; flex: none; }
.ring svg { display: block; width: 100%; height: 100%; transform: rotate(-90deg); }
.ring b { position: absolute; inset: 0; display: grid; place-items: center; font-size: 13px; font-variant-numeric: tabular-nums; }
.ring .track { fill: none; stroke: var(--idle-soft); stroke-width: 8; }
.ring .value { fill: none; stroke: var(--accent); stroke-width: 8; }
.quad[data-q="do"] .ring .value { stroke: var(--danger); }

.quad-list { margin: 0; padding: 0; list-style: none; }
.quad-list li {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 96px 70px 72px 40px;
  gap: 8px;
  align-items: center;
  padding: 7px 14px;
  border-bottom: 1px solid var(--line);
  font-size: 13px;
}
.quad-list li:last-child { border-bottom: 0; }
.quad-list .title { overflow-wrap: anywhere; }
.quad-list .st { color: var(--muted); }
.quad-list .left { text-align: right; font-variant-numeric: tabular-nums; }

/* ---------- ปฏิทิน ---------- */
.cal-head { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-bottom: 10px; }
.cal-head h2 { min-width: 170px; font-size: 18px; }
.cal-grid {
  display: grid;
  grid-template-columns: repeat(7, minmax(0, 1fr));
  overflow: hidden;
  background: var(--line);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  gap: 1px;
}
.cal-dow { padding: 6px 8px; background: var(--surface-2); color: var(--muted); font-size: 12px; font-weight: 600; }
.cal-day { display: flex; flex-direction: column; gap: 3px; min-width: 0; min-height: 118px; padding: 6px; background: var(--surface); }
.cal-day.is-other { background: #fafaf8; }
.cal-day.is-other .cal-date { color: #a1a4a8; }
.cal-date { font-size: 12px; font-variant-numeric: tabular-nums; }
.cal-day.is-today { box-shadow: inset 0 0 0 2px var(--accent); }
.cal-day.is-today .cal-date { color: var(--accent); font-weight: 700; }
.cal-task {
  display: flex;
  align-items: flex-start;
  gap: 5px;
  padding: 2px 5px;
  background: var(--surface-2);
  border-radius: 4px;
  font-size: 12px;
  overflow-wrap: anywhere;
}
.cal-task .dot { margin-top: 5px; }
.cal-task.is-done { color: var(--muted); text-decoration: line-through; }
.cal-more { font-size: 12px; }
.cal-list { display: none; }

/* ---------- บอร์ดสถานะ ---------- */
.board { display: flex; gap: 10px; overflow-x: auto; padding-bottom: 8px; align-items: flex-start; }
.column { flex: 0 0 256px; min-height: 240px; background: var(--surface-2); border: 1px solid var(--line); border-radius: var(--radius); }
.column.is-over { border-color: var(--accent); box-shadow: inset 0 0 0 1px var(--accent); }
.column-head { display: flex; justify-content: space-between; padding: 9px 11px; font-weight: 600; }
.column-head span { color: var(--muted); font-variant-numeric: tabular-nums; font-weight: 400; }
.cards { display: grid; gap: 8px; padding: 0 8px 8px; }
.card {
  display: grid;
  gap: 5px;
  padding: 9px 10px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  cursor: grab;
}
.card.is-dragging { opacity: .45; }
.card-title { font-weight: 600; overflow-wrap: anywhere; }
.card-meta { display: flex; justify-content: space-between; gap: 8px; color: var(--muted); font-size: 12px; }
.card-meta span:last-child { text-align: right; }
.card select { min-height: 30px; font-size: 12px; }

/* ---------- KPI ---------- */
.kpi-controls { display: flex; flex-wrap: wrap; gap: 12px; align-items: flex-end; margin-bottom: 12px; }
.kpi-controls label.field { width: 160px; }
.stats { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 1px; margin-bottom: 12px; overflow: hidden; background: var(--line); border: 1px solid var(--line); border-radius: var(--radius); }
.stat { padding: 10px 14px; background: var(--surface); }
.stat span { color: var(--muted); font-size: 12px; }
.stat b { display: block; font-size: 24px; line-height: 1.2; font-variant-numeric: tabular-nums; }

.weights { margin-bottom: 12px; padding: 12px 14px; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); }
.weights h2 { margin-bottom: 8px; font-size: 14px; }
.weights-row { display: grid; grid-template-columns: repeat(3, minmax(0, 200px)) auto; gap: 12px; align-items: end; }
.weights-sum { padding-bottom: 7px; font-variant-numeric: tabular-nums; }
.weights-sum.is-ok { color: var(--ok); }
.weights-sum.is-bad { color: var(--danger); font-weight: 600; }

.kpi-wrap { overflow-x: auto; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); }
.kpi-table { width: 100%; min-width: 780px; border-collapse: collapse; }
.kpi-table th, .kpi-table td { padding: 8px 10px; border-bottom: 1px solid var(--line); text-align: right; font-variant-numeric: tabular-nums; }
.kpi-table tr:last-child td { border-bottom: 0; }
.kpi-table th { background: var(--surface-2); color: var(--muted); font-size: 12px; font-weight: 600; }
.kpi-table th small { display: block; font-weight: 400; }
.kpi-table th:first-child, .kpi-table td:first-child { text-align: left; }
.kpi-table .col-bar { width: 170px; text-align: left; }
.kpi-table .kpi-total { font-weight: 700; }
.kpi-bar { display: grid; grid-template-columns: 1fr 42px; gap: 6px; align-items: center; }
.kpi-bar small { text-align: right; color: var(--muted); }
.note { margin-top: 8px; color: var(--muted); font-size: 12px; }

/* ---------- ตั้งค่า ---------- */
.settings-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 12px; margin-bottom: 12px; }
.panel { min-width: 0; padding: 12px 14px; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); }
.panel h2 { margin-bottom: 8px; font-size: 15px; }
.name-row, .add-row { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 6px; margin-bottom: 6px; }
.add-row { margin: 10px 0 0; padding-top: 10px; border-top: 1px solid var(--line); }
.panel .hint { margin-top: 6px; color: var(--muted); font-size: 12px; }
.panel .hint.is-error { color: var(--danger); }
.data-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.visually-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); }

/* ---------- วิธีใช้ ---------- */
.guide { max-width: 760px; }
.guide h2 { font-size: 20px; }
.guide-intro { margin: 4px 0 14px; color: var(--muted); }
.guide-steps { display: grid; gap: 10px; margin: 0; padding: 0; list-style: none; counter-reset: step; }
.guide-steps li {
  counter-increment: step;
  position: relative;
  padding: 12px 14px 14px 52px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}
.guide-steps li::before {
  content: counter(step);
  position: absolute;
  top: 12px;
  left: 14px;
  display: grid;
  place-items: center;
  width: 26px;
  height: 26px;
  background: var(--accent-soft);
  border-radius: 50%;
  color: var(--accent);
  font-weight: 700;
}
.guide-steps h3 { font-size: 15px; }
.guide-try { margin: 6px 0 10px; padding: 8px 10px; background: var(--surface-2); border-radius: var(--radius); }

/* ---------- จอเล็ก ---------- */
@media (min-width: 900px) {
  .task-table .c-title { position: sticky; left: 0; z-index: 1; background: var(--surface); box-shadow: 1px 0 0 var(--line); }
  .task-table th.c-title { z-index: 3; background: var(--surface-2); }
  tr.blank-row .c-title { background: var(--accent-soft); }
  tr.is-done .c-title { background: #fafaf8; }
}

@media (max-width: 1100px) {
  .quad-grid { grid-template-columns: 1fr; }
  .settings-grid { grid-template-columns: 1fr; }
}

@media (max-width: 700px) {
  :root { --control-h: 40px; }
  .topbar { display: block; padding: 0 12px; }
  .brand { padding: 8px 0 0; }
  .tabs { margin: 0 -12px; padding: 0 6px; }
  .tabs button { min-height: 44px; }
  .banner { padding: 8px 12px; }
  main { padding: 12px 12px 32px; }

  .filters { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .filters .f-search { grid-column: 1 / -1; }
  .filters .count { padding: 0; }

  .table-wrap { max-height: none; }
  .task-table .c-title { width: 230px; }

  .quad-list li { grid-template-columns: minmax(0, 1fr) auto; gap: 2px 10px; }
  .quad-list .title { grid-column: 1 / -1; }

  .cal-grid { display: none; }
  .cal-list { display: grid; gap: 8px; }
  .cal-list section { padding: 9px 11px; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); }
  .cal-list section.is-today { border-color: var(--accent); box-shadow: inset 0 0 0 1px var(--accent); }
  .cal-list h3 { margin-bottom: 5px; font-size: 13px; }
  .cal-list .cal-task { margin-top: 3px; padding: 4px 6px; font-size: 13px; }
  .cal-empty { display: block; }

  .column { flex-basis: 80vw; }

  .stats { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .weights-row { grid-template-columns: 1fr; }
  .kpi-controls label.field { width: calc(50% - 6px); }
}
