/* Reusable components: page header, cards, buttons, fields, chips, rows, empty states, dialogs, toasts. */

/* ---------- Page header ---------- */
.page-head {
  display: flex; align-items: center; justify-content: space-between; gap: var(--space-4);
  margin-bottom: var(--space-5);
  min-height: 36px;
}
.page-title {
  font-family: var(--font-serif);
  font-size: var(--fs-lg);
  font-weight: var(--fw-semi);
  letter-spacing: .005em;
}
.page-sub { font-size: var(--fs-sm); color: var(--text-muted); margin-top: 2px; }
.page-actions { display: flex; gap: var(--space-3); flex-wrap: wrap; }

/* ---------- Grid ---------- */
.grid { display: grid; gap: var(--gap); }
.grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
@media (min-width: 640px) { .grid-3, .grid-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
@media (min-width: 1100px) {
  .grid-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .grid-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
}
.span-2 { grid-column: span 2; }
@media (max-width: 639px) { .grid-3 > .span-2, .grid-4 > .span-2 { grid-column: auto; } }
.stack { display: flex; flex-direction: column; gap: var(--gap); }
.row { display: flex; align-items: center; gap: var(--space-3); }
.row-between { display: flex; align-items: center; justify-content: space-between; gap: var(--space-3); }
.wrap { flex-wrap: wrap; }

/* ---------- Card ---------- */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: var(--card-pad);
  box-shadow: var(--shadow);
  min-width: 0;
}
.card-head {
  display: flex; align-items: center; justify-content: space-between; gap: var(--space-3);
  margin-bottom: var(--space-3);
}
.card-title {
  display: flex; align-items: center; gap: var(--space-2);
  font-size: var(--fs-md);
  font-weight: var(--fw-semi);
}
.card-title svg { width: 16px; height: 16px; color: var(--gold-ink); flex: none; }
.card-link { font-size: var(--fs-sm); color: var(--accent); text-decoration: none; font-weight: var(--fw-medium); }
.card-link:hover { text-decoration: underline; }
.card-note { font-size: var(--fs-sm); color: var(--text-muted); }

/* ---------- Stat card ---------- */
.stat { display: flex; flex-direction: column; gap: 2px; }
.stat-label { font-size: var(--fs-xs); color: var(--text-muted); text-transform: uppercase; letter-spacing: .06em; font-weight: var(--fw-medium); }
.stat-value { font-family: var(--font-serif); font-size: var(--fs-xl); font-weight: var(--fw-semi); line-height: 1.1; }
.stat-foot { font-size: var(--fs-xs); color: var(--text-muted); }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: var(--space-2);
  height: var(--btn-h);
  padding: 0 var(--space-5);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  font-size: var(--fs-base);
  font-weight: var(--fw-medium);
  text-decoration: none;
  white-space: nowrap;
  transition: background var(--dur-fast) var(--ease), border-color var(--dur-fast) var(--ease), transform var(--dur-fast) var(--ease);
}
.btn svg { width: 16px; height: 16px; flex: none; }
.btn:hover { background: var(--surface-2); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn-primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn-ghost { background: transparent; border-color: transparent; }
.btn-ghost:hover { background: var(--surface-2); }
.btn-danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn-danger:hover { filter: brightness(1.06); background: var(--danger); }
[data-theme="academia"] .btn-danger, [data-theme="romantasy"] .btn-danger, [data-theme="midnight"] .btn-danger { color: #1a0f0f; }
.btn-sm { height: var(--btn-h-sm); padding: 0 var(--space-4); font-size: var(--fs-sm); }
.btn-block { width: 100%; }

.icon-btn {
  display: inline-grid; place-items: center;
  width: var(--tap); height: var(--tap);
  margin: -6px;
  border: 0; border-radius: var(--radius-pill);
  background: transparent;
  color: var(--text-muted);
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.icon-btn svg { width: 20px; height: 20px; }

/* ---------- Fields ---------- */
.field { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.field-label { font-size: var(--fs-sm); font-weight: var(--fw-medium); color: var(--text); }
.field-hint { font-size: var(--fs-xs); color: var(--text-muted); }
.input, .select, .textarea {
  width: 100%;
  min-height: var(--btn-h);
  padding: 0 var(--space-4);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  font-size: var(--fs-input);
}
.textarea { padding: var(--space-3) var(--space-4); min-height: 80px; resize: vertical; }
.select {
  appearance: none; -webkit-appearance: none;
  padding-right: 28px;
  background-image: linear-gradient(45deg, transparent 50%, currentColor 50%), linear-gradient(135deg, currentColor 50%, transparent 50%);
  background-position: calc(100% - 14px) 52%, calc(100% - 9px) 52%;
  background-size: 5px 5px, 5px 5px;
  background-repeat: no-repeat;
}
.input:focus-visible, .select:focus-visible, .textarea:focus-visible { outline-offset: 0; border-color: var(--focus); }

/* Setting row: label on the left, control on the right */
.set-row {
  display: flex; align-items: center; justify-content: space-between; gap: var(--space-5);
  min-height: var(--row-h);
  padding: var(--space-2) 0;
  border-top: 1px solid var(--border);
}
.set-row:first-of-type { border-top: 0; }
.set-row .set-text { min-width: 0; }
.set-row .set-label { font-weight: var(--fw-medium); }
.set-row .set-hint { font-size: var(--fs-xs); color: var(--text-muted); }
.set-row .select, .set-row .input { width: auto; min-width: 120px; max-width: 55%; }

/* Toggle switch (a styled checkbox) */
.switch { position: relative; display: inline-flex; flex: none; width: 40px; height: 24px; }
.switch input { position: absolute; inset: 0; opacity: 0; margin: 0; cursor: pointer; z-index: 1; }
.switch span {
  position: absolute; inset: 0;
  border-radius: var(--radius-pill);
  background: var(--border-strong);
  transition: background var(--dur) var(--ease);
}
.switch span::after {
  content: ""; position: absolute; left: 3px; top: 3px;
  width: 18px; height: 18px; border-radius: 50%;
  background: var(--surface);
  box-shadow: 0 1px 2px rgba(0,0,0,.2);
  transition: transform var(--dur) var(--ease);
}
.switch input:checked + span { background: var(--accent); }
.switch input:checked + span::after { transform: translateX(16px); }
.switch input:focus-visible + span { outline: 2px solid var(--focus); outline-offset: 2px; }

/* ---------- Chips & badges ---------- */
.chip {
  display: inline-flex; align-items: center; gap: 4px;
  height: 24px; padding: 0 var(--space-3);
  border-radius: var(--radius-pill);
  background: var(--accent-soft);
  color: var(--text);
  font-size: var(--fs-xs); font-weight: var(--fw-medium);
  white-space: nowrap;
}
.badge {
  display: inline-flex; align-items: center;
  height: 18px; padding: 0 6px;
  border-radius: var(--radius-pill);
  border: 1px solid var(--border-strong);
  font-size: 10px; font-weight: var(--fw-semi);
  letter-spacing: .04em; text-transform: uppercase;
  color: var(--text-muted);
}

/* ---------- List rows ---------- */
.list { display: flex; flex-direction: column; }
.list-row {
  display: flex; align-items: center; gap: var(--space-4);
  min-height: var(--row-h);
  padding: var(--space-2) var(--space-1);
  border-top: 1px solid var(--border);
  color: inherit; text-decoration: none;
}
.list-row:first-child { border-top: 0; }
a.list-row:hover { background: var(--surface-2); border-radius: var(--radius-sm); }
.list-row .grow { flex: 1; min-width: 0; }
.list-row .title { font-weight: var(--fw-medium); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.list-row .meta { font-size: var(--fs-xs); color: var(--text-muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.list-row > svg { width: 16px; height: 16px; color: var(--text-muted); flex: none; }

/* ---------- Progress ---------- */
.progress { height: 6px; border-radius: var(--radius-pill); background: var(--surface-2); overflow: hidden; }
.progress > span { display: block; height: 100%; background: var(--accent); border-radius: inherit; }

/* ---------- Empty state ---------- */
.empty {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: var(--space-2);
  padding: var(--space-7) var(--space-5);
  text-align: center;
  color: var(--text-muted);
}
.empty svg { width: 30px; height: 30px; color: var(--gold-ink); margin-bottom: var(--space-1); }
.empty-title { font-family: var(--font-serif); font-size: var(--fs-md); font-weight: var(--fw-semi); color: var(--text); }
.empty-text { font-size: var(--fs-sm); max-width: 34ch; }
.empty .btn { margin-top: var(--space-3); }
.empty-sm { padding: var(--space-5) var(--space-3); }

/* ---------- Toast ---------- */
.toast-region {
  position: fixed; left: 50%; z-index: 90;
  bottom: calc(var(--bottom-nav-h) + env(safe-area-inset-bottom) + 12px);
  transform: translateX(-50%);
  display: flex; flex-direction: column; align-items: center; gap: 6px;
  width: max-content; max-width: calc(100vw - 32px);
  pointer-events: none;
}
@media (min-width: 900px) { .toast-region { bottom: 24px; left: calc(50% + var(--sidebar-w) / 2); } }
.toast {
  padding: 9px 14px;
  border-radius: var(--radius);
  background: var(--text);
  color: var(--bg);
  font-size: var(--fs-sm); font-weight: var(--fw-medium);
  box-shadow: var(--shadow-lg);
  animation: toast-in var(--dur) var(--ease);
  pointer-events: auto;
}
.toast.warn { background: var(--danger); color: #fff; }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } }

/* ---------- Dialog (modal on laptop, bottom sheet on phone) ---------- */
.dialog {
  width: min(460px, calc(100vw - 32px));
  max-height: min(86vh, 720px);
  padding: 0;
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow-lg);
  overflow: hidden;
}
.dialog::backdrop { background: var(--overlay); }
.dialog[open] { display: flex; flex-direction: column; animation: dialog-in var(--dur) var(--ease); }
@keyframes dialog-in { from { opacity: 0; transform: translateY(10px) scale(.98); } }
.dialog-head {
  display: flex; align-items: center; justify-content: space-between; gap: var(--space-3);
  padding: var(--space-5) var(--space-6) var(--space-3);
}
.dialog-title { font-family: var(--font-serif); font-size: 17px; font-weight: var(--fw-semi); }
.dialog-body { padding: 0 var(--space-6) var(--space-5); overflow-y: auto; font-size: var(--fs-base); }
.dialog-body p + p { margin-top: var(--space-3); }
.dialog-foot {
  display: flex; justify-content: flex-end; gap: var(--space-3);
  padding: var(--space-4) var(--space-6) var(--space-5);
  border-top: 1px solid var(--border);
  background: var(--surface);
}
@media (max-width: 599px) {
  .dialog.sheet {
    width: 100vw; max-width: 100vw;
    margin: auto 0 0;
    border-radius: var(--radius-lg) var(--radius-lg) 0 0;
    border-bottom: 0;
    padding-bottom: env(safe-area-inset-bottom);
  }
  .dialog.sheet[open] { animation: sheet-in var(--dur) var(--ease); }
  .dialog-foot .btn { flex: 1; }
}
@keyframes sheet-in { from { transform: translateY(40px); opacity: .4; } }

/* ---------- Theme swatches ---------- */
.swatches { display: grid; grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); gap: var(--space-3); }
.swatch {
  display: flex; flex-direction: column; gap: 6px;
  padding: 6px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  text-align: left;
  font-size: var(--fs-sm); font-weight: var(--fw-medium);
}
.swatch[aria-pressed="true"] { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }
.swatch-preview {
  position: relative;
  height: 46px; border-radius: 6px; overflow: hidden;
  display: flex; align-items: flex-end; gap: 3px;
  padding: 6px 8px;
}
.swatch-preview i { display: block; width: 9px; border-radius: 2px 2px 0 0; }
.swatch-preview i:nth-child(1) { height: 26px; }
.swatch-preview i:nth-child(2) { height: 32px; }
.swatch-preview i:nth-child(3) { height: 22px; }
.swatch-check { margin-left: auto; width: 14px; height: 14px; color: var(--accent); }

/* ---------- Phase 2 additions ---------- */
.dialog-wide { width: min(600px, calc(100vw - 32px)); }
@media (max-width: 599px) { .dialog.sheet { max-height: 92vh; max-height: 92dvh; } }

/* Option chips (radio / checkbox styled as chips) */
.opts { display: flex; flex-wrap: wrap; gap: 6px; }
.opt { position: relative; display: inline-flex; }
.opt input { position: absolute; opacity: 0; inset: 0; margin: 0; cursor: pointer; }
.opt span {
  display: inline-flex; align-items: center;
  min-height: 32px; padding: 0 12px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-pill);
  background: var(--surface);
  font-size: var(--fs-sm); font-weight: var(--fw-medium);
  user-select: none;
  transition: background var(--dur-fast) var(--ease), border-color var(--dur-fast) var(--ease);
}
.opt input:checked + span { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.opt input:focus-visible + span { outline: 2px solid var(--focus); outline-offset: 2px; }
.opt-more {
  min-height: 32px; padding: 0 10px;
  border: 1px dashed var(--border-strong); border-radius: var(--radius-pill);
  background: transparent; color: var(--text-muted);
  font-size: var(--fs-sm);
}
.more-genres { display: contents; }

.field-error { font-size: var(--fs-xs); color: var(--danger); font-weight: var(--fw-medium); }
.link-btn {
  border: 0; background: none; padding: 4px 2px;
  color: var(--accent); font-size: var(--fs-sm); font-weight: var(--fw-medium);
  text-decoration: underline; text-underline-offset: 2px;
}
.select-sm { min-height: var(--btn-h-sm); font-size: var(--fs-sm); width: auto; }
@media (max-width: 899px) { .select-sm { font-size: var(--fs-input); } }
.spinner {
  display: inline-block; width: 13px; height: 13px; margin-right: 6px; vertical-align: -2px;
  border: 2px solid var(--border-strong); border-top-color: var(--accent); border-radius: 50%;
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .spinner { animation-duration: 2.4s; } }
@media (max-width: 479px) { .hide-xs { display: none; } }

.chip-x { border: 0; cursor: pointer; padding-right: 6px; }
.chip-x svg { width: 12px; height: 12px; }

/* Segmented control */
.seg { display: inline-flex; border: 1px solid var(--border-strong); border-radius: var(--radius-sm); overflow: hidden; flex: none; }
.seg button {
  display: grid; place-items: center;
  width: 34px; height: var(--btn-h-sm);
  border: 0; background: var(--surface); color: var(--text-muted);
}
.seg button + button { border-left: 1px solid var(--border-strong); }
.seg button[aria-pressed="true"] { background: var(--accent-soft); color: var(--text); }
.seg svg { width: 16px; height: 16px; }
