/* =========================================================================
   02._.librarian — หลังบ้าน
   ใช้ tokens เดียวกันกับ 02._.library (css/tokens.css) ต่างกันแค่โทนกลางคืน
   + โครง layout ของหลังบ้าน
   ========================================================================= */

[data-theme='night'] {
  --bg: var(--paper);
  --panel: var(--surface);
  --panel-2: var(--surface-2);
  --line-soft: rgba(242, 238, 228, .07);
  --gold: #d4af37;
  --gold-soft-2: #2a2413;
}

body { background: var(--paper); min-height: 100vh; }
body::before {
  content: ''; position: fixed; inset: 0; z-index: 0; pointer-events: none;
  background:
    radial-gradient(60% 40% at 80% -10%, rgba(212, 175, 55, .10), transparent 70%),
    radial-gradient(50% 40% at 0% 0%, rgba(242, 238, 228, .05), transparent 70%);
}

/* ---------------------------------------------------------------- boot */
.boot { display: grid; place-items: center; gap: 14px; height: 100vh; color: var(--muted); font-size: var(--fs-xs); }

/* ---------------------------------------------------------------- login */
.login-shell { position: relative; display: grid; place-items: center; min-height: 100vh; padding: 24px; }
.login-card {
  position: relative; width: min(100%, 412px); background: color-mix(in srgb, var(--surface) 88%, transparent);
  border: 1px solid var(--line-2); border-radius: var(--r-xl); padding: 34px;
  box-shadow: var(--shadow-4); backdrop-filter: blur(18px);
}
.login-card::before {
  content: ''; position: absolute; inset: -1px; border-radius: inherit; padding: 1px; z-index: -1;
  background: linear-gradient(160deg, rgba(212, 175, 55, .55), transparent 40%, transparent 70%, rgba(212, 175, 55, .2));
  -webkit-mask: linear-gradient(#000 0 0) content-box, linear-gradient(#000 0 0);
  -webkit-mask-composite: xor; mask-composite: exclude;
}
.login-brand { display: flex; align-items: center; gap: 11px; margin-bottom: 24px; }
.login-brand img { width: 40px; height: 40px; border-radius: 12px; }
.login-brand b { font-family: var(--display); font-weight: 600; font-size: var(--fs-md); display: block; line-height: 1.2; }
.login-brand span { font-size: var(--fs-2xs); color: var(--muted); }
.login-card h1 { font-family: var(--display); font-weight: 600; font-size: 24px; margin-bottom: 6px; letter-spacing: -.02em; }
.login-card p.sub { color: var(--muted); font-size: var(--fs-xs); margin-bottom: 24px; }
.field { display: grid; gap: 7px; }
.field > label { font-size: var(--fs-xs); font-weight: 600; color: var(--ink-2); }
.input, .select, .textarea {
  width: 100%; min-height: 46px; padding: 11px 14px; border-radius: var(--r-sm);
  border: 1px solid var(--line-2); background: var(--surface-2); color: var(--ink);
  transition: border-color var(--dur-fast), box-shadow var(--dur-fast);
}
.textarea { min-height: 98px; resize: vertical; }
.input:focus, .select:focus, .textarea:focus { outline: none; border-color: var(--gold); box-shadow: 0 0 0 3px rgba(212, 175, 55, .18); }
.input::placeholder, .textarea::placeholder { color: var(--muted-2); }
.select { appearance: none; background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%); background-position: calc(100% - 18px) 20px, calc(100% - 13px) 20px; background-size: 5px 5px; background-repeat: no-repeat; }
.error-box { padding: 11px 13px; border-radius: var(--r-sm); background: var(--danger-soft); color: var(--danger); font-size: var(--fs-xs); }

/* ---------------------------------------------------------------- buttons */
.btn {
  --bg: var(--gold); --fg: var(--gold-ink); --bd: transparent;
  position: relative; overflow: hidden; isolation: isolate;
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 42px; padding: 9px 17px; border-radius: var(--r-pill); background: var(--bg); color: var(--fg);
  border: 1px solid var(--bd); font-size: var(--fs-xs); font-weight: 600; line-height: 1;
  transition: transform var(--dur-fast) var(--ease-out), filter var(--dur-fast), opacity var(--dur-fast), background var(--dur-fast), border-color var(--dur-fast);
  white-space: nowrap;
}
.btn::before { content: ''; position: absolute; inset: 0; z-index: -1; opacity: 0; background: linear-gradient(110deg, transparent 25%, rgba(255, 255, 255, .3) 48%, transparent 70%); transform: translateX(-120%); transition: opacity var(--dur-fast), transform .7s var(--ease-out); }
.btn:hover { transform: translateY(-1px); }
.btn:hover::before { opacity: 1; transform: translateX(120%); }
.btn:disabled, .btn.is-disabled { opacity: .45; pointer-events: none; }
.btn--ghost { --bg: transparent; --fg: var(--ink-2); --bd: var(--line-2); }
.btn--ghost:hover { --bg: var(--surface-2); }
.btn--soft { --bg: var(--surface-2); --fg: var(--ink); --bd: var(--line); }
.btn--danger { --bg: transparent; --fg: var(--danger); --bd: color-mix(in srgb, var(--danger) 40%, transparent); }
.btn--sm { min-height: 34px; padding: 6px 12px; font-size: var(--fs-2xs); }
.btn--lg { min-height: 50px; padding: 13px 22px; font-size: var(--fs-sm); }
.btn--block { width: 100%; }
.btn--icon { min-height: 38px; width: 38px; padding: 0; border-radius: var(--r-sm); }
.btn__spinner { width: 14px; height: 14px; border: 2px solid currentColor; border-right-color: transparent; border-radius: 50%; animation: spin .7s linear infinite; }

/* ---------------------------------------------------------------- shell */
.shell { position: relative; z-index: 1; display: grid; grid-template-columns: 250px 1fr; min-height: 100vh; }
.side {
  position: sticky; top: 0; height: 100vh; display: flex; flex-direction: column; gap: 6px;
  padding: 20px 14px; background: color-mix(in srgb, var(--paper-2) 78%, transparent);
  border-right: 1px solid var(--line); backdrop-filter: blur(10px);
}
.side-brand { display: flex; align-items: center; gap: 10px; padding: 6px 8px 20px; }
.side-brand img { width: 34px; height: 34px; border-radius: 10px; }
.side-brand b { font-family: var(--display); font-weight: 600; font-size: var(--fs-sm); display: block; line-height: 1.2; }
.side-brand span { font-size: var(--fs-2xs); color: var(--muted); }
.side-nav { display: grid; gap: 3px; }
.side-nav a {
  position: relative; display: flex; align-items: center; gap: 10px; padding: 10px 12px;
  border-radius: var(--r-sm); color: var(--muted); font-size: var(--fs-xs); font-weight: 500;
  transition: all var(--dur-fast) var(--ease-out);
}
.side-nav a::before {
  content: ''; position: absolute; left: 0; top: 22%; bottom: 22%; width: 2px; border-radius: 2px;
  background: var(--gold); transform: scaleY(0); transition: transform var(--dur) var(--ease-out);
}
.side-nav a:hover { background: var(--surface-2); color: var(--ink); }
.side-nav a.is-active { background: var(--gold-soft); color: var(--gold); }
.side-nav a.is-active::before { transform: scaleY(1); }
.side-nav .count { margin-left: auto; background: var(--danger); color: #fff; border-radius: var(--r-pill); font-size: 10.5px; padding: 1px 7px; font-weight: 700; }
.side-foot { margin-top: auto; border-top: 1px solid var(--line); padding-top: 12px; display: grid; gap: 8px; }
.side-user { display: flex; align-items: center; gap: 9px; padding: 6px 8px; font-size: var(--fs-2xs); color: var(--muted); overflow: hidden; }
.side-user b { color: var(--ink); font-size: var(--fs-2xs); display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.main { min-width: 0; }
.topbar {
  position: sticky; top: 0; z-index: 30; display: flex; align-items: center; gap: 14px;
  padding: 16px 28px; border-bottom: 1px solid var(--line);
  background: color-mix(in srgb, var(--paper) 82%, transparent); backdrop-filter: blur(14px);
}
.topbar h1 { font-family: var(--display); font-weight: 600; font-size: 19px; letter-spacing: -.01em; }
.topbar .sub { font-size: var(--fs-2xs); color: var(--muted); }
.content { padding: 24px 28px 70px; }

/* ---------------------------------------------------------------- cards / stats */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--r-lg); padding: 20px; }
.card-head { display: flex; align-items: start; gap: 14px; margin-bottom: 16px; }
.card-title { font-family: var(--display); font-weight: 600; font-size: var(--fs-md); }
.card-sub { font-size: var(--fs-2xs); color: var(--muted); margin-top: 2px; }
.stat-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(186px, 1fr)); gap: 14px; }
.stat {
  position: relative; overflow: hidden; background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--r-lg); padding: 18px 20px; transition: transform var(--dur) var(--ease-out), border-color var(--dur);
}
.stat:hover { transform: translateY(-3px); border-color: color-mix(in srgb, var(--gold) 40%, var(--line)); }
.stat span { font-size: var(--fs-2xs); color: var(--muted); }
.stat b { display: block; font-family: var(--display); font-weight: 600; font-size: 28px; line-height: 1.24; margin-top: 4px; }
.stat small { font-size: var(--fs-2xs); color: var(--muted-2); }

/* ---------------------------------------------------------------- table */
.table-wrap { border: 1px solid var(--line); border-radius: var(--r-lg); overflow: hidden; background: var(--surface); }
table { width: 100%; border-collapse: collapse; font-size: var(--fs-xs); }
thead th { text-align: left; font-size: 11px; text-transform: uppercase; letter-spacing: .1em; color: var(--muted); font-weight: 600; padding: 12px 14px; background: var(--surface-2); border-bottom: 1px solid var(--line); white-space: nowrap; }
tbody td { padding: 13px 14px; border-bottom: 1px solid var(--line); vertical-align: middle; }
tbody tr:last-child td { border-bottom: 0; }
tbody tr { transition: background var(--dur-fast); }
tbody tr:hover { background: color-mix(in srgb, var(--ink) 3%, transparent); }
.td-actions { display: flex; gap: 6px; justify-content: flex-end; }
.thumb { width: 38px; height: 52px; border-radius: 5px; object-fit: cover; background: var(--surface-2); }
.book-cell { display: flex; align-items: center; gap: 12px; min-width: 0; }
.book-cell b { font-weight: 600; font-size: var(--fs-xs); }
.book-cell span { display: block; font-size: var(--fs-2xs); color: var(--muted); }

/* ---------------------------------------------------------------- badges / pills */
.badge { display: inline-flex; align-items: center; gap: 5px; padding: 3px 9px; border-radius: 6px; font-size: var(--fs-2xs); font-weight: 600; background: var(--surface-2); color: var(--muted); border: 1px solid var(--line); white-space: nowrap; }
.badge--ok { background: var(--ok-soft); color: var(--ok); border-color: transparent; }
.badge--warn { background: var(--warn-soft); color: var(--warn); border-color: transparent; }
.badge--danger { background: var(--danger-soft); color: var(--danger); border-color: transparent; }
.badge--gold { background: var(--gold-soft); color: var(--gold); border-color: transparent; }
.badge--blue { background: color-mix(in srgb, #6aa9ff 14%, transparent); color: #6aa9ff; border-color: transparent; }

/* ---------------------------------------------------------------- toolbar */
.toolbar { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-bottom: 16px; }
.chip {
  display: inline-flex; align-items: center; gap: 6px; padding: 7px 13px; border-radius: var(--r-pill);
  border: 1px solid var(--line-2); background: var(--surface); font-size: var(--fs-2xs); color: var(--ink-2);
  font-weight: 500; cursor: pointer; transition: all var(--dur-fast) var(--ease-out);
}
.chip:hover { border-color: var(--ink); }
.chip.is-active { background: var(--gold-soft); border-color: color-mix(in srgb, var(--gold) 45%, transparent); color: var(--gold); }
.search-box { position: relative; margin-left: auto; min-width: 240px; }
.search-box .icon { position: absolute; left: 13px; top: 50%; transform: translateY(-50%); color: var(--muted-2); }
.search-box .input { padding-left: 38px; border-radius: var(--r-pill); }

/* ---------------------------------------------------------------- forms grid */
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.grid-3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 14px; }
.stack { display: grid; gap: 12px; }
.row { display: flex; align-items: center; gap: 10px; }
.row--wrap { flex-wrap: wrap; }
.spacer { flex: 1; }
.divider { height: 1px; background: var(--line); border: 0; margin: 18px 0; }
.help { font-size: var(--fs-2xs); color: var(--muted-2); }

/* ---------------------------------------------------------------- modal */
#modal-root { position: relative; z-index: 200; }
.modal-overlay { position: fixed; inset: 0; display: grid; place-items: center; padding: 20px; background: rgba(4, 6, 10, .68); backdrop-filter: blur(4px); opacity: 0; transition: opacity .22s var(--ease-out); overflow-y: auto; }
.modal-overlay.is-open { opacity: 1; }
.modal { width: min(100%, 620px); background: var(--surface); border: 1px solid var(--line-2); border-radius: var(--r-xl); padding: 24px; box-shadow: var(--shadow-4); max-height: calc(100vh - 40px); overflow-y: auto; transform: translateY(14px); transition: transform .26s var(--ease-out); }
.modal-overlay.is-open .modal { transform: none; }
.modal--sm { max-width: 420px; }
.modal--lg { max-width: 830px; }
.modal-head { display: flex; align-items: start; gap: 14px; margin-bottom: 18px; }
.modal-title { font-family: var(--display); font-weight: 600; font-size: var(--fs-lg); }
.modal-close { margin-left: auto; width: 32px; height: 32px; display: grid; place-items: center; border-radius: var(--r-sm); color: var(--muted); }
.modal-close:hover { background: var(--surface-2); color: var(--ink); }
.modal-foot { display: flex; justify-content: flex-end; gap: 9px; margin-top: 22px; flex-wrap: wrap; }

/* ---------------------------------------------------------------- toast */
#toast-stack { position: fixed; right: 20px; bottom: 20px; z-index: 300; display: grid; gap: 10px; width: min(340px, calc(100vw - 32px)); }
.toast { display: flex; align-items: flex-start; gap: 10px; padding: 12px 14px; border-radius: var(--r-md); background: var(--surface-2); border: 1px solid var(--line-2); color: var(--ink); font-size: var(--fs-xs); box-shadow: var(--shadow-3); opacity: 0; transform: translateY(10px); transition: all .26s var(--ease-out); }
.toast.is-visible { opacity: 1; transform: none; }
.toast--error { border-color: color-mix(in srgb, var(--danger) 45%, transparent); color: #ffb6b0; }
.toast--success { border-color: color-mix(in srgb, var(--ok) 40%, transparent); }
.toast__close { margin-left: auto; color: var(--muted); }

/* ---------------------------------------------------------------- misc */
.empty { display: grid; place-items: center; gap: 8px; padding: 52px 20px; text-align: center; color: var(--muted); border: 1px dashed var(--line-2); border-radius: var(--r-lg); }
.empty h3 { font-family: var(--display); font-weight: 600; color: var(--ink); font-size: var(--fs-md); }
.qr-frame { width: 232px; height: 232px; padding: 12px; background: #fff; border-radius: var(--r-md); display: grid; place-items: center; margin-inline: auto; }
.qr-frame img { width: 208px; height: 208px; }
.key-code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: var(--fs-xs); letter-spacing: .04em; background: var(--surface-2); border: 1px solid var(--line); border-radius: 6px; padding: 3px 8px; }
.dropzone { border: 1.5px dashed var(--line-2); border-radius: var(--r-md); padding: 22px; text-align: center; color: var(--muted); font-size: var(--fs-xs); transition: all var(--dur-fast); cursor: pointer; }
.dropzone:hover, .dropzone.is-over { border-color: var(--gold); background: var(--gold-soft); color: var(--gold); }
.avatar { display: inline-grid; place-items: center; border-radius: var(--r-pill); background: var(--surface-2); color: var(--ink-2); font-size: var(--fs-2xs); font-weight: 600; width: 32px; height: 32px; border: 1px solid var(--line); }
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin-bottom: 18px; }
.tabs button { padding: 10px 14px; font-size: var(--fs-xs); font-weight: 600; color: var(--muted); border-bottom: 2px solid transparent; margin-bottom: -1px; }
.tabs button.is-active { color: var(--gold); border-color: var(--gold); }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: var(--fs-xs); }
.progress { height: 6px; border-radius: 3px; background: var(--surface-2); overflow: hidden; }
.progress i { display: block; height: 100%; background: var(--gold); }
.center { text-align: center; margin-inline: auto; }

/* ---------------------------------------------------------------- responsive */
@media (max-width: 900px) {
  .shell { grid-template-columns: 1fr; }
  .side { position: static; height: auto; flex-direction: row; flex-wrap: wrap; align-items: center; padding: 12px; }
  .side-brand { padding: 0 10px 0 4px; }
  .side-nav { display: flex; overflow-x: auto; gap: 4px; }
  .side-nav a { white-space: nowrap; }
  .side-nav a::before { display: none; }
  .side-foot { margin: 0 0 0 auto; border: 0; padding: 0; flex-direction: row; }
  .grid-2, .grid-3 { grid-template-columns: 1fr; }
  .content, .topbar { padding-inline: 16px; }
}

/* ---------------------------------------------------------------- entrance */
@keyframes fade-up { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: none; } }
.fade-up { animation: fade-up .5s var(--ease-out) both; }
.stat { animation: fade-up .5s var(--ease-out) both; }
.stat:nth-child(2) { animation-delay: .05s; }
.stat:nth-child(3) { animation-delay: .1s; }
.stat:nth-child(4) { animation-delay: .15s; }
tbody tr { animation: fade-up .35s var(--ease-out) both; }
tbody tr:nth-child(2) { animation-delay: .02s; } tbody tr:nth-child(3) { animation-delay: .04s; }
tbody tr:nth-child(4) { animation-delay: .06s; } tbody tr:nth-child(5) { animation-delay: .08s; }
