/* ===== ธีมสี 5 ชุด แต่ละชุดมี 5 สี: หลัก เข้ม อ่อน เส้น ตัวอักษร ===== */
:root, [data-theme="orange"] { --brand: #c2500a; --brand-dark: #9a3f08; --brand-soft: #fdebdd; --brand-line: #f2b48a; --brand-ink: #3a1c0a; }
[data-theme="blue"]   { --brand: #1d4ed8; --brand-dark: #1e40af; --brand-soft: #dbeafe; --brand-line: #93c5fd; --brand-ink: #172554; }
[data-theme="green"]  { --brand: #047857; --brand-dark: #065f46; --brand-soft: #d1fae5; --brand-line: #6ee7b7; --brand-ink: #022c22; }
[data-theme="violet"] { --brand: #6d28d9; --brand-dark: #5b21b6; --brand-soft: #ede9fe; --brand-line: #c4b5fd; --brand-ink: #2e1065; }
[data-theme="brown"]  { --brand: #7c4a2d; --brand-dark: #5c3620; --brand-soft: #f4e8df; --brand-line: #d2b09a; --brand-ink: #2d1a0e; }

:root {
  --bg: #f6f7f9; --surface: #fff; --rail: #eef1f5; --text: #16202e; --muted: #556274; --line: #d6dee8;
  --selected: var(--brand-soft); --on-selected: var(--brand-ink); --link: var(--brand-dark);
  --ok: #05603a; --ok-bg: #dcfae6; --warn: #854a0e; --warn-bg: #fef0c7; --bad: #b42318; --bad-bg: #fee4e2;
  --pad: .5rem; --gap: 1rem;
  color-scheme: light;
}
[data-mode="dark"] {
  --bg: #12161c; --surface: #1b212a; --rail: #232a35; --text: #e8ecf2; --muted: #a3aebd; --line: #343e4c;
  --selected: #2f3846; --on-selected: var(--brand-line); --link: var(--brand-line);
  --ok: #a6f4c5; --ok-bg: #123b29; --warn: #fedf89; --warn-bg: #42300b; --bad: #fecdca; --bad-bg: #4a1a16;
  color-scheme: dark;
}

/* ===== ขนาดตัวอักษรและความโปร่งของหน้าจอ ===== */
[data-font="85"] { font-size: 85%; }   [data-font="90"] { font-size: 90%; }   [data-font="95"] { font-size: 95%; }
[data-font="100"] { font-size: 100%; } [data-font="105"] { font-size: 105%; } [data-font="110"] { font-size: 110%; }
[data-font="115"] { font-size: 115%; } [data-font="120"] { font-size: 120%; } [data-font="125"] { font-size: 125%; }
[data-font="130"] { font-size: 130%; }
[data-density="compact"] { --pad: .25rem; --gap: .6rem; }
[data-density="normal"]  { --pad: .5rem;  --gap: 1rem; }
[data-density="roomy"]   { --pad: .8rem;  --gap: 1.4rem; }
@media (max-height: 760px) { [data-density="auto"] { --pad: .3rem; --gap: .75rem; } }

* { box-sizing: border-box; }
[hidden] { display: none !important; }
body { margin: 0; background: var(--bg); color: var(--text); font-family: "Leelawadee UI", "Noto Sans Thai", system-ui, sans-serif; font-size: .9rem; line-height: 1.5; }
h1 { font-size: 1.35rem; margin: 0; } h2 { font-size: 1.05rem; margin: 0 0 var(--pad); }
a { color: var(--link); }
:focus-visible { outline: 3px solid var(--brand-line); outline-offset: 2px; }

button, .button, input, select { font: inherit; color: inherit; min-height: 2.35rem; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); }
input, select { padding: 0 .7rem; max-width: 100%; }
input:read-only:not([type="file"]):not([type="range"]):not([type="checkbox"]) { background: var(--rail); }
button, .button { display: inline-flex; align-items: center; justify-content: center; padding: 0 .9rem; cursor: pointer; text-decoration: none; transition: background-color .15s; }
button:hover, .button:hover { background: var(--rail); }
button:disabled { opacity: .6; cursor: not-allowed; }
.primary, .primary:hover { background: var(--brand); border-color: var(--brand); color: #fff; }
.primary:hover:not(:disabled) { background: var(--brand-dark); }
.danger { color: var(--bad); }
input.rate { width: 8.5rem; text-align: right; font-variant-numeric: tabular-nums; }
td.money { text-align: right; white-space: nowrap; }
input[type="checkbox"] { width: 1.15rem; height: 1.15rem; min-height: 0; accent-color: var(--brand); }
input[type="range"] { accent-color: var(--brand); padding: 0; border: 0; width: 100%; }

/* ===== โครงหน้า: header, เมนู, เนื้อหา, footer ===== */
.layout { min-height: 100vh; display: grid; grid-template-rows: auto 1fr auto; grid-template-columns: 13rem minmax(0, 1fr); grid-template-areas: "header header" "nav main" "footer footer"; }
.header { grid-area: header; background: var(--brand); color: #fff; display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: .75rem; padding: .6rem 1.25rem; }
.header strong { font-size: 1.1rem; }
.header .title { display: flex; flex-direction: column; line-height: 1.3; min-width: 0; }
.header .title span { font-size: .8rem; }
.header .tools { display: flex; flex-wrap: wrap; align-items: center; gap: .6rem; }
.header button { background: transparent; color: #fff; border-color: rgb(255 255 255 / .55); }
.header button:hover { background: var(--brand-dark); }
.nav { grid-area: nav; background: var(--surface); border-right: 1px solid var(--line); padding: .75rem; display: flex; flex-direction: column; gap: .2rem; }
.nav a, .nav-toggle { display: flex; align-items: center; gap: .6rem; padding: var(--pad) .75rem; border-radius: 8px; text-decoration: none; color: var(--muted); white-space: nowrap; }
.nav .nav-toggle { justify-content: flex-start; border-color: transparent; background: transparent; }
.nav .nav-toggle:hover { background: var(--rail); }
.icon { width: 1.15rem; height: 1.15rem; flex: none; }
.nav .nav-logout { display: flex; align-items: center; gap: .6rem; justify-content: flex-start; margin-top: .6rem; flex: none; white-space: nowrap; }
.nav-min .nav .nav-logout { justify-content: center; padding-inline: 0; }
.nav-min .nav .nav-logout span { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); }
.layout.nav-min { grid-template-columns: 3.6rem minmax(0, 1fr); }
.nav-min .nav { padding: .5rem .4rem; }
.nav-min .nav a, .nav-min .nav-toggle { justify-content: center; padding-inline: 0; }
.nav-min .nav a span, .nav-min .nav-toggle span { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); }
.nav a:hover { background: var(--rail); color: var(--text); }
.nav a[aria-current="page"] { background: var(--selected); color: var(--on-selected); font-weight: 700; }
/* กลุ่มเมนูพับได้ เช่น รายงาน > รายงานเดือน / สรุปการมาทำงาน */
.nav-group { display: flex; flex-direction: column; gap: .2rem; }
.nav .nav-parent { display: flex; align-items: center; gap: .6rem; justify-content: flex-start; padding: var(--pad) .75rem; border-color: transparent; background: transparent; color: var(--muted); white-space: nowrap; min-height: 0; }
.nav .nav-parent:hover { background: var(--rail); color: var(--text); }
.nav-group.has-current > .nav-parent { color: var(--text); font-weight: 700; }
.nav-parent .chevron { margin-left: auto; transition: transform .15s; }
.nav-group.closed .chevron { transform: rotate(-90deg); }
.nav-sub { display: flex; flex-direction: column; gap: .2rem; margin-left: 1.1rem; padding-left: .5rem; border-left: 1px solid var(--line); }
.nav-group.closed .nav-sub { display: none; }
.nav-min .nav .nav-parent { justify-content: center; padding-inline: 0; }
.nav-min .nav .nav-parent span { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); }
.nav-min .nav-parent .chevron { display: none; }
.nav-min .nav-sub { margin-left: 0; padding-left: 0; border-left: 0; }
.main { grid-area: main; padding: var(--gap) calc(var(--gap) * 1.25); min-width: 0; }
.footer { grid-area: footer; background: var(--surface); border-top: 1px solid var(--line); color: var(--muted); display: flex; flex-wrap: wrap; justify-content: space-between; gap: .5rem; padding: .5rem 1.25rem; font-size: .8rem; }
@media (max-width: 760px) {
  .layout { grid-template-columns: minmax(0, 1fr); grid-template-areas: "header" "nav" "main" "footer"; grid-template-rows: auto auto 1fr auto; }
  .nav { flex-direction: row; overflow-x: auto; white-space: nowrap; border-right: 0; border-bottom: 1px solid var(--line); }
  .main { padding: var(--gap); }
  /* จอเล็กใช้เมนูด้านบนพร้อมชื่อเสมอ */
  .layout.nav-min { grid-template-columns: minmax(0, 1fr); }
  .nav .nav-toggle { display: none; }
  .nav-min .nav a span, .nav-min .nav .nav-logout span { position: static; width: auto; height: auto; clip-path: none; }
  .nav .nav-logout { margin-top: 0; }
  /* จอเล็กแสดงเมนูย่อยเรียงต่อกันในแถวเดียว ไม่มีหัวกลุ่ม */
  .nav-group, .nav-group .nav-sub, .nav-group.closed .nav-sub { display: contents; }
  .nav .nav-parent { display: none; }
}

/* ===== ส่วนประกอบ ===== */
.page-head { display: flex; flex-wrap: wrap; align-items: flex-end; justify-content: space-between; gap: .75rem; margin-bottom: var(--gap); }
.page-head p { margin: .2rem 0 0; color: var(--muted); }
.page-head:has(.with-icon) { align-items: flex-start; }
.page-head:has(.with-icon) > .row { margin-left: auto; }
.with-icon { gap: .4rem; }
.fold-head { margin-bottom: var(--gap); }
.head-line { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: flex-end; gap: .6rem; margin-bottom: .4rem; }
.head-line p { margin: 0; color: var(--muted); }
.head-line .row.end { margin-left: auto; justify-content: flex-end; }
.password { display: flex; gap: .3rem; align-items: stretch; }
.password input { flex: 1; min-width: 0; }
.password .reveal { padding: 0 .6rem; touch-action: none; user-select: none; }
.row { display: flex; flex-wrap: wrap; align-items: flex-end; gap: .6rem; }
.field { display: flex; flex-direction: column; gap: .2rem; font-size: .8rem; color: var(--muted); }
.field input, .field select { font-size: .9rem; color: var(--text); }
.panel { background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: var(--gap); margin-bottom: var(--gap); }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(10rem, 1fr)); gap: 1px; background: var(--line); border: 1px solid var(--line); border-radius: 10px; overflow: hidden; margin: 0 0 var(--gap); }
.stats div { background: var(--surface); padding: var(--pad) 1rem; }
.stats dt { color: var(--muted); } .stats dd { margin: 0; font-size: 1.7rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.chart { display: grid; grid-template-columns: repeat(6, minmax(0, 1fr)); gap: .5rem; text-align: center; font-size: .78rem; }
.chart svg { display: block; width: 100%; height: 5.5rem; border-bottom: 1px solid var(--line); }
.chart rect { fill: var(--brand); } .chart rect.late { fill: #d97706; }
.table-wrap { background: var(--surface); border: 1px solid var(--line); border-radius: 10px; overflow-x: auto; margin-bottom: .4rem; }
.pager { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: .5rem; margin-bottom: var(--gap); color: var(--muted); font-size: .8rem; font-variant-numeric: tabular-nums; }
.pager select, .pager button { min-height: 1.9rem; border-radius: 6px; color: var(--text); }
.pager button { min-width: 1.9rem; padding: 0 .4rem; }
.pager-nav { display: flex; align-items: center; gap: .3rem; }
table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
th, td { padding: var(--pad) .75rem; text-align: left; border-bottom: 1px solid var(--line); white-space: nowrap; }
th { background: var(--rail); color: var(--muted); font-size: .8rem; }
tbody tr:hover { background: var(--selected); color: var(--on-selected); }
tbody tr:last-child td { border-bottom: 0; }
.filter { margin-bottom: var(--pad); }
.found { margin: 0 0 var(--pad); color: var(--muted); font-size: .8rem; }
.empty { text-align: center; color: var(--muted); padding: 2rem 1rem; margin: 0; }
.badge { display: inline-block; border-radius: 6px; padding: 0 .5rem; margin-right: .25rem; font-size: .78rem; background: var(--rail); color: var(--muted); }
.badge.ok { background: var(--ok-bg); color: var(--ok); } .badge.warn { background: var(--warn-bg); color: var(--warn); } .badge.bad { background: var(--bad-bg); color: var(--bad); }
.notice { border-radius: 8px; padding: .5rem .8rem; margin: 0 0 var(--pad); }
.notice.info { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: .6rem; background: var(--selected); color: var(--on-selected); border: 1px solid var(--brand-line); margin-bottom: .6rem; }
.notice.ok { background: var(--ok-bg); color: var(--ok); } .notice.bad { background: var(--bad-bg); color: var(--bad); }
.loading { color: var(--muted); padding: 2rem 0; }

.login { min-height: 100vh; display: grid; place-items: center; padding: 1rem; }
.login form { width: min(22rem, 100%); display: flex; flex-direction: column; gap: .75rem; background: var(--surface); border: 1px solid var(--line); border-top: 6px solid var(--brand); border-radius: 10px; padding: 1.5rem; }

/* ===== สีปุ่มมาตรฐาน (คงที่ทุกธีม) ===== */
:is(button, .button).btn-save { background: #15803d; border-color: #15803d; color: #fff; }
:is(button, .button).btn-info { background: #1d4ed8; border-color: #1d4ed8; color: #fff; }
:is(button, .button).btn-warn { background: #b45309; border-color: #b45309; color: #fff; }
:is(button, .button).btn-delete { background: #b91c1c; border-color: #b91c1c; color: #fff; }
:is(button, .button).btn-neutral { background: #4b5563; border-color: #4b5563; color: #fff; }
:is(button, .button):is(.btn-save, .btn-info, .btn-warn, .btn-delete, .btn-neutral):hover:not(:disabled) { filter: brightness(1.12); }
.header button:is(.btn-info, .btn-neutral) { border-color: rgb(255 255 255 / .75); }
input[type="file"]::file-selector-button { font: inherit; margin-right: .6rem; border: 0; border-radius: 6px; padding: .2rem .7rem; background: #1d4ed8; color: #fff; cursor: pointer; }

/* ===== กล่องแจ้งผล ===== */
dialog.result { width: min(24rem, calc(100% - 2rem)); padding: 0; border: 1px solid var(--line); border-radius: 12px; background: var(--surface); color: var(--text); overflow: hidden; }
dialog.result::backdrop { background: rgb(10 15 22 / .45); }
.result h2 { margin: 0; padding: .55rem .9rem; color: #fff; font-size: 1rem; }
.result.ok h2 { background: #15803d; } .result.bad h2 { background: #b91c1c; } .result.ask h2 { background: #b45309; }
.result.ok h2::before { content: "✓  "; } .result.bad h2::before { content: "!  "; } .result.ask h2::before { content: "?  "; }
.result-body { padding: .75rem .9rem; overflow-wrap: anywhere; }
.result-body p { margin: 0 0 .4rem; }
.result-note { color: var(--muted); font-size: .85rem; }
.result-actions { display: flex; justify-content: flex-end; gap: .5rem; padding: 0 .9rem .75rem; }

/* ===== ดึงข้อมูลจากเครื่อง ===== */
.sync-bar { display: flex; flex-wrap: wrap; align-items: center; gap: .6rem; margin-bottom: .6rem; color: var(--muted); font-size: .85rem; }
.sync-bar progress { width: min(18rem, 100%); height: .7rem; accent-color: #1d4ed8; }
.check { display: inline-flex; align-items: center; gap: .4rem; margin-bottom: .5rem; }
.fold-body form > .row { margin-bottom: .5rem; }

/* ===== กล่องพับได้ ===== */
.fold { background: var(--surface); border: 1px solid var(--line); border-radius: 8px; margin-bottom: .6rem; }
.fold summary { display: flex; align-items: center; gap: .5rem; padding: .4rem .75rem; cursor: pointer; font-weight: 700; list-style: none; border-radius: 8px; }
.fold summary::-webkit-details-marker { display: none; }
.fold summary:hover { background: var(--rail); color: var(--text); }
.fold .chevron { margin-left: auto; color: var(--muted); transition: transform .15s; }
.fold[open] .chevron { transform: rotate(180deg); }
.fold-body { padding: .6rem .75rem; border-top: 1px solid var(--line); }
.fold-body > :last-child, .fold-body section > :last-child { margin-bottom: 0; }
.fold-body .stats, .fold-body figure.plain { margin: 0; }
.fold-body section > form { margin-bottom: .6rem; }

/* ===== ปฏิทิน พ.ศ. ===== */
.picker { position: relative; font-size: .9rem; color: var(--text); }
.picker-trigger { min-width: 10.5rem; justify-content: space-between; gap: .6rem; }
.picker-pop { position: absolute; z-index: 20; top: calc(100% + 4px); left: 0; width: 18rem; max-width: 90vw; background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: .6rem; box-shadow: 0 8px 24px rgb(10 15 22 / .18); }
.picker-pop.end { left: auto; right: 0; }
.picker-head, .picker-foot { display: flex; justify-content: space-between; align-items: center; gap: .5rem; }
.picker-foot { margin-top: .5rem; }
.picker-days, .picker-months { display: grid; gap: 2px; margin-top: .5rem; text-align: center; }
.picker-days { grid-template-columns: repeat(7, minmax(0, 1fr)); }
.picker-months { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.picker-days span { color: var(--muted); font-size: .75rem; }
.picker-days button, .picker-months button { padding: 0; min-height: 2.1rem; border-color: transparent; }
.picker-pop button[aria-current="date"] { border-color: var(--brand); }
.picker-pop button[aria-pressed="true"] { background: var(--brand); border-color: var(--brand); color: #fff; font-weight: 700; }

/* ===== แผงตั้งค่าหน้าตา ===== */
dialog.drawer { margin: 0 0 0 auto; height: 100%; max-height: 100%; width: min(19rem, 100%); border: 0; border-left: 1px solid var(--line); background: var(--surface); color: var(--text); padding: 0; font-size: .82rem; }
dialog.drawer::backdrop { background: rgb(10 15 22 / .45); }
.drawer h2 { font-size: .85rem; margin: 0 0 .35rem; }
.drawer button, .drawer input:not([type="range"]) { min-height: 1.9rem; border-radius: 6px; }
.drawer button { padding: 0 .5rem; }
.drawer-head { display: flex; justify-content: space-between; align-items: flex-start; padding: .6rem .75rem; border-bottom: 1px solid var(--line); }
.drawer-head p { margin: 0; color: var(--muted); font-size: .75rem; }
.drawer section { padding: .6rem .75rem; border-bottom: 1px solid var(--line); }
.drawer .row { flex-direction: column; align-items: stretch; gap: .4rem; }
.segment { display: grid; grid-template-columns: repeat(3, 1fr); }
.segment button { border-radius: 0 !important; }
.segment button:first-child { border-radius: 6px 0 0 6px !important; } .segment button:last-child { border-radius: 0 6px 6px 0 !important; }
.segment button[aria-pressed="true"] { background: var(--brand); border-color: var(--brand); color: #fff; }
.choices { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: .3rem; }
.choices button[aria-pressed="true"] { border-color: var(--brand); background: var(--selected); color: var(--on-selected); font-weight: 700; }
.theme-picker { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: .4rem; }
.theme-picker button, .theme-picker button:hover { color: #fff; border-color: transparent; }
.theme-picker button:hover { filter: brightness(1.12); }
.theme-picker .theme-brown { background: #7c4a2d; } .theme-picker .theme-blue { background: #1d4ed8; }
.theme-picker .theme-green { background: #047857; } .theme-picker .theme-violet { background: #6d28d9; }
.theme-picker .theme-orange { background: #c2500a; }
.theme-picker button[aria-pressed="true"] { outline: 2px solid var(--text); outline-offset: 1px; font-weight: 700; }
.theme-picker button[aria-pressed="true"]::before { content: "✓"; margin-right: .25rem; }
.between { display: flex; justify-content: space-between; align-items: center; }

@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }

/* ===== หน้าภาพรวมสาธารณะ (ก่อนเข้าสู่ระบบ) ===== */
.public { max-width: 64rem; margin: 0 auto; padding: 1.25rem 1rem 2rem; display: flex; flex-direction: column; gap: var(--gap); }
.public-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: .75rem; border-bottom: 4px solid var(--brand); padding-bottom: .75rem; }
.public-head h1 { font-size: 1.6rem; }
.public-company { margin: 0; font-size: 1.15rem; font-weight: 700; color: var(--brand-dark); }
[data-mode="dark"] .public-company { color: var(--brand-line); }
.public-photo { margin: 0; border-radius: 12px; overflow: hidden; border: 1px solid var(--line); background: var(--rail); aspect-ratio: 16 / 7; }
.public-photo img { width: 100%; height: 100%; object-fit: cover; display: block; }
.public-photo-empty { height: 100%; display: grid; place-items: center; color: var(--muted); font-size: 1.1rem; }
.public-devices { display: flex; flex-direction: column; gap: var(--gap); }
.public-card { background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: 1rem; }
.public-card h2 { display: flex; align-items: center; gap: .5rem; }
.public-card .stats { margin-bottom: .75rem; }
.public-card .stats small { color: var(--muted); }
.public-bar { height: .75rem; border-radius: 999px; background: var(--rail); overflow: hidden; margin-bottom: .5rem; }
.public-bar span { display: block; height: 100%; background: var(--brand); }
.public-bar.bad span { background: var(--bad); }
.public-card .muted { color: var(--muted); margin: 0; }
.public-warn { background: var(--bad-bg); color: var(--bad); border: 2px solid var(--bad); border-radius: 10px; padding: 1rem 1.25rem; }
.public-warn h2 { color: var(--bad); }
.public-warn p, .public-warn ol { margin: 0 0 .4rem; }
.public-warn ol { padding-left: 1.25rem; }
.link-button { border: 0; background: none; color: var(--link); text-decoration: underline; cursor: pointer; min-height: auto; padding: .25rem; }
@media (max-width: 40rem) { .public-photo { aspect-ratio: 4 / 3; } .public-head h1 { font-size: 1.3rem; } }
.muted-line { display: block; color: var(--muted); font-size: .75rem; white-space: normal; min-width: 9rem; max-width: 16rem; }

/* ลืมรหัสผ่าน / ตั้งรหัสใหม่ / เปลี่ยนรหัสผ่าน */
.notice.warn { background: var(--warn-bg); color: var(--warn); }
.login .hint { margin: 0; color: var(--muted); font-size: .85rem; line-height: 1.6; }
.login .notice { margin: 0; }
.login .remember .check { margin: 0; font-weight: 600; }
.login .remember .hint { font-size: .78rem; margin-top: .15rem; }
.login .remembered { font-size: .85rem; overflow-wrap: anywhere; }
.login .remembered .link-button { padding: 0; color: inherit; font-size: .82rem; }
.login .link-button { align-self: center; }
.stack { display: flex; flex-direction: column; gap: .6rem; }
