:root {
  --primary: #1f4e78;
  --primary-2: #3860b2;
  --primary-soft: #ddebf7;
  --alarm: #c00000;
  --ok: #388e3c;
  --ok-soft: #e8f5e9;
  --warn: #e67800;
  --warn-soft: #fff3e0;
  --bg: #f4f6f9;
  --card: #ffffff;
  --text: #1b1f24;
  --muted: #5f6b7a;
  --line: #d5dbe3;
  --soft: #eef2f7;
  --input: #ffffff;
  --value: #1f4e9c;
  --shadow: 0 1px 3px rgba(0,0,0,.08);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --primary: #6aa5dc; --primary-2: #8bb8ea; --primary-soft: #1d3247;
    --alarm: #ff6b6b; --ok: #6cc070; --ok-soft: #1f3322; --warn: #ffa64d; --warn-soft: #3a2a14;
    --bg: #12161b; --card: #1b2128; --text: #e6e9ed; --muted: #9aa5b1; --line: #2e3742; --input: #141a20;
    --value: #8fbcff; --shadow: none; --soft: #232b34;
  }
}
:root[data-theme="dark"] {
  --primary: #6aa5dc; --primary-2: #8bb8ea; --primary-soft: #1d3247;
  --alarm: #ff6b6b; --ok: #6cc070; --ok-soft: #1f3322; --warn: #ffa64d; --warn-soft: #3a2a14;
  --bg: #12161b; --card: #1b2128; --text: #e6e9ed; --muted: #9aa5b1; --line: #2e3742; --input: #141a20;
  --value: #8fbcff; --shadow: none; --soft: #232b34;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body { background: var(--bg); color: var(--text); font: 15px/1.45 "Sarabun", Tahoma, system-ui, sans-serif; }
a { color: var(--primary); }
.topbar { position: sticky; top: 0; z-index: 20; display: flex; align-items: center; justify-content: space-between;
  gap: 12px; padding: 8px 16px; background: var(--card); border-bottom: 1px solid var(--line); }
.brand { display: flex; align-items: center; gap: 10px; text-decoration: none; color: var(--text); font-weight: 700; }
.brand img { height: 40px; background: #fff; border-radius: 4px; padding: 2px; }
.who { font-size: 13px; color: var(--muted); text-align: right; margin-left: auto; }
.lang-switch { display: flex; gap: 0; margin-left: 10px; border: 1px solid var(--line); border-radius: 6px; overflow: hidden; flex-shrink: 0; }
.lang-btn { background: transparent; color: var(--muted); border: 0; border-radius: 0; padding: 4px 9px; font-size: 12px; min-height: 0; box-shadow: none; }
.lang-btn.on { background: var(--primary); color: #fff; }
/* Dashboard */
.dash-tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 8px; margin-bottom: 10px; }
.dash-tile { border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; background: var(--card); }
.dash-val { font-size: 20px; font-weight: 700; color: var(--value); }
.dash-val small { font-size: 12px; font-weight: 400; color: var(--muted); }
.dash-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(420px, 1fr)); gap: 12px; }
.dash-chart { border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px 4px; background: var(--card); min-width: 0; }
.dash-chart-title { font-weight: 600; margin-bottom: 4px; }
.dash-canvas { position: relative; height: 240px; }
@media (max-width: 760px) { .dash-grid { grid-template-columns: 1fr; } .dash-canvas { height: 210px; } }
.pdf-page .dash-grid { grid-template-columns: 1fr 1fr; }
.pdf-page .dash-canvas { height: auto; }
.pdf-page .dash-canvas img { width: 100%; height: auto; display: block; }

/* หน้าที่ส่งออก PDF – ใช้สีโหมดสว่างเสมอ ไม่ตัดตาราง */
.pdf-page { --bg: #fff; --card: #fff; --text: #1b1f24; --muted: #5f6b7a; --line: #c9d0d9; --input: #fff; --soft: #eef2f7; --value: #1f4e9c;
  --ok: #2e7d32; --alarm: #c62828; --warn-soft: #fff3e0; --ok-soft: #e8f5e9; --primary-soft: #e3eefa; --shadow: none;
  background: #fff; color: var(--text); font-size: 12px; padding: 0 2px; box-sizing: border-box; width: 100%; }
.pdf-page *, .pdf-page *::before, .pdf-page *::after { box-sizing: border-box; }
/* กรอบตารางต้องอยู่ในหน้ากระดาษ (เดิม width 100% + ขอบ ทำให้ขอบขวาตกกรอบ) ; ไม่มีเงา / มุมโค้ง */
.pdf-page .tbl-wrap { overflow: visible !important; max-height: none !important; width: 100%; max-width: 100%; box-shadow: none; border-radius: 0; }
.pdf-page .card { box-shadow: none; max-width: 100%; }
.pdf-page table { width: 100%; max-width: 100%; }
.pdf-page table.t { font-size: 11px; }
.pdf-page table.t th, .pdf-page table.t td { padding: 4px 6px; }
.pdf-page table.t td:first-child { white-space: nowrap; }
.pdf-page h2 { font-size: 16px; }
.pdf-page h3 { font-size: 13px; margin: 12px 0 4px; }
.pdf-page .pdf-head > div { flex: 1; min-width: 0; white-space: nowrap; }
.pdf-page .pdf-head img { height: 30px; width: auto; max-width: 180px; flex: 0 0 auto; object-fit: contain; object-position: right; }
.pdf-page tr, .pdf-page .banner { page-break-inside: avoid; break-inside: avoid; }
.pdf-page .pdf-head { display: flex; justify-content: space-between; align-items: center; border-bottom: 2px solid #1f4e78; margin-bottom: 8px; padding-bottom: 4px; }
.pdf-page .pdf-head img { height: 34px; }
.pdf-page .pdf-foot { margin-top: 10px; font-size: 10px; color: #5f6b7a; text-align: right; }
.pdf-page .report-sheet { background: #fff; color: #000; }
.pdf-page .report-sheet + .report-sheet { break-before: page; page-break-before: always; }
main { max-width: 1280px; margin: 0 auto; padding: 16px; }
.loading { padding: 40px; text-align: center; color: var(--muted); }

h1 { font-size: 20px; margin: 0 0 4px; }
h2 { font-size: 17px; margin: 18px 0 8px; }
.sub { color: var(--muted); font-size: 13px; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 14px; box-shadow: var(--shadow); margin-bottom: 14px; }
.row { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; }
.row.end { justify-content: flex-end; }
.spacer { flex: 1; }
.grid-areas { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 10px; }
.area-tile { text-align: left; padding: 14px; border-radius: 10px; border: 1px solid var(--line); background: var(--card);
  color: var(--text); cursor: pointer; font: inherit; }
.area-tile:hover { border-color: var(--primary); }
.area-tile b { display: block; }
.area-tile small { color: var(--muted); }
/* หน้าแรก: แถบหัวข้อ Processing Logsheet / Metallurgy Logsheet / MET Accounting / Dashboard */
.home-sections { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; margin-bottom: 14px; }
@media (max-width: 1000px) { .home-sections { grid-template-columns: repeat(2, 1fr); } }
/* สีน้ำเงินตามโลโก้ Akara (#1E4F9C) ; หัวข้อที่เลือก = น้ำเงินเข้ม + แถบเหลืองแบบโลโก้ */
.home-sec { text-align: left; padding: 14px 16px 12px; border-radius: 10px; border: 0; border-bottom: 5px solid transparent; background: #1e4f9c; color: #fff;
  cursor: pointer; font: inherit; box-shadow: 0 1px 3px rgba(0, 0, 0, .15); transition: background .15s; }
.home-sec b { display: block; font-size: 18px; color: #fff; }
.home-sec small { color: rgba(255, 255, 255, .82); }
.home-sec:hover { background: #2a62b8; }
.home-sec.on { background: #12346e; border-bottom-color: #f5c400; }
.home-sec:focus-visible { outline: 3px solid #f5c400; outline-offset: 2px; }
@media (max-width: 640px) { .home-sections { grid-template-columns: 1fr; } .home-sec { padding: 10px 12px; } .home-sec b { font-size: 16px; } }

label.f { display: flex; flex-direction: column; gap: 3px; font-size: 13px; color: var(--muted); }
input, select, textarea { font: inherit; color: var(--text); background: var(--input); border: 1px solid var(--line);
  border-radius: 6px; padding: 7px 9px; min-height: 38px; }
textarea { min-height: 80px; width: 100%; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary-2); outline-offset: 0; }
input.bad { border: 2px solid var(--alarm); }
input.oor { background: var(--warn-soft); }
input.off:not(.oor) { background: var(--soft); border-style: dashed; }
input:disabled, textarea:disabled { opacity: .65; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.seg button { border: 0; border-radius: 0; background: var(--card); color: var(--text); min-width: 52px; }
.seg button.on { background: var(--primary); color: #fff; }

button { font: inherit; cursor: pointer; border-radius: 8px; border: 1px solid var(--primary); background: var(--primary);
  color: #fff; padding: 8px 14px; min-height: 40px; }
button.ghost { background: transparent; color: var(--primary); }
button.ok { background: var(--ok); border-color: var(--ok); }
button.warn { background: var(--warn); border-color: var(--warn); }
button.danger { background: var(--alarm); border-color: var(--alarm); }
button.small { min-height: 30px; padding: 3px 10px; font-size: 13px; }
button:disabled { opacity: .5; cursor: not-allowed; }
.icon-btn { background: transparent; color: var(--primary); border: 0; padding: 4px 8px; font-size: 20px; min-height: 0; }
/* ปุ่มหน้าแรก / กลับ (บนซ้ายของทุกหน้า) – สีน้ำเงินตามโลโก้ */
.home-btn { display: inline-flex; align-items: center; gap: 8px; padding: 10px 18px; border: 0; border-radius: 10px; background: #1e4f9c; color: #fff;
  font: inherit; font-size: 17px; font-weight: 700; cursor: pointer; box-shadow: 0 1px 3px rgba(0, 0, 0, .15); white-space: nowrap; }
.home-btn:hover { background: #2a62b8; }
.home-btn:focus-visible { outline: 3px solid #f5c400; outline-offset: 2px; }
.home-btn-icon { font-size: 24px; line-height: 1; }
@media (max-width: 640px) { .home-btn { padding: 8px 12px; font-size: 15px; } .home-btn-icon { font-size: 20px; } }

.banner { padding: 10px 12px; border-radius: 8px; margin: 8px 0; }
.banner.info { background: var(--primary-soft); }
.banner.ok { background: var(--ok-soft); }
.banner.warn { background: var(--warn-soft); }
.banner.bad { background: var(--warn-soft); border-left: 4px solid var(--alarm); }

.tbl-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; border: 1px solid var(--line); border-radius: 8px; background: var(--card); }
table.t { border-collapse: collapse; width: 100%; font-size: 14px; }
table.t th, table.t td { border-bottom: 1px solid var(--line); padding: 6px 8px; text-align: left; vertical-align: middle; }
table.t th { background: var(--primary-soft); font-weight: 600; position: sticky; top: 0; white-space: nowrap; }
table.t td.n, table.t th.n { text-align: right; font-variant-numeric: tabular-nums; }
table.t tr.tot td { font-weight: 700; border-top: 2px solid var(--text); }
table.t tr.sel td { background: var(--warn-soft); }
table.grid td { padding: 3px; }
table.grid td.p { min-width: 220px; padding: 6px 8px; }
table.grid input { width: 100%; min-width: 64px; text-align: center; }
table.grid th.cur { background: var(--primary); color: #fff; }
.v { color: var(--value); font-weight: 700; }
.muted { color: var(--muted); }
.badge { display: inline-block; padding: 1px 8px; border-radius: 10px; font-size: 12px; background: var(--primary-soft); }
.badge.ok { background: var(--ok-soft); color: var(--ok); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.bad { background: var(--warn-soft); color: var(--alarm); }
.tabs { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 12px; }
.tabs button { background: var(--card); color: var(--text); border-color: var(--line); }
.tabs button.on { background: var(--primary); color: #fff; border-color: var(--primary); }
.hours { display: flex; gap: 6px; flex-wrap: wrap; margin: 8px 0; }
.hours button { background: var(--card); color: var(--text); border-color: var(--line); min-width: 64px; }
.hours button.has { background: var(--ok-soft); border-color: var(--ok); color: var(--ok); }
.hours button.hdr { background: var(--warn-soft); border-color: var(--warn); color: var(--warn); }
.hours button.on { background: var(--primary); border-color: var(--primary); color: #fff; }
.split { display: grid; grid-template-columns: 320px 1fr; gap: 14px; }
.list-item { padding: 10px 12px; border-bottom: 1px solid var(--line); cursor: pointer; }
.list-item:hover { background: var(--primary-soft); }
.list-item.on { background: var(--warn-soft); }

#toasts { position: fixed; right: 16px; bottom: 16px; z-index: 100; display: flex; flex-direction: column; gap: 8px; max-width: min(420px, calc(100vw - 32px)); }
.toast { padding: 10px 14px; border-radius: 8px; color: #fff; background: #37474f; box-shadow: 0 4px 12px rgba(0,0,0,.25); white-space: pre-line; }
.toast.success { background: var(--ok); } .toast.error { background: var(--alarm); } .toast.warning { background: var(--warn); }
.modal-back { position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 50; display: flex; align-items: center; justify-content: center; padding: 16px; }
.modal { background: var(--card); color: var(--text); border-radius: 12px; width: min(720px, 100%); max-height: calc(100vh - 32px); overflow: auto; padding: 18px; }
.modal h3 { margin: 0 0 10px; }

@media (max-width: 760px) {
  main { padding: 10px 16px; }
  .split { grid-template-columns: 1fr; }
  .brand span { display: none; }
  /* แถบบนบนมือถือ: โลโก้ (ภาพแนวยาว 1130×78) ไม่ให้ดันชื่อ / ปุ่มภาษาออกนอกจอ */
  .topbar { flex-wrap: wrap; gap: 6px 10px; padding: 6px 12px; }
  .brand { flex: 0 1 auto; min-width: 0; }
  .brand img { height: auto; width: auto; max-width: 120px; max-height: 32px; object-fit: contain; }
  .who { flex: 1 1 160px; min-width: 0; font-size: 12px; overflow-wrap: anywhere; }
  .lang-switch { margin-left: 0; }
  table.grid td.p { min-width: 160px; }
}
@media print {
  .topbar, .noprint, #toasts, button { display: none !important; }
  body { background: #fff; color: #000; }
  .card { border: 0; box-shadow: none; }
}
