/* ─── Reset & Base ──────────────────────────────────────────────────────────── */

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg:           #f0f2f7;
  --surface:      #ffffff;
  --border:       #d8dde8;
  --primary:      #1f2a44;
  --primary-h:    #162036;
  --primary-light:#e8eaf0;
  --danger:       #b91c1c;
  --danger-light: #fef2f2;
  --success:      #1e3a6e;
  --success-light:#eff6ff;
  --text:         #1a1a1a;
  --muted:        #6b7280;
  --radius:       10px;
  --shadow:       0 1px 4px rgba(0,0,0,.08);
  --shadow-md:    0 4px 16px rgba(0,0,0,.10);
}

html { font-size: 16px; -webkit-text-size-adjust: 100%; }

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ─── Header / Nav ──────────────────────────────────────────────────────────── */

.site-header {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 100;
}

.site-nav {
  max-width: 900px;
  margin: 0 auto;
  padding: 0 1rem;
  height: 56px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
}

.brand {
  display: flex;
  align-items: center;
  gap: .5rem;
  font-weight: 700;
  font-size: .95rem;
  color: var(--text);
  text-decoration: none;
  flex-shrink: 0;
}

.brand-name { color: var(--primary); font-weight: 700; font-size: 1.05rem; letter-spacing: 0.01em; }

.nav-links {
  display: flex;
  align-items: center;
  gap: .25rem;
  flex-wrap: wrap;
}

.nav-links a {
  padding: .35rem .65rem;
  border-radius: 6px;
  font-size: .875rem;
  color: var(--muted);
  text-decoration: none;
  transition: background .15s, color .15s;
  white-space: nowrap;
}

.nav-links a:hover,
.nav-links a.active {
  background: var(--primary-light);
  color: var(--primary);
  text-decoration: none;
}

.logout-form { display: inline; }

.btn-logout {
  padding: .35rem .65rem;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--muted);
  font-size: .875rem;
  cursor: pointer;
  transition: background .15s;
  white-space: nowrap;
}

.btn-logout:hover {
  background: var(--bg);
}

/* ─── Main content ──────────────────────────────────────────────────────────── */

.site-main {
  flex: 1;
  max-width: 900px;
  width: 100%;
  margin: 0 auto;
  padding: 1.5rem 1rem 3rem;
}

.site-footer {
  text-align: center;
  padding: 1.5rem 1rem;
  color: var(--muted);
  font-size: .8rem;
  border-top: 1px solid var(--border);
}

.footer-logo {
  height: 48px;
  width: auto;
  opacity: 0.7;
}

/* ─── Flash messages ────────────────────────────────────────────────────────── */

.flash {
  padding: .875rem 1rem;
  border-radius: var(--radius);
  margin-bottom: 1.25rem;
  font-size: .9rem;
  font-weight: 500;
}

.flash-success {
  background: var(--success-light);
  color: var(--success);
  border: 1px solid #bfdbfe;
}

.flash-error {
  background: var(--danger-light);
  color: var(--danger);
  border: 1px solid #fecaca;
}

/* ─── Auth pages ────────────────────────────────────────────────────────────── */

.auth-card {
  max-width: 420px;
  margin: 2rem auto 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 2rem 1.75rem;
  box-shadow: var(--shadow-md);
}

.auth-card h1 {
  font-size: 1.5rem;
  font-weight: 700;
  margin-bottom: 1.5rem;
}

.auth-link {
  margin-top: 1.25rem;
  font-size: .875rem;
  color: var(--muted);
  text-align: center;
}

/* ─── Forms ─────────────────────────────────────────────────────────────────── */

.auth-form,
.checkout-form {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.field {
  display: flex;
  flex-direction: column;
  gap: .375rem;
}

.field label {
  font-size: .875rem;
  font-weight: 600;
  color: var(--text);
}

.field input,
.field select,
.field textarea {
  padding: .75rem .875rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 1rem;
  font-family: inherit;
  color: var(--text);
  background: var(--surface);
  transition: border-color .15s, box-shadow .15s;
  width: 100%;
  -webkit-appearance: none;
  appearance: none;
}

.field select {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath d='M1 1l5 5 5-5' stroke='%236b7280' stroke-width='1.5' fill='none' stroke-linecap='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right .875rem center;
  padding-right: 2.5rem;
}

.field input:focus,
.field select:focus,
.field textarea:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(44,95,46,.15);
}

.field textarea { resize: vertical; min-height: 80px; }

.field-hint { font-size: .78rem; color: var(--muted); }
.field-error { color: var(--danger); font-size: .875rem; margin-bottom: .5rem; }

.field-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
}

@media (max-width: 540px) {
  .field-row { grid-template-columns: 1fr; }
}

/* ─── Buttons ───────────────────────────────────────────────────────────────── */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: .75rem 1.25rem;
  border-radius: 8px;
  font-size: 1rem;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  border: none;
  transition: background .15s, opacity .15s;
  text-decoration: none;
  min-height: 48px;
  white-space: nowrap;
}

.btn:disabled { opacity: .5; cursor: not-allowed; }

.btn-primary {
  background: var(--primary);
  color: #fff;
}
.btn-primary:hover:not(:disabled) { background: var(--primary-h); }

.btn-secondary {
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
}
.btn-secondary:hover:not(:disabled) { background: var(--border); }

.btn-danger {
  background: var(--danger-light);
  color: var(--danger);
  border: 1px solid #fecaca;
}
.btn-danger:hover:not(:disabled) { background: #fecaca; }

.btn-full { width: 100%; }

.btn-sm {
  padding: .375rem .75rem;
  font-size: .8rem;
  min-height: 34px;
}

/* ─── Page header ───────────────────────────────────────────────────────────── */

.page-header {
  display: flex;
  align-items: baseline;
  gap: .75rem;
  margin-bottom: 1.5rem;
}

.page-header h1 {
  font-size: 1.5rem;
  font-weight: 700;
}

.page-count {
  font-size: .875rem;
  color: var(--muted);
}

/* ─── Loan card (user home) ─────────────────────────────────────────────────── */

.loan-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 4px solid var(--primary);
  border-radius: var(--radius);
  padding: 1.5rem;
  margin-bottom: 1rem;
  box-shadow: var(--shadow);
}

.loan-card-label {
  font-size: .75rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--muted);
  margin-bottom: .5rem;
}

.loan-card-title {
  font-size: 1.2rem;
  font-weight: 700;
  margin-bottom: .25rem;
}

.loan-card-author {
  color: var(--muted);
  margin-bottom: .75rem;
}

.loan-card-date {
  font-size: .8rem;
  color: var(--muted);
  margin-bottom: 1.25rem;
}

.loan-card form { margin-top: .75rem; }

.muted-note {
  font-size: .85rem;
  color: var(--muted);
  margin-top: .5rem;
}

.checkout-form {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.5rem;
  box-shadow: var(--shadow);
  margin-bottom: 1rem;
}

/* ─── Stats grid (admin dashboard) ─────────────────────────────────────────── */

.stats-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 1rem;
  margin-bottom: 2rem;
}

@media (max-width: 480px) {
  .stats-grid { grid-template-columns: 1fr; }
}

.stat-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem 1rem;
  text-align: center;
  box-shadow: var(--shadow);
}

.stat-card-green {
  border-color: #93c5fd;
  background: var(--success-light);
}

.stat-value {
  font-size: 2.5rem;
  font-weight: 800;
  line-height: 1;
  color: var(--primary);
  margin-bottom: .375rem;
}

.stat-label {
  font-size: .8rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .04em;
  color: var(--muted);
}

.admin-shortcuts {
  display: flex;
  flex-direction: column;
  gap: .5rem;
}

.shortcut-link {
  font-size: .9rem;
  color: var(--primary);
  padding: .25rem 0;
}

/* ─── Section card ──────────────────────────────────────────────────────────── */

.section-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.5rem;
  margin-bottom: 1.5rem;
  box-shadow: var(--shadow);
}

.section-card h2 {
  font-size: 1rem;
  font-weight: 700;
  margin-bottom: 1rem;
}

.add-book-form {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

/* ─── Table ─────────────────────────────────────────────────────────────────── */

.table-wrap {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: .875rem;
}

.data-table th {
  padding: .75rem 1rem;
  text-align: left;
  font-size: .75rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .04em;
  color: var(--muted);
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

.data-table td {
  padding: .875rem 1rem;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}

.data-table tbody tr:last-child td { border-bottom: none; }

.data-table tbody tr:hover { background: #f4f6fb; }

.row-inactive { opacity: .55; }

.empty-row {
  text-align: center;
  color: var(--muted);
  padding: 2rem 1rem !important;
}

.inline-form { display: inline; }

/* ─── Badges ────────────────────────────────────────────────────────────────── */

.badge {
  display: inline-block;
  padding: .2rem .6rem;
  border-radius: 999px;
  font-size: .75rem;
  font-weight: 600;
  white-space: nowrap;
}

.badge-in       { background: #dbeafe; color: #1e3a6e; }
.badge-out      { background: #fef9c3; color: #854d0e; }
.badge-inactive { background: var(--bg); color: var(--muted); border: 1px solid var(--border); }

/* ─── Book cards (user home) ────────────────────────────────────────────────── */

.books-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 1.25rem;
}

.book-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.book-cover {
  width: 100%;
  aspect-ratio: 2 / 3;
  object-fit: cover;
  display: block;
  background: var(--bg);
}

.book-cover-placeholder {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 2.5rem;
  color: var(--muted);
  background: var(--bg);
}

.book-card-info {
  padding: .75rem .875rem .5rem;
  flex: 1;
}

.book-card-title {
  font-weight: 700;
  font-size: .9rem;
  line-height: 1.35;
  margin-bottom: .25rem;
}

.book-card-author {
  font-size: .8rem;
  color: var(--muted);
}

.book-card-copies {
  font-size: .75rem;
  color: var(--primary);
  margin-top: .25rem;
  font-weight: 600;
}

.book-card-form {
  padding: .625rem .875rem .875rem;
}

/* ─── Admin book lookup ──────────────────────────────────────────────────────── */

.lookup-row {
  display: flex;
  align-items: flex-end;
  gap: .75rem;
}

.lookup-btn-align {
  flex-shrink: 0;
  margin-bottom: 0;
}

.cover-input-row {
  display: flex;
  align-items: center;
  gap: .75rem;
}

.cover-input-row input { flex: 1; }

.cover-preview {
  width: 56px;
  height: 80px;
  object-fit: cover;
  border-radius: 4px;
  border: 1px solid var(--border);
  flex-shrink: 0;
}

.table-cover {
  width: 36px;
  height: 50px;
  object-fit: cover;
  border-radius: 3px;
  border: 1px solid var(--border);
  display: block;
}

.table-cover-placeholder {
  width: 36px;
  height: 50px;
}

/* ─── Invite link display ───────────────────────────────────────────────────── */

.invite-link {
  font-family: monospace;
  font-size: .75rem;
  color: var(--muted);
  word-break: break-all;
}

.invite-link-row {
  display: flex;
  align-items: flex-start;
  gap: .75rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: .75rem 1rem;
}

.invite-link-row .invite-link { flex: 1; }

/* ─── Utilities ─────────────────────────────────────────────────────────────── */

.muted { color: var(--muted); }

/* ─── Mobile tweaks ─────────────────────────────────────────────────────────── */

@media (max-width: 600px) {
  .site-main { padding: 1rem .875rem 2.5rem; }

  .auth-card { padding: 1.5rem 1.25rem; }

  .stats-grid { grid-template-columns: repeat(3, 1fr); gap: .75rem; }
  .stat-value { font-size: 1.875rem; }

  .data-table th,
  .data-table td { padding: .625rem .75rem; }

  .page-header h1 { font-size: 1.25rem; }
}
