:root {
  --bg: #ffffff;
  --panel: #f4f1f8;
  --panel-head: #ece7f3;
  --text: #1f1b24;
  --muted: #6b6475;
  --line: #ddd5e8;
  --brand: #5b2a86;
  --brand-text: #ffffff;
  --accent: #7a3fb0;
  --btn-bg: #efe6f9;    --btn-fg: #4a1f70;   --btn-line: #d6c2ee;
  --ok-bg: #efe7f8;     --ok-fg: #4a1f70;
  --warn-bg: #fff3d6;   --warn-fg: #8a5a00;
  --danger-bg: #fde3e3; --danger-fg: #a1161b;
  --now-row: #f3eafc;
  --img-filter: none;
  --left: 55%;
  --bar-h: 0px;
  --fk: 'Kanit', 'Leelawadee UI', 'Tahoma', sans-serif;
}
[data-theme="dark"] {
  --bg: #15111c;
  --panel: #1e1828;
  --panel-head: #251e31;
  --text: #ddd6e6;
  --muted: #9087a0;
  --line: #342b42;
  --brand: #4a2a6b;
  --brand-text: #e9e1f3;
  --accent: #b48ce0;
  --btn-bg: #2a2138;    --btn-fg: #d6c4ee;   --btn-line: #3f3354;
  --ok-bg: #2a2138;     --ok-fg: #d6c4ee;
  --warn-bg: #3a2e14;   --warn-fg: #f1cf7a;
  --danger-bg: #3d1a1d; --danger-fg: #f29a9d;
  --now-row: #2b2240;
  --img-filter: brightness(.62) contrast(.95);
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg); color: var(--text);
  font-family: 'Sarabun', 'Leelawadee UI', 'Tahoma', system-ui, sans-serif;
  transition: background .3s, color .3s;
  overflow: hidden;
}
button { font: inherit; }

#app { display: grid; grid-template-columns: var(--left) 1fr; height: calc(100vh - var(--bar-h)); }

/* ---------- ฝั่งซ้าย: ขนาดตัวอักษรอิงความกว้างของฝั่งนี้ (cqw) ---------- */
#left {
  container-type: inline-size;
  display: flex; flex-direction: column; align-items: center; justify-content: flex-start;
  padding: 3vh 2.5vw; gap: 2vh; min-width: 0; overflow: hidden;
}
#brand { display: flex; flex-direction: column; align-items: center; gap: .5vh; }
#logo { height: 9vh; width: auto; }
[data-theme="dark"] #logo { filter: brightness(.85); }
#school-th { font-family: var(--fk); color: var(--accent); font-size: 2.6vh; font-weight: 500; }
#school-en {
  font-family: var(--fk); font-weight: 500; letter-spacing: .08em;
  background: var(--brand); color: var(--brand-text);
  padding: .3vh 1.2vw; font-size: 2.1vh;
}
#date { font-family: var(--fk); font-weight: 300; text-align: center; font-size: 3vw; font-size: min(6.2cqw, 7vh); white-space: nowrap; }
#clock {
  font-family: var(--fk); font-weight: 400; line-height: 1;
  display: flex; align-items: center;
  font-size: 9vw; font-size: min(20cqw, 26vh);
}
#clock > i { font-style: normal; display: inline-block; width: .42em; text-align: center; position: relative; top: -.06em; opacity: .85; }
/* ช่องตัวเลขกว้างคงที่ (ใช้ทั้งนาฬิกาและตัวนับถอยหลัง) */
.dg { display: inline-block; width: .62em; text-align: center; font-weight: inherit; }
.sep { font-style: normal; display: inline-block; width: .3em; text-align: center; }

/* กล่อง "กำลังสอบ" */
#now { width: 100%; display: flex; flex-direction: column; gap: 1.2vh; }
.now-card {
  border-radius: 14px; padding: 1.6vh 2.4cqw;
  background: var(--ok-bg); color: var(--ok-fg);
  transition: background .4s, color .4s;
}
.now-card.warn   { background: var(--warn-bg);   color: var(--warn-fg); }
.now-card.danger { background: var(--danger-bg); color: var(--danger-fg); }
.now-card.next   { background: var(--panel); color: var(--text); }
.now-label { font-family: var(--fk); font-size: min(2.6cqw, 3vh); opacity: .85; }
.now-subject { font-family: var(--fk); font-weight: 500; font-size: min(4cqw, 4.6vh); line-height: 1.25; }
.now-meta { font-size: min(2.5cqw, 2.9vh); opacity: .9; }
.now-remain { font-family: var(--fk); font-weight: 600; font-size: min(6.4cqw, 7.5vh); line-height: 1.1; white-space: nowrap; }
.now-row { display: flex; justify-content: space-between; align-items: flex-end; gap: 2cqw; }
.now-more { font-size: min(2.2cqw, 2.6vh); opacity: .8; }
#now.many .now-card { padding: 1.1vh 2.2cqw; }
#now.many .now-subject { font-size: min(3.2cqw, 3.6vh); }
#now.many .now-remain { font-size: min(5cqw, 5.6vh); }

/* ---------- ฝั่งขวา ---------- */
#right {
  overflow-y: auto; padding: 1.4vh 1.2vw 4vh 0; min-width: 0;
  scrollbar-width: thin; scrollbar-color: var(--line) transparent;
}
#exam-title {
  font-family: var(--fk); font-weight: 500; color: var(--accent);
  font-size: clamp(15px, 1.25vw, 32px); margin: .3vh 0 1vh .2vw;
  min-height: 46px; display: flex; align-items: center;
  padding-right: 400px;          /* เว้นที่ให้ปุ่มมุมขวาบน */
}
.tab { border: 1px solid var(--line); border-radius: 8px; margin-bottom: .8vh; background: var(--panel); overflow: hidden; }
.tab-head {
  width: 100%; text-align: left; border: 0; cursor: pointer;
  background: var(--panel-head); color: var(--text);
  padding: 1vh .9vw; font-family: var(--fk); font-size: clamp(14px, 1.1vw, 30px);
  display: flex; align-items: center; gap: .6vw;
}
.tab-head::before { content: '›'; display: inline-block; transition: transform .2s; font-size: 1.3em; line-height: .8; }
.tab.open .tab-head::before { transform: rotate(90deg); }
.tab-body { display: none; padding: 1vh .8vw; }
.tab.open .tab-body { display: block; }
.block + .block { margin-top: 1.8vh; }

/* ตารางสอบ */
.sched-title { font-family: var(--fk); font-weight: 500; text-align: center; font-size: clamp(13px, 1.05vw, 28px); margin-bottom: .8vh; }
table.sched { width: 100%; border-collapse: collapse; font-size: clamp(12px, .92vw, 24px); background: var(--bg); }
table.sched th, table.sched td { border: 1px solid var(--line); padding: .4vh .35vw; vertical-align: top; line-height: 1.35; }
table.sched th { background: var(--panel-head); font-family: var(--fk); font-weight: 500; white-space: nowrap; }
table.sched td.c { text-align: center; white-space: nowrap; }
table.sched td.date { font-family: var(--fk); text-align: center; background: var(--panel); white-space: nowrap; }
table.sched tr.past td:not(.date) { opacity: .45; }
table.sched tr.running td:not(.date) { background: var(--now-row); font-weight: 600; }
table.sched tr.running td.time::before { content: '● '; color: var(--danger-fg); }

/* รูปภาพ */
.img-block img { display: block; width: 100%; height: auto; border-radius: 6px; cursor: zoom-in; filter: var(--img-filter); transition: filter .25s; }
.img-block img:hover, .img-block img:active { filter: none; }
.img-cap { text-align: center; color: var(--muted); font-size: clamp(12px, .95vw, 22px); margin-top: .6vh; }

/* ข้อความ */
.text-block { font-size: clamp(14px, 1.05vw, 28px); line-height: 1.6; }
.text-block h3 { font-family: var(--fk); font-weight: 500; color: var(--accent); margin: .6em 0 .3em; font-size: 1.2em; }
.text-block ul { margin: .3em 0; padding-left: 1.4em; }
.text-block p { margin: .35em 0; }

.empty { color: var(--muted); text-align: center; padding: 2vh; }

/* ---------- โหมดนาฬิกาอย่างเดียว ---------- */
#app.mode-clock { grid-template-columns: 100%; height: 100vh; }
#app.mode-clock #right, #app.mode-clock #now { display: none; }
#app.mode-clock #left { gap: 4vh; justify-content: center; }
#app.mode-clock #logo { height: 11vh; }
#app.mode-clock #date { font-size: 4vw; font-size: min(4.2cqw, 9vh); }
#app.mode-clock #clock { font-size: 17vw; font-size: min(17cqw, 40vh); }
body.clock-mode #announce { display: none; }

/* ---------- ปุ่มมุมขวาบน ---------- */
#tools {
  position: fixed; top: 1.4vh; right: 1.2vw; display: flex; gap: 8px; z-index: 6;
  transition: opacity .5s;
}
#tools button {
  display: inline-flex; align-items: center; gap: 8px;
  height: 46px; padding: 0 18px; border-radius: 999px;
  border: 1px solid var(--btn-line); background: var(--btn-bg); color: var(--btn-fg);
  cursor: pointer; font-family: var(--fk); font-size: 17px; font-weight: 500; white-space: nowrap;
  box-shadow: 0 1px 3px rgba(60, 20, 100, .12);
  transition: filter .15s, transform .1s;
}
#tools button:hover { filter: brightness(.96); }
#tools button:active { transform: scale(.97); }
#tools .ic { font-size: 20px; line-height: 1; }
/* นาฬิกาอย่างเดียว: จางลงเมื่อไม่ได้ขยับเมาส์ */
body.clock-mode.idle #tools { opacity: .12; }

/* จุดสถานะเวลา (มุมซ้ายล่าง) */
#sync {
  position: fixed; left: 1vw; bottom: calc(var(--bar-h) + 1.2vh);
  display: inline-flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted);
  opacity: .45; transition: opacity .25s; z-index: 6;
}
#sync:hover { opacity: 1; }
#sync b { width: 9px; height: 9px; border-radius: 50%; background: #999; display: inline-block; }
#sync.ok b { background: #2e9e5b; }
#sync.stale b { background: #d99a00; }
#sync.bad b { background: #d33; }
#sync.bad { opacity: 1; color: var(--danger-fg); }

/* ---------- แถบข้อความประกาศ ---------- */
#announce {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 5;
  background: var(--brand); color: var(--brand-text);
  font-family: var(--fk); font-weight: 500; font-size: clamp(18px, 2.1vw, 56px);
  overflow: hidden;
}
#announce[hidden] { display: none; }
#announce.static #announce-inner { padding: 1.2vh 2vw; text-align: center; line-height: 1.35; }
#announce.marquee { height: calc(1.35em + 2.4vh); }
#announce.marquee #announce-inner {
  position: absolute; top: 50%; left: 0; white-space: nowrap;
  transform: translate(100vw, -50%); animation: et-marquee var(--mq-dur, 20s) linear infinite;
}
@keyframes et-marquee {
  from { transform: translate(100vw, -50%); }
  to   { transform: translate(-100%, -50%); }
}

#preview-badge {
  position: fixed; top: 0; left: 50%; transform: translateX(-50%); z-index: 7;
  background: #d99a00; color: #000; padding: 4px 14px; border-radius: 0 0 8px 8px; font-family: var(--fk); font-size: 14px;
}

#lightbox { position: fixed; inset: 0; background: rgba(0,0,0,.85); display: flex; align-items: center; justify-content: center; cursor: zoom-out; z-index: 10; }
#lightbox[hidden] { display: none; }
#lightbox img { max-width: 96vw; max-height: 96vh; }

/* จอเล็ก: ปุ่มเหลือแต่ไอคอน */
@media (max-width: 1100px) {
  #tools .lb { display: none; }
  #tools button { padding: 0 13px; }
  #exam-title { padding-right: 170px; }
}
/* จอแนวตั้ง */
@media (max-aspect-ratio: 1/1) {
  body { overflow: auto; }
  #app { grid-template-columns: 100%; height: auto; }
  #left { min-height: 60vh; padding-top: 8vh; }
  #right { overflow: visible; padding: 0 3vw calc(var(--bar-h) + 10vh); }
  #exam-title { padding-right: 0; }
}
