:root {
  color-scheme: light;
  --bg: #f5f7fb;
  --surface: #ffffff;
  --surface-2: #eef3f8;
  --text: #111827;
  --muted: #1f2937;
  --line: #d9e2ec;
  --primary: #0f766e;
  --primary-dark: #115e59;
  --danger: #b42318;
  --ok: #137333;
  --shadow: 0 16px 40px rgba(15, 23, 42, 0.12);
  --font-sans: "TH SarabunPSK", "TH Sarabun", "TH Sarabun New", sans-serif;
}

html,
body,
body * {
  font-family: "TH SarabunPSK", "TH Sarabun", "TH Sarabun New", sans-serif;
}

form label {
  font-weight: 700;
}

form input,
form select,
form textarea {
  font-weight: 400;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-sans);
  font-size: 22px;
  line-height: 1.35;
  text-rendering: optimizeLegibility;
  -webkit-font-smoothing: antialiased;
}

button,
input,
select {
  font: inherit;
  font-size: 22px;
}

.system-chooser { min-height: 100vh; display: grid; place-items: center; padding: 32px; }
.chooser-panel { width: min(900px, 100%); background: var(--surface); border-radius: 18px; box-shadow: var(--shadow); padding: 40px; text-align: center; }
.system-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 24px; margin: 32px 0; }
.system-card { min-height: 220px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 10px; padding: 28px; border: 2px solid var(--line); border-radius: 16px; background: #fff; color: var(--text); text-decoration: none; transition: .15s ease; }
.system-card:hover { transform: translateY(-3px); border-color: var(--primary); box-shadow: 0 12px 28px rgba(15, 118, 110, .14); background: #fff; }
.system-card strong { font-size: 22px; font-weight: 700; }
.system-card span:last-child { color: var(--muted); font-size: 22px; font-weight: 600; }
.system-icon { font-size: 54px; }
.equipment-card { border-color: #c7d2fe; }
.chooser-footer { display: flex; justify-content: center; align-items: center; gap: 16px; }
.page-header-actions { display: flex; align-items: center; gap: 10px; }
@media (max-width: 700px) { .system-grid { grid-template-columns: 1fr; } .chooser-panel { padding: 24px; } }

input:focus,
select:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 2px rgba(15, 118, 110, 0.12);
  outline: none;
}

button {
  border: 0;
  border-radius: 6px;
  background: var(--primary);
  color: #fff;
  cursor: pointer;
  font-weight: 600;
  min-height: 42px;
  padding: 0 16px;
}

button:hover {
  background: var(--primary-dark);
}

button.secondary {
  background: var(--surface-2);
  color: var(--text);
}

button.secondary:hover {
  background: #dce7f1;
}

button.danger {
  background: var(--danger);
}

.row-actions {
  display: flex;
  gap: 8px;
}

.book-row-controls {
  align-items: flex-end;
  display: grid;
  gap: 8px;
  justify-items: end;
}

input,
select {
  border: 1px solid var(--line);
  border-radius: 6px;
  color: var(--text);
  min-height: 42px;
  padding: 0 12px;
  width: 100%;
}

label {
  color: var(--muted);
  display: grid;
  font-size: 22px;
  gap: 6px;
}

#exportBooksButton,
#exportLoanHistoryButton {
  flex-shrink: 0;
  white-space: nowrap;
}

.hidden {
  display: none !important;
}

.login-shell {
  align-items: center;
  display: flex;
  justify-content: center;
  min-height: 100vh;
  padding: 24px;
}

.login-panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  display: grid;
  gap: 28px;
  max-width: 420px;
  padding: 32px;
  width: 100%;
}

.login-panel h1,
.page-header h1,
.sidebar h2 {
  font-weight: 700;
  line-height: 1.25;
  margin: 0;
}

.stack {
  display: grid;
  gap: 16px;
}

.eyebrow {
  color: var(--primary);
  font-size: 22px;
  font-weight: 700;
  letter-spacing: 0;
  margin: 0 0 8px;
  text-transform: uppercase;
}

.muted,
.hint {
  color: var(--muted);
  margin: 6px 0 0;
}

.hint {
  font-size: 22px;
}

.message {
  color: var(--danger);
  font-size: 22px;
  min-height: 18px;
  margin: 0;
}

.app-shell {
  display: grid;
  grid-template-columns: 260px 1fr;
  min-height: 100vh;
}

.sidebar {
  align-self: start;
  background: #14213d;
  color: #fff;
  display: flex;
  flex-direction: column;
  gap: 28px;
  height: 100vh;
  overflow-y: auto;
  padding: 24px;
  position: sticky;
  top: 0;
}

.sidebar .eyebrow,
.sidebar .muted {
  color: #9fd8ce;
}

.sidebar nav {
  display: grid;
  gap: 8px;
}

.nav-button {
  background: transparent;
  color: #dbeafe;
  justify-content: flex-start;
  text-align: left;
  width: 100%;
}

.nav-button:hover,
.nav-button.active {
  background: rgba(255, 255, 255, 0.12);
  color: #fff;
}

.user-box {
  border-top: 1px solid rgba(255, 255, 255, 0.16);
  display: grid;
  gap: 12px;
  margin-top: auto;
  padding-top: 18px;
}

.content {
  padding: 28px;
  position: relative;
}

.tab {
  display: none;
}

.tab.active {
  display: grid;
  gap: 22px;
}

.page-header {
  align-items: end;
  display: flex;
  gap: 16px;
  justify-content: space-between;
}

.search {
  max-width: 360px;
}

.stats-grid {
  display: grid;
  gap: 16px;
  grid-template-columns: repeat(5, minmax(140px, 1fr));
}

.stat {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 18px;
}

.stat strong {
  display: block;
  font-size: 34px;
  line-height: 1;
  margin-bottom: 8px;
}

.stat span {
  color: var(--muted);
  font-size: 22px;
}

.dashboard-grid {
  display: grid;
  gap: 16px;
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.dashboard-panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  min-width: 0;
  padding: 18px;
}

.dashboard-alert-panel {
  border-top: 3px solid #d97706;
}

.panel-header {
  align-items: center;
  display: flex;
  gap: 16px;
  justify-content: space-between;
  margin-bottom: 14px;
}

.panel-header h2 {
  font-size: 22px;
  margin: 0;
}

.panel-header .muted {
  font-size: 22px;
  margin-top: 2px;
}

button.compact {
  min-height: 34px;
  padding: 0 12px;
  white-space: nowrap;
}

.dashboard-list {
  display: grid;
}

.dashboard-item {
  align-items: center;
  border-top: 1px solid var(--line);
  display: grid;
  gap: 12px;
  grid-template-columns: minmax(0, 1fr) auto;
  padding: 11px 0;
}

.dashboard-item:first-child {
  border-top: 0;
}

.dashboard-item strong,
.dashboard-item span {
  display: block;
}

.dashboard-item > div:first-child strong {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.dashboard-item span {
  color: var(--muted);
  font-size: 22px;
}

.dashboard-item-status {
  text-align: right;
}

.overdue-status strong {
  color: var(--danger);
}

.status-returned {
  color: var(--ok);
}

.status-borrowed {
  color: #9a5b00;
}

.empty-state {
  background: var(--surface-2);
  border-radius: 8px;
  color: var(--muted);
  display: grid;
  gap: 2px;
  padding: 18px;
  text-align: center;
}

.empty-state strong {
  color: var(--text);
}

.form-grid {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  display: grid;
  gap: 12px;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  padding: 16px;
}

.form-grid > label {
  align-content: start;
  grid-template-rows: auto 60px;
}

.form-grid > label > input,
.form-grid > label > select,
.form-grid > label > .stock-summary {
  height: 60px;
}

.form-grid button {
  align-self: end;
}

.autocomplete {
  position: relative;
}

.form-grid > label > .combobox-control {
  border: 1px solid var(--line);
  border-radius: 6px;
  height: 60px;
  position: relative;
}

.form-grid > label > .combobox-control:focus-within {
  border-color: var(--primary);
  box-shadow: 0 0 0 2px rgba(15, 118, 110, 0.12);
}

.combobox-control input {
  border: 0;
  border-radius: 6px;
  box-shadow: none;
  height: 100%;
  outline: none;
  padding-right: 46px;
  width: 100%;
}

.form-grid .combobox-toggle {
  align-self: stretch;
  align-items: center;
  background: transparent;
  border: 0;
  border-left: 0;
  border-radius: 0;
  bottom: auto;
  color: var(--muted);
  cursor: pointer;
  display: flex;
  font-size: 0;
  height: calc(100% - 2px);
  justify-content: center;
  min-height: 0;
  padding: 0;
  position: absolute;
  right: 1px;
  top: 1px;
  width: 40px;
}

.form-grid .combobox-toggle::before {
  border-bottom: 2px solid currentColor;
  border-right: 2px solid currentColor;
  content: "";
  height: 7px;
  transform: translateY(-2px) rotate(45deg);
  width: 7px;
}

.form-grid .combobox-toggle:hover,
.form-grid .combobox-toggle:focus-visible {
  background: rgba(15, 118, 110, 0.08);
  color: var(--primary);
  outline: none;
}

#borrowForm .borrow-book-field,
#borrowForm .borrow-member-field,
#borrowForm .borrow-stock-field {
  grid-column: span 2;
}

#borrowForm .borrow-submit {
  grid-column: 1 / -1;
  justify-self: end;
  min-width: 240px;
}

.suggestions {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 12px 28px rgba(15, 23, 42, 0.16);
  left: 0;
  max-height: 320px;
  overflow-y: auto;
  position: absolute;
  right: 0;
  top: calc(100% + 4px);
  z-index: 20;
}

.suggestion-item {
  background: transparent;
  border: 0;
  border-bottom: 1px solid var(--line);
  border-radius: 0;
  color: inherit;
  cursor: pointer;
  display: block;
  padding: 10px 12px;
  text-align: left;
  width: 100%;
}

.suggestion-item:last-child {
  border-bottom: 0;
}

.suggestion-item:hover,
.suggestion-item:focus {
  background: #eef6ff;
}

.suggestion-item strong,
.suggestion-item span {
  display: block;
}

.stock-summary {
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 8px;
  display: grid;
  gap: 3px;
  min-height: 60px;
  padding: 7px 12px;
}

.stock-summary strong {
  color: var(--primary-dark);
}

.stock-summary.out-of-stock strong {
  color: var(--danger);
}

.stock-counts {
  color: var(--muted);
  font-size: 22px;
}

.suggestion-item span {
  color: var(--muted);
  font-size: 22px;
  margin-top: 3px;
}

.list {
  display: grid;
  gap: 10px;
}

.row {
  align-items: center;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  display: grid;
  gap: 12px;
  grid-template-columns: 1fr auto;
  padding: 14px 16px;
}

.row-title {
  font-weight: 700;
  margin: 0 0 4px;
}

.row-meta {
  color: var(--muted);
  font-size: 22px;
  font-weight: 600;
  margin: 0;
}

.muted {
  font-weight: 600;
}

.badge {
  background: #e7f6f1;
  border-radius: 999px;
  color: var(--ok);
  display: inline-flex;
  font-size: 22px;
  font-weight: 700;
  padding: 5px 10px;
}

.badge.warn {
  background: #fff1d6;
  color: #9a5b00;
}

.history-status {
  align-items: flex-end;
  display: grid;
  gap: 6px;
  justify-items: end;
}

.toast {
  background: #dcfce7;
  border: 1px solid #86efac;
  border-radius: 6px;
  bottom: auto;
  box-shadow: var(--shadow);
  color: #166534;
  font-weight: 600;
  left: 50%;
  margin: 0;
  min-width: 260px;
  padding: 12px 18px;
  position: fixed;
  text-align: center;
  top: 20px;
  transform: translateX(-50%);
  z-index: 1000;
}

.toast.error {
  background: #fee2e2;
  border-color: #fca5a5;
  color: #991b1b;
}

@media (max-width: 960px) {
  .app-shell {
    grid-template-columns: 1fr;
  }

  .sidebar {
    height: auto;
    overflow: visible;
    position: static;
  }

  .sidebar nav {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .stats-grid,
  .form-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .dashboard-grid {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 620px) {
  .content,
  .sidebar {
    padding: 18px;
  }

  .page-header {
    align-items: stretch;
    flex-direction: column;
  }

  .stats-grid,
  .form-grid,
  .row {
    grid-template-columns: 1fr;
  }

  .panel-header,
  .dashboard-item {
    align-items: stretch;
    grid-template-columns: 1fr;
  }

  .panel-header {
    flex-direction: column;
  }

  .dashboard-item-status {
    text-align: left;
  }

  #borrowForm .borrow-book-field,
  #borrowForm .borrow-member-field,
  #borrowForm .borrow-stock-field,
  #borrowForm .borrow-submit {
    grid-column: 1;
  }

  #borrowForm .borrow-submit {
    justify-self: stretch;
    min-width: 0;
  }
}
