/* components.css — the shared pieces. Every screen builds from these; a screen's
   own stylesheet only lays them out.

   Buttons take their look from the logo: a rounded shape with the ink outline,
   and for the one primary action, a hard ink shadow underneath that the press
   animation squashes, so it feels like a real key going down.               */

/* ------------------------------------------------------------------ buttons */
.btn {
  --btn-bg: var(--surface-2);
  --btn-fg: var(--text);
  --btn-bd: var(--line-2);
  display: inline-flex; align-items: center; justify-content: center; gap: var(--s2);
  min-height: var(--tap);
  padding: 0 var(--s4);
  border: var(--stroke) solid var(--btn-bd);
  border-radius: var(--r-pill);
  background: var(--btn-bg);
  color: var(--btn-fg);
  font-family: var(--font-ui);
  font-size: var(--fs-md);
  font-weight: 800;
  line-height: 1;
  letter-spacing: .01em;
  text-decoration: none;
  white-space: nowrap;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
  transition: background-color var(--t-fast) var(--ease), border-color var(--t-fast) var(--ease), color var(--t-fast) var(--ease);
  will-change: transform;
}
.btn:hover { --btn-bd: var(--text-2); }
.btn:disabled, .btn[aria-disabled="true"] { opacity: .45; cursor: not-allowed; }
.btn .icon { width: 1.2em; height: 1.2em; }

.btn-primary {
  --btn-bg: var(--accent);
  --btn-fg: var(--on-accent);
  --btn-bd: var(--outline);
  box-shadow: 0 var(--press-depth) 0 var(--outline);
  margin-bottom: var(--press-depth);
}
.btn-primary:hover { --btn-bg: var(--accent-hi); --btn-bd: var(--outline); }
.btn-brand { --btn-bg: var(--brand); --btn-fg: var(--on-brand); --btn-bd: var(--outline); }
.btn-brand:hover { --btn-bg: var(--brand-deep); --btn-bd: var(--outline); color: #fff; }
.btn-ghost { --btn-bg: transparent; --btn-bd: transparent; }
.btn-ghost:hover { --btn-bg: var(--bg-sunk); --btn-bd: transparent; }
.btn-danger { --btn-bg: var(--danger-soft); --btn-fg: var(--danger); --btn-bd: color-mix(in srgb, var(--danger) 45%, transparent); }
.btn-danger:hover { --btn-bg: var(--danger); --btn-fg: #fff; --btn-bd: var(--danger); }
.btn-confirm { --btn-bg: var(--danger); --btn-fg: #fff; --btn-bd: var(--outline); }
.btn-sm { min-height: 34px; padding: 0 var(--s3); font-size: var(--fs-sm); }
.btn-lg { min-height: 54px; padding: 0 var(--s5); font-size: var(--fs-lg); }
.btn-block { display: flex; width: 100%; }
.btn-icon { width: var(--tap); padding: 0; flex: none; }
.btn-icon.btn-sm { width: 34px; }
.btn-icon.btn-lg { width: 60px; min-height: 60px; }
.btn-round { border-radius: 50%; }
.btn-group { display: inline-flex; gap: var(--s2); flex-wrap: wrap; }

/* Text-and-icon rows used in menus and lists that act like buttons. */
.linkish { background: none; border: 0; padding: 0; color: var(--brand-deep); font-weight: 800; text-decoration: underline; text-underline-offset: 3px; }

/* ------------------------------------------------------------------ form fields */
.field { display: flex; flex-direction: column; gap: 6px; }
.field > label, .label { font-weight: 800; font-size: var(--fs-sm); color: var(--text-2); }
.hint { font-size: var(--fs-sm); color: var(--text-3); }
.error-text { font-size: var(--fs-sm); color: var(--danger); font-weight: 700; }
.input, .select, .textarea {
  width: 100%;
  min-height: var(--tap);
  padding: 10px var(--s4);
  border: var(--stroke) solid var(--line-2);
  border-radius: var(--r-md);
  background: var(--raise);
  color: var(--text);
  font-weight: 600;
  transition: border-color var(--t-fast) var(--ease), box-shadow var(--t-fast) var(--ease);
}
.textarea { min-height: 110px; resize: vertical; line-height: 1.5; }
.input::placeholder, .textarea::placeholder { color: var(--text-3); font-weight: 500; }
.input:focus, .select:focus, .textarea:focus { border-color: var(--brand-deep); box-shadow: 0 0 0 4px var(--brand-soft); outline: none; }
.input[aria-invalid="true"] { border-color: var(--danger); }
.input-wrap { position: relative; display: flex; align-items: center; }
.input-wrap > .icon { position: absolute; left: 14px; color: var(--text-3); pointer-events: none; }
.input-wrap > .icon + .input { padding-left: 42px; }
.input-wrap > .input:has(+ .input-end) { padding-right: 48px; }
.input-wrap > .input-end { position: absolute; right: 4px; }

/* On/off switch (a real checkbox underneath). */
.switch { position: relative; display: inline-flex; align-items: center; gap: var(--s3); cursor: pointer; min-height: var(--tap); font-weight: 700; }
.switch input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.switch-track {
  position: relative; flex: none;
  width: 50px; height: 30px;
  border-radius: var(--r-pill);
  background: var(--bg-sunk);
  border: var(--stroke) solid var(--line-2);
  transition: background-color var(--t-base) var(--ease), border-color var(--t-base) var(--ease);
}
.switch-thumb {
  position: absolute; top: 2px; left: 2px;
  width: 22px; height: 22px; border-radius: 50%;
  background: var(--raise);
  border: var(--stroke) solid var(--outline);
  transition: transform var(--t-base) var(--ease);
}
.switch input:checked + .switch-track { background: var(--accent); border-color: var(--outline); }
.switch input:checked + .switch-track .switch-thumb { transform: translateX(20px); }
.switch input:focus-visible + .switch-track { outline: 3px solid var(--focus); outline-offset: 3px; }

/* Segmented choice: a row of radio buttons that reads as one control. */
.segmented { display: inline-flex; padding: 3px; gap: 2px; border-radius: var(--r-pill); background: var(--bg-sunk); border: 1px solid var(--line); position: relative; }
.segmented button {
  position: relative; z-index: 1;
  min-height: 36px; padding: 0 var(--s4);
  border: 0; border-radius: var(--r-pill); background: transparent;
  color: var(--text-2); font-weight: 800; font-size: var(--fs-sm);
  display: inline-flex; align-items: center; gap: 6px;
}
.segmented button[aria-pressed="true"], .segmented button[aria-checked="true"] { color: var(--text); }
.segmented .seg-thumb { position: absolute; z-index: 0; top: 3px; bottom: 3px; border-radius: var(--r-pill); background: var(--raise); box-shadow: var(--shadow-1); border: 1px solid var(--line-2); }

/* Range slider with brand colours. */
.range { -webkit-appearance: none; appearance: none; width: 100%; height: 28px; background: transparent; }
.range::-webkit-slider-runnable-track { height: 6px; border-radius: 6px; background: linear-gradient(to right, var(--accent) var(--fill, 50%), var(--progress-track) var(--fill, 50%)); }
.range::-moz-range-track { height: 6px; border-radius: 6px; background: var(--progress-track); }
.range::-moz-range-progress { height: 6px; border-radius: 6px; background: var(--accent); }
.range::-webkit-slider-thumb { -webkit-appearance: none; width: 22px; height: 22px; margin-top: -8px; border-radius: 50%; background: var(--raise); border: var(--stroke) solid var(--outline); box-shadow: var(--shadow-1); }
.range::-moz-range-thumb { width: 20px; height: 20px; border-radius: 50%; background: var(--raise); border: var(--stroke) solid var(--outline); }

/* ------------------------------------------------------------------ chips, badges */
.chip {
  display: inline-flex; align-items: center; gap: 6px;
  min-height: 36px; padding: 0 var(--s3);
  border-radius: var(--r-pill);
  border: 1.5px solid var(--line-2);
  background: var(--surface-2);
  color: var(--text-2);
  font-size: var(--fs-sm); font-weight: 800;
  white-space: nowrap;
}
.chip[aria-pressed="true"], .chip.is-on { background: var(--text); color: var(--bg); border-color: var(--text); }
.chip .count { font-variant-numeric: tabular-nums; opacity: .7; }
.badge {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 2px 8px; border-radius: var(--r-pill);
  font-size: 11px; font-weight: 800; letter-spacing: .02em;
  background: var(--bg-sunk); color: var(--text-2);
}
.badge-accent { background: var(--accent-soft); color: var(--accent-deep); }
.badge-brand { background: var(--brand-soft); color: var(--brand-deep); }
.badge-ok { background: var(--ok-soft); color: var(--ok); }
.badge-danger { background: var(--danger-soft); color: var(--danger); }
.badge-sun { background: var(--sun-soft); color: var(--sb-bark); }
:root[data-theme="dark"] .badge-sun { color: var(--sun); }

/* ------------------------------------------------------------------ cards, lists */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--r-lg); padding: var(--s5); }
.card-flat { background: var(--surface); border-radius: var(--r-lg); padding: var(--s5); }
.section { margin-bottom: var(--s6); }
.section-head { display: flex; align-items: baseline; gap: var(--s3); margin-bottom: var(--s4); }
.section-head h2 { margin-right: auto; }
.list { display: flex; flex-direction: column; border: 1px solid var(--line); border-radius: var(--r-lg); background: var(--surface); overflow: hidden; }
.list-row { display: flex; align-items: center; gap: var(--s3); min-height: 60px; padding: var(--s3) var(--s4); border-bottom: 1px solid var(--line); }
.list-row:last-child { border-bottom: 0; }
.list-row .grow { flex: 1; min-width: 0; }
.list-row .title { font-weight: 800; }
.list-row .sub { font-size: var(--fs-sm); color: var(--text-3); }

/* ------------------------------------------------------------------ avatars */
.avatar {
  --size: 36px;
  width: var(--size); height: var(--size); flex: none;
  display: inline-grid; place-items: center;
  border-radius: 50%;
  background: var(--c, var(--accent));
  color: var(--sb-ink);
  border: var(--stroke) solid var(--outline);
  font-family: var(--font-brand); font-weight: 600; font-size: calc(var(--size) * .42);
  text-transform: uppercase;
}
.avatar-lg { --size: 64px; }
.avatar-sm { --size: 28px; }

/* ------------------------------------------------------------------ covers */
/* Every cover is 2:3 so a shelf of mixed books lines up. A book without a
   cover gets a generated one in the brand colours with its title on it. */
.cover {
  position: relative;
  aspect-ratio: 2 / 3;
  width: 100%;
  border-radius: var(--cover-r);
  overflow: hidden;
  background: var(--cover-bg, var(--bark-soft));
  box-shadow: var(--shadow-cover);
  isolation: isolate;
}
.cover img { width: 100%; height: 100%; object-fit: cover; }
.cover::after { /* the spine: a faint fold down the left edge, like a real book */
  content: ''; position: absolute; inset: 0 auto 0 0; width: 7%;
  background: linear-gradient(90deg, rgba(0,0,0,.16), rgba(255,255,255,.08) 55%, transparent);
  pointer-events: none; z-index: 2;
}
.cover-gen {
  position: absolute; inset: 0;
  display: flex; flex-direction: column; justify-content: space-between;
  padding: 12% 10% 10% 14%;
  color: var(--gen-fg, var(--sb-ink));
  background: var(--gen-bg, var(--sb-teal));
  font-family: var(--font-brand);
}
.cover-gen .gt { font-weight: 600; font-size: clamp(12px, 1.1em, 22px); line-height: 1.12; overflow-wrap: anywhere; }
.cover-gen .ga { font-family: var(--font-ui); font-weight: 800; font-size: .7em; opacity: .85; }
.cover-gen .gb { position: absolute; right: 8%; bottom: 7%; width: 22%; opacity: .9; }
.cover-progress { position: absolute; left: 0; right: 0; bottom: 0; height: 5px; background: rgba(26,22,16,.35); z-index: 3; }
.cover-progress > i { display: block; height: 100%; background: var(--sun); }

/* ------------------------------------------------------------------ progress */
.bar { height: 6px; border-radius: 6px; background: var(--progress-track); overflow: hidden; }
.bar > i { display: block; height: 100%; width: 0; border-radius: inherit; background: var(--accent); }
.ring { --p: 0; width: 40px; height: 40px; border-radius: 50%; background: conic-gradient(var(--accent) calc(var(--p) * 1turn), var(--progress-track) 0); display: grid; place-items: center; }
.ring::before { content: ''; width: 76%; height: 76%; border-radius: 50%; background: var(--surface); grid-area: 1 / 1; }
.ring > span { grid-area: 1 / 1; font-size: 11px; font-weight: 800; z-index: 1; }
.spinner { width: 22px; height: 22px; border-radius: 50%; border: 3px solid var(--progress-track); border-top-color: var(--accent); animation: sb-spin .8s linear infinite; }
@keyframes sb-spin { to { transform: rotate(360deg); } }
.skeleton { background: linear-gradient(90deg, var(--bg-sunk), var(--surface-2), var(--bg-sunk)); background-size: 200% 100%; animation: sb-shimmer 1.4s ease-in-out infinite; border-radius: var(--r-sm); }
@keyframes sb-shimmer { from { background-position: 100% 0; } to { background-position: -100% 0; } }

/* ------------------------------------------------------------------ sheets (dialogs) */
.scrim { position: fixed; inset: 0; background: var(--scrim); z-index: var(--z-sheet); }
.sheet {
  position: fixed; z-index: calc(var(--z-sheet) + 1);
  display: flex; flex-direction: column;
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow-3);
  max-height: calc(100dvh - var(--s6));
  outline: none;
}
.sheet-center { left: 50%; top: 50%; translate: -50% -50%; width: min(560px, calc(100vw - 32px)); border-radius: var(--r-xl); border: var(--stroke) solid var(--outline); }
.sheet-center.sheet-lg { width: min(880px, calc(100vw - 32px)); }
.sheet-right { top: 0; right: 0; bottom: 0; width: min(460px, 100vw); max-height: none; border-left: 1px solid var(--line); padding-top: var(--safe-top); }
.sheet-bottom { left: 0; right: 0; bottom: 0; border-radius: var(--r-xl) var(--r-xl) 0 0; max-height: 88dvh; padding-bottom: var(--safe-bottom); border-top: var(--stroke) solid var(--outline); }
.sheet-bottom::before { content: ''; display: block; width: 44px; height: 5px; margin: 10px auto 0; border-radius: 5px; background: var(--line-2); }
.sheet-full { inset: 0; max-height: none; border-radius: 0; padding: var(--safe-top) 0 var(--safe-bottom); }
.sheet-head { display: flex; align-items: center; gap: var(--s3); padding: var(--s4) var(--s5); border-bottom: 1px solid var(--line); }
.sheet-head h2 { font-size: var(--fs-lg); margin-right: auto; }
.sheet-body { padding: var(--s5); overflow: auto; overscroll-behavior: contain; flex: 1; }
.sheet-foot { display: flex; gap: var(--s2); justify-content: flex-end; flex-wrap: wrap; padding: var(--s4) var(--s5); border-top: 1px solid var(--line); }
@media (max-width: 760px) {
  .sheet-center { top: auto; bottom: 0; left: 0; translate: none; width: 100vw; border-radius: var(--r-xl) var(--r-xl) 0 0; border-width: var(--stroke) 0 0; padding-bottom: var(--safe-bottom); }
}

/* ------------------------------------------------------------------ menus */
.menu {
  position: fixed; z-index: var(--z-top);
  min-width: 220px; max-width: min(320px, calc(100vw - 16px));
  padding: 6px;
  background: var(--raise);
  border: 1.5px solid var(--line-2);
  border-radius: var(--r-md);
  box-shadow: var(--shadow-2);
}
.menu button, .menu a {
  display: flex; align-items: center; gap: var(--s3); width: 100%;
  min-height: 42px; padding: 0 var(--s3);
  border: 0; border-radius: var(--r-sm); background: none;
  color: var(--text); text-align: left; text-decoration: none; font-weight: 700;
}
.menu button:hover, .menu a:hover, .menu button:focus-visible { background: var(--bg); outline: none; }
.menu .menu-danger { color: var(--danger); }
.menu hr { margin: 6px 0; }
.menu [aria-checked="true"]::after { content: '✓'; margin-left: auto; font-weight: 900; color: var(--accent-deep); }

/* ------------------------------------------------------------------ toasts */
.toasts {
  position: fixed; z-index: var(--z-toast);
  left: 50%; translate: -50% 0;
  bottom: calc(var(--s5) + var(--safe-bottom) + var(--player-h, 0px) + var(--tabbar-h, 0px));
  display: flex; flex-direction: column; align-items: center; gap: var(--s2);
  pointer-events: none;
  width: min(560px, calc(100vw - 24px));
}
.toast {
  pointer-events: auto;
  display: flex; align-items: center; gap: var(--s3);
  padding: 10px 10px 10px var(--s4);
  min-height: 52px;
  border-radius: var(--r-lg);
  background: var(--sb-ink);
  color: var(--sb-paper);
  box-shadow: var(--shadow-2);
  font-weight: 700;
  max-width: 100%;
}
:root[data-theme="dark"] .toast { background: var(--sb-paper); color: var(--sb-ink); }
.toast .icon { color: var(--sun); }
.toast-error .icon { color: #F08C74; }
.toast-ok .icon { color: #8FD39A; }
.toast .btn { --btn-bg: transparent; --btn-fg: currentColor; --btn-bd: color-mix(in srgb, currentColor 40%, transparent); min-height: 36px; }
.toast-msg { flex: 1; }

/* ------------------------------------------------------------------ empty / notices */
.empty { display: flex; flex-direction: column; align-items: center; text-align: center; gap: var(--s3); padding: var(--s7) var(--s4); color: var(--text-2); }
.empty .bear { width: 96px; }
.empty h2 { color: var(--text); }
.notice { display: flex; gap: var(--s3); align-items: flex-start; padding: var(--s3) var(--s4); border-radius: var(--r-md); background: var(--brand-soft); color: var(--text); font-weight: 600; }
.notice-warn { background: var(--sun-soft); }
.notice-error { background: var(--danger-soft); }

/* Two-click confirmation: the button becomes a statement plus Confirm/Cancel. */
.confirm2 { display: inline-flex; align-items: center; gap: var(--s2); flex-wrap: wrap; padding: 6px 6px 6px var(--s3); border-radius: var(--r-lg); background: var(--danger-soft); }
.confirm2-text { font-weight: 800; color: var(--danger); font-size: var(--fs-sm); }

/* Keyboard hints. */
.kbd { display: inline-block; min-width: 1.6em; padding: 1px 6px; border-radius: 6px; border: 1.5px solid var(--line-2); border-bottom-width: 3px; font-size: var(--fs-xs); font-weight: 800; text-align: center; background: var(--surface-2); }

/* The wordmark, set like the logo: "Sunbear" over "Books", both in cocoa,
   rounded and plain, no outline. */
.wordmark { font-family: var(--font-brand); font-weight: 600; line-height: .92; letter-spacing: -.005em; display: inline-flex; flex-direction: column; align-items: center; color: var(--sb-ink); }
.wordmark .w1 { font-size: 1em; }
.wordmark .w2 { font-size: .82em; }
.wordmark-inline { flex-direction: row; gap: .28em; align-items: baseline; }
:root[data-theme="dark"] .wordmark { color: var(--text); }
