/* =====================================================================
   Photo Album — responsive UI
   จอเล็ก (มือถือ)      < 640px   : เมนูล่าง, กริด 3 คอลัมน์, dialog เป็น bottom sheet, ตารางเป็นการ์ด
   แท็บเล็ต (แนวตั้ง)    640–1023 : เมนูล่าง, กริด 4–5 คอลัมน์, sheet กว้าง 560px
   จอใหญ่ / แท็บเล็ตแนวนอน ≥ 1024 : เมนูบน, lightbox มีแผงข้อมูลด้านขวา
   ===================================================================== */
:root {
  --bg: #f6f5f2; --surface: #fff; --surface-2: #faf9f7; --ink: #1c1b19; --muted: #6f6b64; --line: #e4e1da;
  --accent: #0c367f; --accent-ink: #fff; --accent-soft: #e6edf8;
  --brand-red: #ad2547; --brand-sky: #319bd0; --brand-teal: #009788;
  --red: #b4372b; --red-soft: #fbe9e6; --green: #2f7a3d; --green-soft: #e5f2e6;
  --amber: #9a6a00; --amber-soft: #fbf1d9; --blue: #2a5ea8; --blue-soft: #e5edf8;
  --radius: 12px; --shadow: 0 1px 2px rgba(0,0,0,.05), 0 4px 16px rgba(0,0,0,.05);
  --tap: 44px;
  --top-h: 56px; --tab-h: 0px;
  --safe-b: env(safe-area-inset-bottom, 0px); --safe-t: env(safe-area-inset-top, 0px);
  --safe-l: env(safe-area-inset-left, 0px); --safe-r: env(safe-area-inset-right, 0px);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #161514; --surface: #201f1d; --surface-2: #1b1a18; --ink: #ecebe8; --muted: #a29d94; --line: #34322e;
    --accent: #7fb2ff; --accent-ink: #0a1630; --accent-soft: #17243d;
    --red: #ec7a6e; --red-soft: #3a1f1b; --green: #79c487; --green-soft: #1c2f1f;
    --amber: #e1b04a; --amber-soft: #352b14; --blue: #86aee8; --blue-soft: #1b2638;
    --shadow: 0 1px 2px rgba(0,0,0,.4);
  }
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; -webkit-tap-highlight-color: transparent; }
html.modal-open { overflow: hidden; }
body { margin: 0; background: var(--bg); color: var(--ink); font: 15px/1.55 "IBM Plex Sans Thai", system-ui, -apple-system, sans-serif; overscroll-behavior-y: none; }
a { color: var(--accent); text-decoration: none; }
@media (hover: hover) { a:hover { text-decoration: underline; } }
h1 { font-size: 1.55rem; margin: 0; line-height: 1.25; overflow-wrap: anywhere; }
h1 small { font-size: .85rem; font-weight: 400; margin-left: .4rem; }
h2 { font-size: 1.15rem; margin: 1.6rem 0 .6rem; }
h3 { font-size: 1.05rem; margin: 0 0 .8rem; }
.muted { color: var(--muted); }
.small, small { font-size: .82rem; }
.nowrap { white-space: nowrap; }
.mt { margin-top: 1.4rem; }
.row { display: flex; flex-wrap: wrap; gap: .5rem; align-items: center; }
.inline { display: inline; }
.grow { flex: 1; }
code { font-size: .85em; background: var(--bg); padding: .1em .35em; border-radius: 4px; overflow-wrap: anywhere; }
.ic { flex: none; }

/* แสดง/ซ่อนตามขนาดจอ และตามชนิด pointer */
@media (min-width: 1024px) { .only-compact { display: none !important; } }
@media (max-width: 1023px) { .only-wide { display: none !important; } }
@media (pointer: coarse) { .mouse-only { display: none !important; } }
@media (pointer: fine) { .touch-only { display: none !important; } }

/* ================================================================ header */
.top { position: sticky; top: 0; z-index: 30; display: flex; align-items: center; gap: 1.2rem;
  min-height: calc(var(--top-h) + var(--safe-t)); padding: var(--safe-t) calc(1.2rem + var(--safe-r)) 0 calc(1.2rem + var(--safe-l));
  background: color-mix(in srgb, var(--surface) 92%, transparent); backdrop-filter: saturate(1.4) blur(12px); -webkit-backdrop-filter: saturate(1.4) blur(12px);
  border-bottom: 1px solid var(--line); }
.brand { display: flex; align-items: center; gap: .5rem; font-weight: 700; color: var(--ink); white-space: nowrap; }
.brand .ic { color: var(--accent); }
.brand:hover { text-decoration: none; }
.topnav { display: flex; gap: .2rem; flex: 1; }
.topnav a { padding: .45rem .8rem; border-radius: 8px; color: var(--muted); white-space: nowrap; }
.topnav a:hover { background: var(--bg); text-decoration: none; color: var(--ink); }
.topnav a.on { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.me { display: flex; align-items: center; gap: .7rem; margin-left: auto; }
.me > a { display: flex; align-items: center; gap: .5rem; color: var(--ink); }
.me > a:hover { text-decoration: none; }
.avatar { width: 34px; height: 34px; border-radius: 50%; display: grid; place-items: center; background: var(--accent); color: var(--accent-ink); font-weight: 600; flex: none; }
.avatar.lg { width: 48px; height: 48px; font-size: 1.2rem; }
.avatar.sm { width: 32px; height: 32px; font-size: .85rem; }
.who { display: flex; flex-direction: column; line-height: 1.2; font-size: .88rem; }
.who small { color: var(--muted); font-size: .75rem; }

/* แถบเมนูล่าง */
.tabbar { display: none; }
@media (max-width: 1023px) {
  :root { --top-h: 52px; --tab-h: 62px; }
  .top { gap: .6rem; }
  .topnav, .logout-top, .who { display: none; }
  .tabbar { position: fixed; left: 0; right: 0; bottom: 0; z-index: 30; display: flex; justify-content: space-around;
    height: calc(var(--tab-h) + var(--safe-b)); padding: 0 var(--safe-r) var(--safe-b) var(--safe-l);
    background: color-mix(in srgb, var(--surface) 94%, transparent); backdrop-filter: saturate(1.4) blur(14px); -webkit-backdrop-filter: saturate(1.4) blur(14px);
    border-top: 1px solid var(--line); }
  .tabbar a, .tabbar button { flex: 1; max-width: 140px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
    background: none; border: 0; font: inherit; font-size: .72rem; color: var(--muted); cursor: pointer; padding: 0; }
  .tabbar a:hover { text-decoration: none; }
  .tabbar .on { color: var(--accent); font-weight: 600; }
  .tabbar .on .ic { stroke-width: 2.2; }
  .tabbar a:active, .tabbar button:active { opacity: .6; }
  body.authed { padding-bottom: calc(var(--tab-h) + var(--safe-b)); }
}
@media (min-width: 640px) and (max-width: 1023px) {
  .tabbar a, .tabbar button { flex-direction: row; gap: .45rem; font-size: .85rem; }
}

.wrap { max-width: 1280px; margin: 0 auto; padding: 1.4rem calc(1.2rem + var(--safe-r)) 3rem calc(1.2rem + var(--safe-l)); }
@media (max-width: 639px) { .wrap { padding: .9rem calc(.8rem + var(--safe-r)) 2rem calc(.8rem + var(--safe-l)); } h1 { font-size: 1.3rem; } }
.foot { text-align: center; color: var(--muted); font-size: .8rem; padding: 1.5rem 0 2rem; }
@media (max-width: 1023px) { .authed .foot { display: none; } }
.page-head { display: flex; align-items: center; justify-content: space-between; gap: .8rem 1rem; flex-wrap: wrap; margin-bottom: 1.1rem; }
@media (max-width: 639px) { .page-head h1 small { display: block; margin: .15rem 0 0; } }

/* ================================================================ controls */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: .4rem; min-height: 40px; padding: .45rem .95rem; border-radius: 10px;
  border: 1px solid var(--line); background: var(--surface); color: var(--ink); font: inherit; font-weight: 500; cursor: pointer; white-space: nowrap;
  touch-action: manipulation; user-select: none; -webkit-user-select: none; }
@media (hover: hover) { .btn:hover { border-color: var(--muted); text-decoration: none; } }
.btn:active { transform: scale(.98); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.danger { color: var(--red); }
.btn.small { min-height: 34px; padding: .25rem .7rem; font-size: .85rem; }
.btn.big { min-height: 52px; padding: .6rem 1.3rem; font-size: 1rem; border-radius: 12px; }
.btn.block { width: 100%; }
.btn:disabled { opacity: .4; cursor: not-allowed; }
@media (pointer: coarse) { .btn { min-height: var(--tap); } .btn.small { min-height: 38px; } }
.link { background: none; border: 0; padding: 0; color: var(--accent); font: inherit; cursor: pointer; }
.link.danger { color: var(--red); }
.icon-btn { display: inline-grid; place-items: center; width: var(--tap); height: var(--tap); border-radius: 50%; border: 0; background: none; color: inherit; cursor: pointer; flex: none; }
.icon-btn:active { background: rgba(127,127,127,.18); }
.icon-btn.danger { color: var(--red); }
label { display: block; font-size: .88rem; font-weight: 500; margin-bottom: .8rem; }
label.check { display: flex; align-items: center; gap: .6rem; margin-top: 1.7rem; min-height: var(--tap); }
/* font-size ≥16px กัน iOS ซูมเองตอนแตะช่องกรอก */
input, select, textarea { font: inherit; font-size: 16px; color: var(--ink); background: var(--surface); border: 1px solid var(--line); border-radius: 10px;
  padding: .55rem .7rem; width: 100%; margin-top: .25rem; min-height: var(--tap); }
textarea { min-height: auto; resize: vertical; }
input[type=checkbox] { width: 22px; height: 22px; min-height: 0; margin: 0; accent-color: var(--accent); }
input:focus, select:focus, textarea:focus { outline: 3px solid var(--accent-soft); border-color: var(--accent); }
@media (pointer: fine) and (min-width: 1024px) { input, select, textarea { font-size: 15px; min-height: 38px; } }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 1rem; }
@media (max-width: 639px) { .grid-2 { grid-template-columns: 1fr; } }

.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 1.2rem; box-shadow: var(--shadow); }
@media (max-width: 639px) { .card { padding: 1rem; } }
.flash { padding: .75rem 1rem; border-radius: 10px; margin-bottom: 1rem; border: 1px solid; overflow-wrap: anywhere; }
.flash.ok { background: var(--green-soft); border-color: var(--green); color: var(--green); }
.flash.err { background: var(--red-soft); border-color: var(--red); color: var(--red); }
.flash.warn { background: var(--amber-soft); border-color: var(--amber); color: var(--amber); }
.empty { text-align: center; padding: 3rem 1rem; color: var(--muted); }
.small-empty { padding: 1.5rem 1rem; }

.badge { display: inline-block; padding: .12rem .55rem; border-radius: 99px; font-size: .75rem; font-weight: 500; background: var(--bg); color: var(--muted); border: 1px solid var(--line); white-space: nowrap; }
.b-red, .vis-private { background: var(--red-soft); color: var(--red); border-color: transparent; }
.b-green, .vis-company { background: var(--green-soft); color: var(--green); border-color: transparent; }
.b-amber, .vis-department { background: var(--amber-soft); color: var(--amber); border-color: transparent; }
.b-blue, .lv { background: var(--blue-soft); color: var(--blue); border-color: transparent; }
.role-admin { background: var(--red-soft); color: var(--red); border: 0; }
.role-manager { background: var(--amber-soft); color: var(--amber); border: 0; }
.role-staff { background: var(--accent-soft); color: var(--accent); border: 0; }

/* ================================================================ login */
.login { min-height: 85vh; min-height: 85dvh; display: grid; place-items: center; padding: var(--safe-t) 0 var(--safe-b); }
.login .card { width: min(400px, 100%); padding: 2rem 1.6rem; }
.login h1 { text-align: center; }
.login p { text-align: center; margin: .2rem 0 1.4rem; }
.login-logo { display: grid; place-items: center; color: var(--accent); margin-bottom: .6rem; }
@media (max-width: 639px) { .login .card { border: 0; box-shadow: none; background: transparent; } }

/* ================================================================ albums */
.toolbar { display: flex; gap: .8rem; flex-wrap: wrap; align-items: center; justify-content: space-between; margin-bottom: 1.2rem; }
.toolbar input[type=search] { max-width: 320px; margin: 0; }
.tabs { display: flex; gap: .2rem; background: var(--surface); border: 1px solid var(--line); border-radius: 11px; padding: .2rem; overflow-x: auto; scrollbar-width: none; }
.tabs::-webkit-scrollbar { display: none; }
.tabs a { padding: .4rem .85rem; border-radius: 8px; color: var(--muted); font-size: .9rem; white-space: nowrap; display: flex; align-items: center; min-height: 36px; }
.tabs a.on { background: var(--accent); color: var(--accent-ink); }
.tabs a:hover { text-decoration: none; }
@media (max-width: 639px) {
  .toolbar { flex-direction: column; align-items: stretch; gap: .6rem; }
  .toolbar input[type=search] { max-width: none; }
  .tabs a { flex: 1; justify-content: center; }
}
.albums { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 1.1rem; }
@media (max-width: 1023px) { .albums { grid-template-columns: repeat(3, 1fr); gap: .9rem; } }
@media (max-width: 760px) { .albums { grid-template-columns: repeat(2, 1fr); gap: .7rem; } }
.album-card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; color: var(--ink);
  box-shadow: var(--shadow); transition: transform .15s, box-shadow .15s; display: flex; flex-direction: column; }
.album-card:hover { text-decoration: none; }
@media (hover: hover) { .album-card:hover { transform: translateY(-2px); box-shadow: 0 8px 24px rgba(0,0,0,.09); } }
.album-card:active { transform: scale(.985); }
.cover { position: relative; aspect-ratio: 4/3; background: var(--bg); overflow: hidden; flex: none; }
.cover img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; display: block; }
.nocover { position: absolute; inset: 0; display: grid; place-items: center; color: var(--muted); font-size: .85rem; }
.count { position: absolute; right: .5rem; bottom: .5rem; background: rgba(0,0,0,.6); color: #fff; font-size: .75rem; padding: .1rem .5rem; border-radius: 99px; }
.album-card .meta { padding: .75rem .9rem .9rem; display: flex; flex-direction: column; gap: .2rem; min-width: 0; }
.album-card .meta strong { overflow: hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; line-height: 1.35; }
.album-card .muted { font-size: .8rem; }
@media (max-width: 639px) {
  .album-card .meta { padding: .55rem .65rem .7rem; }
  .album-card .meta strong { font-size: .92rem; }
  .album-card .owner { display: none; }
}

/* หน้าอัลบั้ม */
.album-top { display: flex; align-items: center; justify-content: space-between; margin: -.3rem 0 .4rem -.4rem; }
.back { display: inline-flex; align-items: center; gap: .1rem; color: var(--muted); min-height: var(--tap); padding-right: .6rem; font-size: .92rem; }
.back:hover { text-decoration: none; color: var(--ink); }
.album-head { display: flex; justify-content: space-between; gap: 1.2rem; flex-wrap: wrap; margin-bottom: 1.2rem; }
.album-title { min-width: 0; flex: 1; }
.album-head p { margin: .35rem 0; }
.meta-line { display: flex; flex-wrap: wrap; gap: .1rem .3rem; color: var(--muted); font-size: .9rem; }
.meta-line span:not(:last-child)::after { content: "·"; margin-left: .3rem; }
.desc { max-width: 70ch; }
.od-path { overflow-wrap: anywhere; }
.head-actions { display: flex; gap: .5rem; flex-wrap: wrap; align-items: flex-start; }

.drop { border: 2px dashed var(--line); border-radius: var(--radius); padding: 1.2rem; text-align: center; background: var(--surface); margin-bottom: 1rem; transition: .15s; }
.drop.over { border-color: var(--accent); background: var(--accent-soft); }
.drop p { margin: .35rem 0 0; }
.drop-btns { display: flex; gap: .6rem; justify-content: center; flex-wrap: wrap; }
@media (pointer: coarse) {
  .drop { border-style: solid; border-width: 1px; padding: .9rem; }
  .drop-btns .btn { flex: 1; max-width: 260px; }
}

.grid-tools { display: flex; align-items: center; justify-content: space-between; margin: 0 0 .5rem; min-height: 38px; }
.photos { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: .5rem; }
@media (max-width: 1023px) { .photos { grid-template-columns: repeat(5, 1fr); gap: 4px; } }
@media (max-width: 820px) { .photos { grid-template-columns: repeat(4, 1fr); } }
@media (max-width: 639px) { .photos { grid-template-columns: repeat(3, 1fr); gap: 2px; margin: 0 calc(-.8rem + 2px); } }
.ph { position: relative; margin: 0; aspect-ratio: 1; background: var(--line); border-radius: 6px; overflow: hidden; cursor: zoom-in;
  user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; touch-action: manipulation; }
@media (max-width: 639px) { .ph { border-radius: 0; } }
.ph img { width: 100%; height: 100%; object-fit: cover; display: block; transition: transform .2s; pointer-events: none; }
@media (hover: hover) { .ph:hover img { transform: scale(1.03); } }
.sync { position: absolute; left: .35rem; bottom: .3rem; font-size: .8rem; text-shadow: 0 0 3px #000; color: #fff; pointer-events: none; }
.sync.s-failed { color: #ffb4a8; }
.pick { position: absolute; top: .4rem; right: .4rem; width: 26px; height: 26px; border-radius: 50%; display: grid; place-items: center;
  border: 2px solid #fff; background: rgba(0,0,0,.25); color: transparent; opacity: 0; transition: .12s; box-shadow: 0 0 3px rgba(0,0,0,.4); }
@media (hover: hover) { .ph:hover .pick { opacity: 1; } }
.selecting .pick { opacity: 1; }
.selecting .ph { cursor: pointer; }
.ph.sel .pick { background: var(--accent); border-color: #fff; color: #fff; }
.ph.sel img { transform: scale(.88); border-radius: 6px; }
.ph.sel { background: var(--accent-soft); }

/* แถบคำสั่งเมื่อเลือกรูป */
.sel-bar { position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(1rem + var(--tab-h) + var(--safe-b)); z-index: 35;
  display: flex; gap: .4rem .8rem; align-items: center; background: var(--ink); color: var(--bg); width: min(720px, calc(100% - 1.6rem));
  padding: .4rem .6rem .4rem .3rem; border-radius: 16px; box-shadow: 0 10px 30px rgba(0,0,0,.25); }
.sel-bar .link { color: var(--bg); text-decoration: underline; font-size: .88rem; }
.sel-actions { margin-left: auto; display: flex; gap: .2rem; }
.sbtn { display: flex; align-items: center; gap: .35rem; background: none; border: 0; color: inherit; font: inherit; font-size: .88rem; padding: .5rem .7rem; border-radius: 10px; cursor: pointer; min-height: var(--tap); }
.sbtn:not(:disabled):active, .sbtn:not(:disabled):hover { background: rgba(127,127,127,.25); }
.sbtn:disabled { opacity: .35; }
.sbtn.danger { color: #ff9b90; }
@media (max-width: 639px) {
  .sel-bar { left: 0; right: 0; transform: none; width: auto; bottom: 0; border-radius: 16px 16px 0 0; flex-wrap: wrap;
    padding: .4rem .5rem calc(.4rem + var(--tab-h) + var(--safe-b)); }
  .sel-actions { width: 100%; justify-content: space-around; margin: 0; }
  .sbtn { flex-direction: column; gap: 0; font-size: .72rem; flex: 1; }
}
body.selecting .fab { display: none; }

/* ปุ่มลอยเพิ่มรูป */
.fab { display: none; }
@media (max-width: 1023px) {
  .fab { position: fixed; right: calc(1rem + var(--safe-r)); bottom: calc(1rem + var(--tab-h) + var(--safe-b)); z-index: 25; display: grid; place-items: center;
    width: 58px; height: 58px; border-radius: 18px; border: 0; background: var(--accent); color: var(--accent-ink); box-shadow: 0 8px 22px rgba(12,54,127,.35); cursor: pointer; }
  .fab:active { transform: scale(.94); }
  .has-fab .wrap { padding-bottom: 6rem; }
}

/* แผงอัปโหลด */
.upload-panel { position: fixed; z-index: 36; right: 1.2rem; bottom: 1.2rem; width: 380px; background: var(--surface); border: 1px solid var(--line);
  border-radius: 16px; box-shadow: 0 14px 40px rgba(0,0,0,.18); padding: .7rem .8rem; }
.upload-panel.has-err { border-color: var(--red); }
@media (max-width: 1023px) {
  .upload-panel { left: .6rem; right: .6rem; width: auto; bottom: calc(.6rem + var(--tab-h) + var(--safe-b)); max-width: 560px; margin: 0 auto; }
}
.up-head { display: flex; align-items: center; gap: .7rem; }
.up-text { flex: 1; display: flex; flex-direction: column; line-height: 1.3; min-width: 0; }
.up-ring { width: 38px; height: 38px; flex: none; }
.up-ring svg { width: 100%; height: 100%; transform: rotate(-90deg); }
.up-ring circle { fill: none; stroke-width: 3.5; stroke: var(--line); }
.up-ring #up-arc { stroke: var(--accent); stroke-linecap: round; stroke-dasharray: 0 100; transition: stroke-dasharray .2s; }
.has-err .up-ring #up-arc { stroke: var(--amber); }
.queue { margin-top: .6rem; display: grid; gap: .35rem; max-height: 38vh; overflow: auto; overscroll-behavior: contain; }
.qrow { display: grid; grid-template-columns: 1fr 70px; grid-template-areas: "n s" "b b"; gap: .15rem .6rem; align-items: center; font-size: .82rem; }
.qn { grid-area: n; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.qs { grid-area: s; text-align: right; color: var(--muted); }
.qbar { grid-area: b; height: 4px; background: var(--bg); border-radius: 3px; overflow: hidden; }
.qbar i { display: block; height: 100%; width: 0; background: var(--accent); transition: width .2s; }
.qrow.ok .qs { color: var(--green); } .qrow.err .qs { color: var(--red); grid-column: 1 / -1; text-align: left; }
.qrow.err { grid-template-areas: "n n" "s s"; } .qrow.err .qbar { display: none; }

/* ================================================================ dialogs + bottom sheets */
dialog { border: 0; border-radius: 16px; padding: 0; background: var(--surface); color: var(--ink); box-shadow: 0 20px 60px rgba(0,0,0,.3);
  width: min(560px, calc(100% - 2rem)); max-height: calc(100dvh - 2rem); overflow: auto; overscroll-behavior: contain; }
dialog::backdrop { background: rgba(10,10,10,.5); }
.dlg { padding: 1.4rem; }
.dlg-actions { display: flex; justify-content: flex-end; gap: .5rem; margin-top: 1rem; }
.sheet-grip { display: none; }
.sheet { padding: 1rem 1.2rem 1.2rem; width: min(420px, calc(100% - 2rem)); }
.sheet h3 { margin: .2rem 0 .8rem; }
.sheet-me { display: flex; gap: .8rem; align-items: center; padding: .2rem .2rem 1rem; border-bottom: 1px solid var(--line); margin-bottom: .4rem; }
.sheet-list { display: flex; flex-direction: column; margin-bottom: .8rem; }
.sheet-list a, .sheet-list button { display: flex; align-items: center; gap: .9rem; min-height: 52px; padding: 0 .4rem; color: var(--ink); background: none; border: 0;
  border-radius: 10px; font: inherit; font-size: 1rem; text-align: left; cursor: pointer; width: 100%; }
.sheet-list a:hover { text-decoration: none; }
.sheet-list a:active, .sheet-list button:active { background: var(--bg); }
.sheet-list .ic { color: var(--muted); }
.sheet-list .on { color: var(--accent); font-weight: 600; } .sheet-list .on .ic { color: var(--accent); }
.sheet-list .danger, .sheet-list .danger .ic { color: var(--red); }
.sheet-list form { margin: 0; }

@media (max-width: 1023px) {
  dialog.sheet, dialog.sheet-md { margin: auto auto 0; width: 100%; max-width: 560px; max-height: 88dvh; border-radius: 20px 20px 0 0;
    padding-bottom: var(--safe-b); animation: sheet-in .22s cubic-bezier(.2,.8,.3,1); }
  dialog.sheet { padding: .5rem 1rem calc(1rem + var(--safe-b)); }
  dialog.closing { animation: sheet-out .18s ease-in forwards; }
  dialog.closing::backdrop { opacity: 0; transition: opacity .18s; }
  .sheet-grip { display: block; width: 40px; height: 5px; border-radius: 3px; background: var(--line); margin: .1rem auto .8rem; cursor: grab; }
  .sheet-md .dlg { padding: .6rem 1.1rem 1.2rem; }
  .sheet-md .dlg-actions { position: sticky; bottom: calc(-1.2rem - var(--safe-b)); background: var(--surface); padding: .7rem 0 calc(.2rem + var(--safe-b)); margin-bottom: calc(-1 * var(--safe-b)); }
  .sheet-md .dlg-actions .btn { flex: 1; }
}
@keyframes sheet-in { from { transform: translateY(100%); } to { transform: none; } }
@keyframes sheet-out { to { transform: translateY(100%); } }

.member-list { list-style: none; padding: 0; margin: 0 0 .5rem; }
.member-list li { display: flex; align-items: center; gap: .7rem; padding: .55rem 0; border-bottom: 1px solid var(--line); }
.member-list .mi { flex: 1; min-width: 0; display: flex; flex-direction: column; line-height: 1.3; }
.member-list .mi small { overflow: hidden; text-overflow: ellipsis; }
.member-list select { width: auto; margin: 0; padding-right: 1.8rem; }
@media (max-width: 480px) {
  .member-list li { flex-wrap: wrap; }
  .member-list .mr { order: 3; flex: 1 0 calc(100% - 3rem); margin-left: 2.7rem; }
  .member-list .mr select { width: 100%; }
}
.add-member { display: grid; grid-template-columns: 1fr 150px auto; gap: .5rem; align-items: end; margin-top: 1rem; }
.add-member input, .add-member select { margin: 0; }
@media (max-width: 560px) { .add-member { grid-template-columns: 1fr 1fr; } .add-member input { grid-column: 1 / -1; } }

/* ================================================================ lightbox */
dialog.lb { width: 100vw; height: 100vh; height: 100dvh; max-width: none; max-height: none; border-radius: 0; background: rgba(8,8,8,var(--bgop, 1));
  color: #eee; display: none; overflow: hidden; }
dialog.lb::backdrop { background: #000; }
dialog.lb[open] { display: grid; grid-template-columns: minmax(0, 1fr) 0; grid-template-rows: minmax(0, 1fr); }
dialog.lb.info-open[open] { grid-template-columns: minmax(0, 1fr) 340px; }
.lb-bar { position: absolute; top: 0; left: 0; right: 0; z-index: 3; display: flex; align-items: center; gap: .2rem;
  padding: calc(.4rem + var(--safe-t)) calc(.5rem + var(--safe-r)) .4rem calc(.5rem + var(--safe-l));
  background: linear-gradient(rgba(0,0,0,.55), transparent); transition: opacity .2s; }
.info-open .lb-bar { right: 340px; }
.lb-bar .icon-btn { color: #fff; }
.lb-count { font-size: .9rem; color: #ccc; margin-left: .3rem; font-variant-numeric: tabular-nums; }
.lb-stage { grid-column: 1; grid-row: 1; display: flex; align-items: center; justify-content: center; min-height: 0; min-width: 0; height: 100%; width: 100%; overflow: hidden; touch-action: none; user-select: none; -webkit-user-select: none; }
.lb-stage img { max-width: 100%; max-height: 100%; object-fit: contain; transform-origin: center; will-change: transform; -webkit-user-drag: none; transition: opacity .15s; }
.lb-stage img.loading { opacity: .4; }
.lb.zoomed .lb-stage { cursor: grab; }
.lb-info { grid-column: 2; grid-row: 1; background: #181818; padding: calc(1.2rem + var(--safe-t)) 1.2rem calc(1.2rem + var(--safe-b)); overflow: auto; border-left: 1px solid #2a2a2a; min-width: 0; }
.lb:not(.info-open) .lb-info { display: none; }
.lb-info h4 { margin: 0 0 .5rem; overflow-wrap: anywhere; }
.lb-info textarea { background: #222; color: #eee; border-color: #333; margin: .2rem 0 .9rem; }
.lb-info .kv dt { color: #999; }
.lb-info a:not(.btn) { color: #6fd3c8; }
.lb-info .btn { background: #262626; border-color: #3a3a3a; color: #eee; }
.lb-info .btn.danger { color: #ff8a7d; }
.lb-actions { display: flex; flex-wrap: wrap; gap: .4rem; margin-top: 1rem; }
.lb-nav { position: absolute; top: 50%; transform: translateY(-50%); z-index: 2; background: rgba(255,255,255,.12); color: #fff; border: 0; border-radius: 50%;
  width: 48px; height: 48px; font-size: 1.6rem; line-height: 1; cursor: pointer; transition: opacity .2s; }
.lb-nav.prev { left: calc(.8rem + var(--safe-l)); }
.lb-nav.next { right: calc(.8rem + var(--safe-r)); }
.info-open .lb-nav.next { right: calc(340px + .8rem); }
.lb-nav:hover { background: rgba(255,255,255,.25); }
.lb.chrome-hidden .lb-bar, .lb.chrome-hidden .lb-nav { opacity: 0; pointer-events: none; }
.lb.zoomed .lb-nav { opacity: 0; pointer-events: none; }

@media (max-width: 1023px), (pointer: coarse) and (max-height: 600px) {
  dialog.lb[open], dialog.lb.info-open[open] { grid-template-columns: minmax(0, 1fr); }
  .info-open .lb-bar { right: 0; }
  .info-open .lb-nav.next { right: calc(.8rem + var(--safe-r)); }
  .lb-info { grid-column: 1; position: absolute; left: 0; right: 0; bottom: 0; z-index: 4; max-height: 62dvh; border-left: 0; border-radius: 18px 18px 0 0;
    padding: .5rem 1.1rem calc(1.1rem + var(--safe-b)); box-shadow: 0 -10px 30px rgba(0,0,0,.5); animation: sheet-in .2s ease-out; }
  .lb-info .sheet-grip { background: #444; }
}
@media (pointer: coarse) { .lb-nav { display: none; } }

/* ================================================================ tables / logs */
.kv { display: grid; grid-template-columns: max-content 1fr; gap: .35rem 1rem; margin: 0 0 1rem; font-size: .9rem; }
.kv dt { color: var(--muted); } .kv dd { margin: 0; overflow-wrap: anywhere; min-width: 0; }
.tbl-wrap { overflow-x: auto; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); -webkit-overflow-scrolling: touch; }
.tbl { width: 100%; border-collapse: collapse; font-size: .9rem; }
.tbl th { text-align: left; font-weight: 600; color: var(--muted); font-size: .8rem; padding: .6rem .8rem; border-bottom: 1px solid var(--line); background: var(--surface-2); white-space: nowrap; }
.tbl td { padding: .65rem .8rem; border-bottom: 1px solid var(--line); vertical-align: top; }
.tbl tr:last-child td { border-bottom: 0; }
.tbl tr.off td { opacity: .5; }
.tbl.log td > div { margin-top: .2rem; }
.tbl details summary { cursor: pointer; font-size: .8rem; color: var(--accent); margin-top: .25rem; min-height: 28px; display: flex; align-items: center; }
.tbl pre { font-size: .75rem; background: var(--bg); padding: .5rem; border-radius: 6px; max-width: 520px; overflow: auto; white-space: pre-wrap; overflow-wrap: anywhere; }
.err-text { color: var(--red); max-width: 380px; }
.actions-cell { display: flex; flex-wrap: wrap; gap: .3rem .9rem; align-items: center; }
.actions-cell form { margin: 0; }

/* ตาราง → การ์ด บนจอเล็ก (td ใส่ data-label) */
@media (max-width: 760px) {
  .tbl-wrap.stack { background: none; border: 0; overflow: visible; }
  .stack .tbl, .stack .tbl tbody, .stack .tbl tr, .stack .tbl td { display: block; width: 100%; }
  .stack .tbl thead { display: none; }
  .stack .tbl tr { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: .7rem .9rem; margin-bottom: .6rem; box-shadow: var(--shadow); }
  /* ป้ายชื่อคอลัมน์วางซ้ายแบบ absolute — เนื้อหาในเซลล์จะเป็นอะไรก็ได้ไม่แตกบรรทัดผิด */
  .stack .tbl td { border: 0; padding: .22rem 0 .22rem 6.8rem; position: relative; min-height: 1.6rem; overflow-wrap: anywhere; }
  .stack .tbl td[data-label=""], .stack .tbl td.full { padding-left: 0; }
  .stack .tbl td::before { content: attr(data-label); position: absolute; left: 0; top: .3rem; width: 6.3rem; color: var(--muted); font-size: .78rem; }
  .stack .tbl td[data-label=""]::before, .stack .tbl td.full::before { display: none; }
  .stack .tbl tr { display: flex; flex-direction: column; }
  .stack .tbl td.head { order: -1; }
  .stack .tbl td.head { font-size: 1rem; padding-bottom: .35rem; margin-bottom: .2rem; border-bottom: 1px dashed var(--line); }
  .stack .tbl pre { max-width: none; }
}

.filters { display: flex; flex-wrap: wrap; gap: .6rem; align-items: flex-end; margin-bottom: 1rem; }
.filters label { margin: 0; font-size: .8rem; display: flex; flex-direction: column; }
.filters input, .filters select { width: auto; min-width: 140px; }
.filter-box { margin-bottom: 1rem; }
.filter-box > summary { display: none; }
@media (max-width: 760px) {
  .filter-box { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); }
  .filter-box > summary { display: flex; align-items: center; justify-content: space-between; min-height: var(--tap); padding: 0 1rem; cursor: pointer; font-weight: 500; list-style: none; }
  .filter-box > summary::-webkit-details-marker { display: none; }
  .filter-box > summary::after { content: "▾"; color: var(--muted); transition: transform .15s; }
  .filter-box[open] > summary::after { transform: rotate(180deg); }
  .filter-box .filters { display: grid; grid-template-columns: 1fr 1fr; padding: 0 1rem 1rem; margin: 0; }
  .filter-box .filters input, .filter-box .filters select { width: 100%; min-width: 0; }
  .filter-box .filters .wide, .filter-box .filters .btn { grid-column: 1 / -1; }
}
.pager { display: flex; gap: .3rem; align-items: center; margin-top: 1rem; flex-wrap: wrap; }
.pager a { min-width: 40px; min-height: 40px; display: grid; place-items: center; border-radius: 8px; border: 1px solid var(--line); background: var(--surface); }
.pager a.on { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.pager a:hover { text-decoration: none; }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(110px, 1fr)); gap: .6rem; margin-bottom: 1rem; }
@media (max-width: 639px) { .stats { grid-template-columns: repeat(3, 1fr); gap: .4rem; } .stats > div { padding: .55rem .65rem; } .stats b { font-size: 1.1rem !important; } }
.stats > div { background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: .7rem .9rem; display: flex; flex-direction: column; min-width: 0; }
.card .stats > div { background: var(--bg); }
.stats b { font-size: 1.35rem; font-variant-numeric: tabular-nums; }
.stats span { color: var(--muted); font-size: .78rem; line-height: 1.3; }
.stats .warn b { color: var(--red); }
.timeline { list-style: none; padding: 0; margin: 0 0 .6rem; font-size: .88rem; }
.timeline li { display: grid; grid-template-columns: 140px 1fr auto; gap: .8rem; padding: .5rem 0; border-bottom: 1px solid var(--line); }
@media (max-width: 760px) { .timeline li { grid-template-columns: 1fr; gap: 0; } .timeline .t { font-size: .78rem; } }
.timeline .t { color: var(--muted); }
.legend summary { cursor: pointer; font-weight: 600; min-height: 32px; display: flex; align-items: center; }
.legend { margin-bottom: 1rem; padding: .8rem 1.2rem; }
.legend .tbl-wrap { margin-top: .8rem; }
.trash-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: .6rem; }
.trash-item { display: grid; grid-template-columns: auto 72px 1fr; gap: .7rem; align-items: center; background: var(--surface); border: 1px solid var(--line);
  border-radius: 12px; padding: .55rem .7rem; margin: 0; font-weight: 400; cursor: pointer; }
.trash-item:has(input:checked) { border-color: var(--accent); background: var(--accent-soft); }
.trash-item img { width: 72px; height: 72px; object-fit: cover; border-radius: 8px; }
.trash-item span { min-width: 0; overflow-wrap: anywhere; }
.sticky-submit { position: sticky; bottom: calc(.8rem + var(--tab-h) + var(--safe-b)); margin-top: 1rem; z-index: 5; }
@media (max-width: 639px) { .sticky-submit .btn { width: 100%; box-shadow: 0 8px 20px rgba(0,0,0,.2); } }
.howto { padding-left: 1.2rem; } .howto li { margin-bottom: .5rem; overflow-wrap: anywhere; }

.toast { position: fixed; left: 50%; bottom: calc(1.5rem + var(--tab-h) + var(--safe-b)); transform: translate(-50%, 20px); opacity: 0; background: var(--ink); color: var(--bg);
  padding: .7rem 1.1rem; border-radius: 12px; z-index: 100; transition: .25s; max-width: calc(100% - 2rem); text-align: center; pointer-events: none; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.err { background: var(--red); color: #fff; }
body.selecting .toast { bottom: calc(6rem + var(--tab-h) + var(--safe-b)); }

@media (prefers-reduced-motion: reduce) { *, *::before, *::after { animation: none !important; transition: none !important; } }


/* ================================================================ brand / logo */
.brand-logo { display: block; line-height: 0; }
.brand-logo img { height: 34px; width: auto; display: block; }
.brand-name { padding-left: .7rem; border-left: 1px solid var(--line); font-weight: 600; }
@media (max-width: 639px) { .brand-logo img { height: 28px; } .brand-name { font-size: .92rem; padding-left: .55rem; } }
.top::after { content: ""; position: absolute; left: 0; right: 0; bottom: -1px; height: 2px;
  background: linear-gradient(90deg, var(--brand-red) 0 33.3%, var(--brand-sky) 33.3% 66.6%, var(--brand-teal) 66.6%); opacity: .9; }
.top { position: sticky; }

/* ================================================================ หน้า login / ลืมรหัส / MFA */
.auth-page { background:
  radial-gradient(1200px 500px at 100% -10%, color-mix(in srgb, var(--brand-sky) 14%, transparent), transparent 60%),
  radial-gradient(900px 500px at -10% 110%, color-mix(in srgb, var(--accent) 12%, transparent), transparent 60%), var(--bg); }
.auth-card { position: relative; overflow: hidden; }
.auth-card::before { content: ""; position: absolute; left: 0; right: 0; top: 0; height: 4px;
  background: linear-gradient(90deg, var(--brand-red) 0 33.3%, var(--brand-sky) 33.3% 66.6%, var(--brand-teal) 66.6%); }
.login-logo { display: flex; justify-content: center; margin: .2rem 0 1rem; }
.login-logo img { width: min(240px, 70%); height: auto; }
.login-logo.small img { width: min(150px, 50%); }
.auth-links { display: flex; justify-content: space-between; align-items: center; gap: 1rem; flex-wrap: wrap; margin: 1rem 0 0 !important; font-size: .9rem; }
.auth-links:has(> :only-child) { justify-content: center; }
.auth-list { padding-left: 1.2rem; margin: 0 0 1.2rem; } .auth-list li { margin-bottom: .5rem; }
.done-icon { width: 56px; height: 56px; border-radius: 50%; display: grid; place-items: center; margin: 0 auto .8rem; background: var(--green-soft); color: var(--green); font-size: 1.6rem; font-weight: 700; }
.mfa-icon { width: 56px; height: 56px; border-radius: 16px; display: grid; place-items: center; margin: 0 auto .8rem; background: var(--accent-soft); color: var(--accent); }
.center { text-align: center; }
.login .card h1 { margin-bottom: .1rem; }
@media (max-width: 639px) { .auth-card::before { display: none; } .login { align-items: start; padding-top: 1.5rem; } }

/* ช่องรหัสผ่าน + ปุ่มรูปตา */
.pw-wrap { position: relative; display: block; margin-top: .25rem; }
.pw-wrap input { margin-top: 0; padding-right: 3rem; }
.pw-eye { position: absolute; right: 2px; top: 50%; transform: translateY(-50%); width: var(--tap); height: var(--tap); display: grid; place-items: center;
  border: 0; background: none; color: var(--muted); cursor: pointer; border-radius: 8px; }
.pw-eye:hover { color: var(--ink); }
.pw-eye[aria-pressed="true"] { color: var(--accent); }
.pw-eye:focus-visible { outline: 2px solid var(--accent); }
input::-ms-reveal, input::-ms-clear { display: none; }  /* ปิดปุ่มตาของ Edge ไม่ให้ซ้อน 2 อัน */
.pw-meter { display: flex; align-items: center; gap: .6rem; margin-top: .35rem; font-size: .78rem; color: var(--muted); }
.pw-meter i { flex: 1; height: 5px; border-radius: 3px; background: var(--line); position: relative; overflow: hidden; }
.pw-meter i::after { content: ""; position: absolute; inset: 0; width: 0; transition: width .2s, background .2s; }
.pw-meter[data-lv="1"] i::after { width: 25%; background: var(--red); }
.pw-meter[data-lv="2"] i::after { width: 50%; background: var(--amber); }
.pw-meter[data-lv="3"] i::after { width: 75%; background: var(--brand-sky); }
.pw-meter[data-lv="4"] i::after { width: 100%; background: var(--green); }
.pw-meter[data-lv="0"] span, .pw-meter:not([data-lv]) span { display: none; }

.code-input { font-size: 1.5rem !important; letter-spacing: .35em; text-align: center; font-variant-numeric: tabular-nums; font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace; }
.code-input::placeholder { letter-spacing: .35em; color: color-mix(in srgb, var(--muted) 50%, transparent); }
.inline-form .code-input { font-size: 1.15rem !important; letter-spacing: .25em; max-width: 210px; margin: 0; }
.inline-form { display: flex; gap: .5rem; flex-wrap: wrap; align-items: center; margin: .6rem 0; }

/* ================================================================ หน้าบัญชี */
.account-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(340px, 1fr)); gap: 1rem; align-items: start; }
@media (max-width: 760px) { .account-grid { grid-template-columns: 1fr; } }
.account-grid .card.focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft), var(--shadow); }
#mfa { grid-row: span 2; }
@media (max-width: 760px) { #mfa { grid-row: auto; } }
.card h3 { display: flex; align-items: center; gap: .5rem; flex-wrap: wrap; }
.steps-note { background: var(--accent-soft); color: var(--accent); border-radius: 10px; padding: .6rem 1rem; margin-bottom: 1rem; font-weight: 500; }
.setup-steps { padding-left: 1.3rem; margin: .8rem 0 0; }
.setup-steps > li { margin-bottom: 1rem; padding-left: .2rem; }
.qr-wrap { display: flex; gap: 1rem; align-items: center; flex-wrap: wrap; margin-top: .6rem; }
.qr { width: 184px; height: 184px; background: #fff; border-radius: 12px; padding: 6px; border: 1px solid var(--line); flex: none; }
.qr svg { width: 100%; height: 100%; display: block; }
.qr-alt { display: flex; flex-direction: column; gap: .35rem; min-width: 0; flex: 1; align-items: flex-start; }
.secret { font-size: .95rem; letter-spacing: .05em; padding: .3rem .5rem; word-break: break-all; }
.recovery-box { border: 1px dashed var(--amber); background: var(--amber-soft); border-radius: 12px; padding: .9rem 1rem; margin-bottom: 1rem; }
.recovery-box p { margin: 0 0 .6rem; }
.codes { display: grid; grid-template-columns: repeat(2, 1fr); gap: .35rem 1.2rem; margin: 0 0 .8rem; padding-left: 1.6rem; }
.codes code { font-size: 1rem; background: var(--surface); letter-spacing: .04em; }
details.sub { border-top: 1px solid var(--line); padding: .3rem 0; }
details.sub > summary { cursor: pointer; min-height: var(--tap); display: flex; align-items: center; font-weight: 500; }
.danger-text { color: var(--red); }
.device-list { list-style: none; padding: 0; margin: 0 0 .6rem; }
.device-list li { display: flex; justify-content: space-between; align-items: center; gap: .8rem; padding: .5rem 0; border-bottom: 1px solid var(--line); }
.device-list li > div { display: flex; flex-direction: column; min-width: 0; }

/* คำขอลืมรหัสผ่าน (หน้า admin) */
.req-card { border-color: var(--amber); margin-bottom: 1rem; }
.req-list { list-style: none; padding: 0; margin: 0; }
.req-list li { display: flex; justify-content: space-between; align-items: center; gap: .6rem 1rem; flex-wrap: wrap; padding: .6rem 0; border-top: 1px solid var(--line); }
.req-list .ri { display: flex; flex-direction: column; min-width: 0; }
.nav-badge { display: inline-grid; place-items: center; min-width: 18px; height: 18px; padding: 0 5px; border-radius: 9px; background: var(--red); color: #fff; font-size: .7rem; font-weight: 700; margin-left: .3rem; line-height: 1; }
.tabbar .nav-badge { position: absolute; transform: translate(14px, -12px); margin: 0; }
.tabbar button { position: relative; }

@media print {
  .top, .tabbar, .foot, .fab, .page-head, .card:not(#mfa), #mfa > :not(.recovery-box), .recovery-box .row { display: none !important; }
  body { background: #fff; padding: 0 !important; } .recovery-box { border: 1px solid #000; background: #fff; }
}
label.check { flex-wrap: wrap; row-gap: 0; line-height: 1.4; }
label.check small { flex-basis: 100%; padding-left: calc(22px + .6rem); }
