:root{
  --bg:#f6f5f1;
  --surface:#ffffff;
  --ink:#20241f;
  --ink-soft:#5c6259;
  --line:#dedad0;
  --teal:#0f6e56;
  --teal-light:#e1f5ee;
  --amber:#854f0b;
  --amber-light:#faeeda;
  --red:#a32d2d;
  --red-light:#fcebeb;
  --radius:10px;
}
*{box-sizing:border-box;}
body{
  margin:0;
  font-family:"Sarabun","Noto Sans Thai",system-ui,sans-serif;
  background:var(--bg);
  color:var(--ink);
  padding-bottom:72px;
}
.topbar{
  background:var(--surface);
  border-bottom:1px solid var(--line);
  padding:10px 14px;
  position:sticky;
  top:0;
  z-index:10;
  display:flex;
  flex-wrap:wrap;
  align-items:center;
  gap:8px;
  justify-content:space-between;
}
.topbar-title{font-weight:700;font-size:18px;color:var(--teal);}
.topbar-controls{display:flex;gap:6px;flex-wrap:wrap;}
.ctrl{
  padding:6px 8px;
  border-radius:8px;
  border:1px solid var(--line);
  background:var(--surface);
  font-size:14px;
  font-family:inherit;
}
.tabs{
  display:flex;
  overflow-x:auto;
  background:var(--surface);
  border-bottom:1px solid var(--line);
  position:sticky;
  top:52px;
  z-index:9;
}
.tab-btn{
  flex:1;
  min-width:120px;
  padding:12px 8px;
  border:none;
  background:none;
  font-family:inherit;
  font-size:14px;
  color:var(--ink-soft);
  cursor:pointer;
  border-bottom:2px solid transparent;
  white-space:nowrap;
}
.tab-btn.active{color:var(--teal);border-bottom-color:var(--teal);font-weight:600;}
main{padding:14px;max-width:900px;margin:0 auto;}
.tab-panel{display:none;}
.tab-panel.active{display:block;}
.card-list{display:flex;flex-direction:column;gap:10px;}
.card{
  background:var(--surface);
  border:1px solid var(--line);
  border-radius:var(--radius);
  padding:14px;
}
.card-part{
  font-size:12px;
  color:var(--ink-soft);
  margin-bottom:2px;
}
.card-title{font-weight:600;font-size:15px;margin-bottom:8px;}
.card-sub{font-size:12px;color:var(--ink-soft);margin-bottom:8px;}
.row{display:flex;gap:8px;align-items:center;flex-wrap:wrap;}
.field{display:flex;flex-direction:column;gap:4px;flex:1;min-width:110px;}
.field label{font-size:12px;color:var(--ink-soft);}
.field input,.field select{
  padding:8px 10px;
  border:1px solid var(--line);
  border-radius:8px;
  font-size:15px;
  font-family:inherit;
  width:100%;
}
.pct-preview{
  font-weight:700;
  font-size:20px;
  min-width:74px;
  text-align:right;
}
.pct-ok{color:var(--teal);}
.pct-bad{color:var(--red);}
.pct-na{color:var(--ink-soft);}
.save-btn{
  margin-top:10px;
  padding:8px 16px;
  border:none;
  border-radius:8px;
  background:var(--teal);
  color:#fff;
  font-family:inherit;
  font-size:14px;
  cursor:pointer;
}
.save-btn:active{opacity:.8;}
.badge{
  display:inline-block;
  padding:2px 8px;
  border-radius:999px;
  font-size:12px;
  font-weight:600;
}
.badge-pass{background:var(--teal-light);color:var(--teal);}
.badge-fail{background:var(--red-light);color:var(--red);}
.badge-none{background:var(--amber-light);color:var(--amber);}
.section-heading{
  font-weight:600;
  font-size:16px;
  margin:18px 0 8px;
  color:var(--ink);
}
.risk-point-row{
  display:grid;
  grid-template-columns:1fr;
  gap:6px;
  padding:10px;
  border:1px solid var(--line);
  border-radius:8px;
  margin-bottom:8px;
  font-size:13px;
}
.risk-point-row .rp-grid{
  display:grid;
  grid-template-columns:repeat(2,1fr);
  gap:6px;
}
@media(min-width:640px){
  .risk-point-row .rp-grid{grid-template-columns:repeat(4,1fr);}
}
.small-btn{
  padding:5px 10px;
  border-radius:6px;
  border:1px solid var(--line);
  background:var(--surface);
  font-size:12px;
  cursor:pointer;
  font-family:inherit;
}
.toast{
  position:fixed;
  bottom:16px;
  left:50%;
  transform:translateX(-50%) translateY(120%);
  background:var(--ink);
  color:#fff;
  padding:10px 18px;
  border-radius:8px;
  font-size:14px;
  transition:transform .25s ease;
  z-index:100;
}
.toast.show{transform:translateX(-50%) translateY(0);}
.empty-msg{color:var(--ink-soft);font-size:14px;padding:20px;text-align:center;}

/* filter bar */
.filter-bar{
  display:flex;
  gap:8px;
  flex-wrap:wrap;
  margin-bottom:12px;
}
.filter-bar select{
  padding:6px 8px;
  border-radius:8px;
  border:1px solid var(--line);
  background:var(--surface);
  font-size:13px;
  font-family:inherit;
}

/* KPI detail modal */
.modal-overlay{
  position:fixed;
  inset:0;
  background:rgba(32,36,31,.5);
  display:flex;
  align-items:flex-end;
  justify-content:center;
  z-index:200;
}
@media(min-width:640px){
  .modal-overlay{align-items:center;}
}
.modal-box{
  background:var(--surface);
  border-radius:var(--radius) var(--radius) 0 0;
  padding:20px;
  width:100%;
  max-width:480px;
  max-height:85vh;
  overflow-y:auto;
  position:relative;
}
@media(min-width:640px){
  .modal-box{border-radius:var(--radius);}
}
.modal-close{
  position:absolute;
  top:12px; right:12px;
  background:none;
  border:none;
  font-size:18px;
  cursor:pointer;
  color:var(--ink-soft);
}
.modal-row{
  display:flex;
  justify-content:space-between;
  gap:12px;
  padding:8px 0;
  border-bottom:1px solid var(--line);
  font-size:14px;
}
.modal-row:last-child{border-bottom:none;}
.modal-row .lbl{color:var(--ink-soft);min-width:100px;}
.modal-row .val{text-align:right;flex:1;}
.modal-title{font-size:17px;font-weight:700;margin-bottom:4px;padding-right:24px;}
.modal-part{font-size:12px;color:var(--teal);margin-bottom:12px;}
.chart-wrap{position:relative;}

/* การ์ดเปรียบเทียบ 3 ปี พร้อมกราฟ */
.card-title-row{
  display:flex;
  justify-content:space-between;
  align-items:center;
  cursor:pointer;
}
.card-title-row:hover .card-title{text-decoration:underline;}
.trend-row{
  display:grid;
  grid-template-columns:repeat(3,1fr);
  gap:8px;
  margin-top:6px;
}
.trend-col{
  text-align:center;
  padding:8px 4px;
  border-radius:8px;
  background:var(--bg);
}
.trend-year{font-size:11px;color:var(--ink-soft);margin-bottom:4px;}
.trend-value{font-weight:700;font-size:16px;}

/* ตารางแบบ scroll แนวนอน (ใช้ร่วมกันทั้ง entry grid และ year table) */
.grid-scroll{
  overflow-x:auto;
  margin-bottom:16px;
  border:1px solid var(--line);
  border-radius:var(--radius);
  background:var(--surface);
  -webkit-overflow-scrolling:touch;
}

/* ตารางกรอกข้อมูลรายเดือน แบบ Excel */
.entry-grid{
  border-collapse:collapse;
  font-size:12px;
  min-width:100%;
}
.entry-grid th, .entry-grid td{
  border:1px solid var(--line);
  padding:3px;
  text-align:center;
  white-space:nowrap;
}
.entry-grid thead th{
  background:var(--bg);
  font-weight:600;
  font-size:11px;
  color:var(--ink-soft);
  padding:6px 4px;
  position:sticky;
  top:0;
  z-index:2;
}
.entry-grid td.grid-kpi-name, .entry-grid th:first-child{
  position:sticky;
  left:0;
  background:var(--surface);
  text-align:left;
  padding:8px 10px;
  min-width:170px;
  max-width:220px;
  white-space:normal;
  font-size:13px;
  cursor:pointer;
  z-index:1;
  border-right:1px solid var(--line-strong,var(--line));
}
.entry-grid thead th:first-child{
  background:var(--bg);
  z-index:3;
}
.entry-grid td.grid-kpi-name:hover{color:var(--teal);text-decoration:underline;}
.entry-grid td.cell-pair{padding:1px;}
.entry-grid input{
  width:42px;
  font-size:11px;
  padding:3px 2px;
  border:1px solid transparent;
  border-radius:3px;
  text-align:center;
  font-family:inherit;
  background:transparent;
}
.entry-grid td.cell-pair input{width:40px;display:block;margin:0 auto;}
.entry-grid td.cell-pair input.grid-num{border-bottom:1px dashed var(--line);}
.entry-grid input:focus{border-color:var(--teal);background:var(--surface);outline:none;}
.entry-grid input.cell-saved{background:var(--teal-light);transition:background .1s;}

/* ปุ่มเปิดกรอกข้อมูลย้อนหลัง + grid สีต่าง */
.batch-toggle{
  margin:10px 0 4px;
  color:var(--amber);
  border-color:var(--amber);
}
.entry-grid.historical-grid thead th{
  background:var(--amber-light);
  color:var(--amber);
}
.entry-grid.historical-grid td.grid-kpi-name{
  background:var(--amber-light);
}
.entry-grid.historical-grid input{
  background:#fdf6ea;
}
.entry-grid.historical-grid input:focus{
  border-color:var(--amber);
  background:var(--surface);
}

/* ตารางเปรียบเทียบ 3 ปีในแดชบอร์ด */
.year-table{
  border-collapse:collapse;
  font-size:13px;
  min-width:100%;
}
.year-table th, .year-table td{
  padding:8px 10px;
  border-bottom:1px solid var(--line);
  text-align:center;
  white-space:nowrap;
}
.year-table thead th{
  background:var(--bg);
  font-weight:600;
  font-size:12px;
  color:var(--ink-soft);
}
.year-table td.grid-kpi-name, .year-table th:first-child{
  position:sticky;
  left:0;
  background:var(--surface);
  text-align:left;
  min-width:200px;
  white-space:normal;
  cursor:pointer;
}
.year-table thead th:first-child{background:var(--bg);}
.year-table td.grid-kpi-name:hover{color:var(--teal);text-decoration:underline;}
.year-table td.target-cell{font-size:11px;color:var(--ink-soft);white-space:nowrap;}
.year-table td.pct-ok{background:var(--teal-light);color:var(--teal);font-weight:600;}
.year-table td.pct-bad{background:var(--red-light);color:var(--red);font-weight:600;}
.year-table td.pct-na{color:var(--ink-soft);}
