:root {
  --bg: #f7f5f0;
  --surface: #ffffff;
  --text: #2a2622;
  --muted: #6f675e;
  --border: #e2ddd3;
  --accent: #7a3e1d;
  --accent-text: #ffffff;
  --accent-soft: #f4e9e1;
  --ok: #2f6b3a;
  --ok-soft: #e4f1e6;
  --warn: #8a5a00;
  --warn-soft: #fbf0d9;
  --bad: #9b2c2c;
  --bad-soft: #fbe5e5;
  --radius: 8px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 15px;
  line-height: 1.5;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1c1a18;
    --surface: #262320;
    --text: #ece7e0;
    --muted: #a59d93;
    --border: #3a3631;
    --accent: #e0915f;
    --accent-text: #1c1a18;
    --accent-soft: #3a2a20;
    --ok: #8fd19e;
    --ok-soft: #1f3324;
    --warn: #e9c46a;
    --warn-soft: #3a3020;
    --bad: #f1948a;
    --bad-soft: #3d2222;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--text); }
a { color: var(--accent); }
h1 { font-size: 1.6rem; margin: 0 0 .25rem; }
h2 { font-size: 1.15rem; margin: 1.5rem 0 .5rem; }
.muted { color: var(--muted); }
.right { text-align: right; }
.nowrap { white-space: nowrap; }

/* Layout */
.topbar { background: var(--surface); border-bottom: 1px solid var(--border); }
.topbar .inner { display: flex; align-items: center; gap: 1.25rem; flex-wrap: wrap;
  max-width: 1200px; margin: 0 auto; padding: .6rem 1rem; }
.brand { font-weight: 700; color: var(--text); text-decoration: none; font-size: 1.1rem; }
.nav { display: flex; gap: .25rem; flex-wrap: wrap; flex: 1; }
.nav a { text-decoration: none; color: var(--muted); padding: .35rem .6rem; border-radius: 6px; }
.nav a:hover, .nav a.active { color: var(--text); background: var(--accent-soft); }
.nav-group { position: relative; }
.nav-toggle { font: inherit; color: var(--muted); background: none; border: 0; cursor: pointer;
  padding: .35rem .6rem; border-radius: 6px; }
.nav-toggle:hover, .nav-toggle.active, .nav-group:focus-within .nav-toggle {
  color: var(--text); background: var(--accent-soft); }
.nav-menu { display: none; position: absolute; top: 100%; left: 0; z-index: 10; min-width: 10rem;
  padding: .25rem; background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: 0 4px 12px rgba(0, 0, 0, .12); }
.nav-group:hover .nav-menu, .nav-group:focus-within .nav-menu {
  display: flex; flex-direction: column; gap: .15rem; }
.user { display: flex; gap: .5rem; align-items: center; color: var(--muted); }
.user form { margin: 0; }
main { max-width: 1200px; margin: 0 auto; padding: 1.25rem 1rem 3rem; }
.page-head { display: flex; justify-content: space-between; align-items: flex-start;
  gap: 1rem; flex-wrap: wrap; margin-bottom: 1rem; }
.actions { display: flex; gap: .5rem; flex-wrap: wrap; align-items: center; }

.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 1rem 1.25rem; margin-bottom: 1rem; }
.card > h2:first-child { margin-top: 0; }
.grid { display: grid; gap: 1rem; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); }
.grid-2 { display: grid; gap: 1rem; grid-template-columns: 2fr 1fr; }
@media (max-width: 860px) { .grid-2 { grid-template-columns: 1fr; } }

.stats { display: grid; gap: 1rem; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); margin-bottom: 1rem; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: .9rem 1rem; }
.stat .label { color: var(--muted); font-size: .85rem; }
.stat .value { font-size: 1.5rem; font-weight: 650; font-variant-numeric: tabular-nums; }
.stat .sub { color: var(--muted); font-size: .85rem; }

/* Tables */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
th, td { text-align: left; padding: .5rem .6rem; border-bottom: 1px solid var(--border); vertical-align: top; }
th { color: var(--muted); font-weight: 600; font-size: .85rem; }
td.num, th.num { text-align: right; }
tr.voided td { color: var(--muted); text-decoration: line-through; }
tfoot td { font-weight: 650; border-bottom: none; }

dl.facts { display: grid; grid-template-columns: max-content 1fr; gap: .3rem 1rem; margin: 0; }
dl.facts dt { color: var(--muted); }
dl.facts dd { margin: 0; }

/* Badges */
.badge { display: inline-block; font-size: .78rem; padding: .05rem .5rem; border-radius: 999px;
  background: var(--accent-soft); color: var(--accent); white-space: nowrap; }
.badge.ok { background: var(--ok-soft); color: var(--ok); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.bad { background: var(--bad-soft); color: var(--bad); }

/* Forms */
input, select, textarea, button { font: inherit; color: inherit; }
input[type=text], input[type=email], input[type=number], input[type=password], input[type=date],
input[type=search], select, textarea {
  width: 100%; padding: .45rem .55rem; border: 1px solid var(--border); border-radius: 6px;
  background: var(--bg);
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
label { display: block; font-weight: 550; margin-bottom: .2rem; }
.field { margin-bottom: .85rem; }
.field.check label { display: flex; gap: .5rem; align-items: center; font-weight: 500; }
.helptext, .field .help { color: var(--muted); font-size: .82rem; }
.errorlist { list-style: none; margin: .2rem 0; padding: 0; color: var(--bad); font-size: .85rem; }
.form-errors { background: var(--bad-soft); color: var(--bad); padding: .6rem .9rem; border-radius: 6px; margin-bottom: 1rem; }
.form-errors ul { margin: 0; padding-left: 1rem; }

.btn { display: inline-block; padding: .45rem .9rem; border-radius: 6px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); text-decoration: none; cursor: pointer; white-space: nowrap; }
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.danger { color: var(--bad); border-color: var(--bad); }
.btn.small { padding: .2rem .55rem; font-size: .85rem; }
.btn.link { border: none; background: none; color: var(--accent); padding: 0; }
.btn:disabled, .btn:disabled:hover { opacity: .45; cursor: not-allowed; border-color: var(--border); }
.btn.primary:disabled { border-color: var(--accent); }
.complete-hint { font-size: .85rem; margin: .5rem 0 0; }

.filters { display: flex; gap: .5rem; flex-wrap: wrap; align-items: end; margin-bottom: 1rem; }
.filters > * { flex: 1 1 150px; }
.filters .q { flex: 3 1 240px; }
.filters .btns { flex: 0 0 auto; display: flex; gap: .4rem; }

/* Line-item tables on sale/intake screens */
.lines td { padding: .35rem .3rem; }
.lines input, .lines select { padding: .35rem .4rem; }
.lines .qty { width: 5rem; }
.lines .money-input { min-width: 9rem; }
.lines .isbn-col { min-width: 11rem; }
.line-info { font-size: .82rem; color: var(--muted); margin-top: .15rem; min-height: 1.1em; }
.line-info.found { color: var(--ok); }
.line-info.missing { color: var(--warn); }
.newbook td { background: var(--accent-soft); }
.lines tbody.line[data-state="bad"] .isbn-input { border-color: var(--warn); background: var(--warn-soft); }

/* "Not in inventory" popup on the sale screen */
.dialog { width: min(36rem, calc(100vw - 2rem)); max-height: calc(100vh - 4rem); padding: 1.25rem;
  border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); color: var(--text);
  box-shadow: 0 10px 30px rgba(0,0,0,.25); }
.dialog::backdrop { background: rgba(0,0,0,.4); }
.dialog h2 { margin-top: 0; }
.dialog-msg { margin-top: 0; }
.dialog-search-row { display: flex; gap: .5rem; }
.dialog-status { font-size: .85rem; margin: .6rem 0 .3rem; min-height: 1.3em; }
.dialog-results { list-style: none; margin: 0; padding: 0; max-height: 18rem; overflow-y: auto; }
.dialog-result { display: flex; flex-direction: column; align-items: flex-start; gap: .1rem; width: 100%;
  text-align: left; padding: .5rem .6rem; margin-bottom: .35rem; border: 1px solid var(--border);
  border-radius: 6px; background: var(--bg); cursor: pointer; }
.dialog-result:hover, .dialog-result:focus-visible { border-color: var(--accent); outline: none; }
.dialog-result .muted { font-size: .82rem; }
.dialog-actions { margin-top: 1rem; justify-content: flex-end; }
.newbook .fields { display: grid; gap: .5rem; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); }
.newbook label { font-size: .8rem; }

.summary { display: grid; grid-template-columns: 1fr auto; gap: .25rem 1rem; font-variant-numeric: tabular-nums; }
.summary .total { font-weight: 700; font-size: 1.15rem; }

/* Tabs (Settings page) */
.tabs { display: flex; gap: .25rem; flex-wrap: wrap; border-bottom: 1px solid var(--border); margin-bottom: 1rem; }
.tabs a { text-decoration: none; color: var(--muted); padding: .5rem .9rem; border: 1px solid transparent;
  border-bottom: none; border-radius: 6px 6px 0 0; margin-bottom: -1px; display: inline-flex; gap: .4rem; align-items: center; }
.tabs a:hover { color: var(--text); }
.tabs a.active { color: var(--text); background: var(--surface); border-color: var(--border); font-weight: 600; }
.tabs .dot { width: .5rem; height: .5rem; border-radius: 50%; background: var(--border); }
.tabs .dot.on { background: var(--ok); }

/* Messages */
.messages { list-style: none; padding: 0; margin: 0 0 1rem; }
.messages li { padding: .6rem .9rem; border-radius: 6px; margin-bottom: .5rem; background: var(--accent-soft); }
.messages li.success { background: var(--ok-soft); color: var(--ok); }
.messages li.error { background: var(--bad-soft); color: var(--bad); }
.messages li.warning { background: var(--warn-soft); color: var(--warn); }

.pagination { display: flex; gap: .5rem; align-items: center; justify-content: center; margin-top: 1rem; }

.login { max-width: 360px; margin: 4rem auto; }

/* Audit trail */
.audit-ip { font-size: .78rem; }
.audit-history { font-size: .8rem; margin-left: .4rem; color: var(--muted); }
.audit-object { background: var(--accent-soft); padding: .5rem .8rem; border-radius: 6px; }
.audit-changes { margin-top: .3rem; font-size: .85rem; }
.audit-changes summary { cursor: pointer; color: var(--muted); }
.audit-changes table { margin-top: .25rem; }
.audit-changes td, .audit-changes th { padding: .15rem .5rem .15rem 0; font-size: .85rem; }
.audit-old { color: var(--muted); text-decoration: line-through; }
@media (max-width: 700px) {
  /* Stack each event: when / who / what on one line, the details below. */
  .audit, .audit > tbody, .audit > tbody > tr, .audit > tbody > tr > td { display: block; }
  .audit > tbody > tr:first-child { display: none; }
  .audit > tbody > tr { padding: .6rem 0; border-bottom: 1px solid var(--border); }
  .audit > tbody > tr > td { border: none; padding: .1rem 0; }
  .audit > tbody > tr > td:nth-child(-n+3) { display: inline-block; margin-right: .5rem; }
  .audit .audit-ip { display: inline; margin-left: .3rem; }
}

@media print {
  .topbar, .no-print, .messages { display: none !important; }
  body { background: #fff; color: #000; }
  .card { border: none; padding: 0; }
}

/* Backup & restore */
.danger-zone { border-color: var(--bad); }
.danger-zone h2 { color: var(--bad); }
.user a.user-name { color: var(--muted); text-decoration: none; }
.user a.user-name:hover { color: var(--text); text-decoration: underline; }
tr.inactive td { color: var(--muted); }
