/* ==========================================================================
   Components
   ========================================================================== */

/* ---- Buttons ---- */
.btn {
  appearance: none;
  font-family: var(--font-ui); font-size: 13.5px; font-weight: 600;
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  padding: 9px 16px; border-radius: 999px;
  border: 1px solid var(--parchment-edge);
  background: var(--parchment-raised); color: var(--ink);
  transition: transform var(--dur) var(--ease), box-shadow var(--dur) var(--ease),
              background var(--dur) var(--ease), border-color var(--dur) var(--ease),
              color var(--dur) var(--ease), opacity var(--dur) var(--ease);
  box-shadow: var(--shadow-sm);
  white-space: nowrap;
}
.btn:hover { transform: translateY(-1px); box-shadow: var(--shadow); }
.btn:active { transform: translateY(0); }
.btn:disabled { opacity: .5; cursor: not-allowed; transform: none; box-shadow: none; }

.btn--primary {
  background: linear-gradient(180deg, var(--gold-bright), var(--gold));
  border-color: var(--gold-deep); color: #2a2008;
  box-shadow: 0 6px 18px var(--gold-glow);
}
.btn--primary:hover { box-shadow: 0 10px 26px var(--gold-glow); }

.btn--night {
  background: var(--night); border-color: var(--night-line); color: var(--night-text);
}
.btn--night:hover { background: var(--night-raised); }

.btn--ghost { background: transparent; border-color: transparent; box-shadow: none; }
.btn--ghost:hover { background: var(--parchment-sunk); box-shadow: none; }

.btn--danger { color: var(--danger); border-color: rgba(168,64,47,.35); background: var(--danger-soft); box-shadow: none; }
.btn--danger:hover { background: rgba(168,64,47,.2); }

.btn--sm { padding: 6px 11px; font-size: 12.5px; }
.btn--icon { padding: 8px; width: 36px; height: 36px; border-radius: 50%; }
.btn--block { width: 100%; }

/* Small icon button used inside cards/entries */
.iconbtn {
  appearance: none; border: 1px solid transparent; background: transparent;
  color: var(--ink-soft); width: 30px; height: 30px; border-radius: 8px;
  display: inline-flex; align-items: center; justify-content: center;
  transition: background var(--dur), color var(--dur), border-color var(--dur);
}
.iconbtn:hover { background: var(--parchment-sunk); color: var(--ink); }
.iconbtn.is-danger:hover { background: var(--danger-soft); color: var(--danger); }

.icon { width: 1em; height: 1em; display: inline-block; stroke: currentColor; fill: none; }

/* ---- Chips / pills ---- */
.chip {
  display: inline-flex; align-items: center; gap: 6px;
  font-family: var(--font-ui); font-size: 11px; font-weight: 600;
  letter-spacing: .04em; padding: 3px 9px; border-radius: 999px;
  background: var(--parchment-sunk); color: var(--ink-soft);
  border: 1px solid var(--parchment-edge);
}
.chip--gold { background: var(--gold-glow); border-color: var(--gold); color: var(--gold-deep); }
.chip--user { background: var(--user-tint); border-color: rgba(176,101,58,.4); color: var(--user-accent); }
.chip--ai { background: var(--ai-tint); border-color: rgba(74,99,148,.4); color: var(--ai-accent); }

/* ---- Forms ---- */
.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; }
.field > label {
  font-family: var(--font-ui); font-size: 11.5px; font-weight: 600;
  letter-spacing: .08em; text-transform: uppercase; color: var(--ink-faint);
}
.field .hint { font-family: var(--font-ui); font-size: 11.5px; color: var(--ink-faint); }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }

.input, .textarea, .select {
  width: 100%;
  background: var(--parchment-raised);
  border: 1px solid var(--parchment-edge);
  border-radius: var(--radius-sm);
  padding: 9px 12px;
  color: var(--ink);
  transition: border-color var(--dur), box-shadow var(--dur), background var(--dur);
}
.input:focus, .textarea:focus, .select:focus {
  outline: none; border-color: var(--gold);
  box-shadow: 0 0 0 3px var(--gold-glow);
  background: #fffaf0;
}
.textarea { resize: vertical; min-height: 84px; line-height: 1.5; }
.select {
  appearance: none;
  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='%238b7a60' stroke-width='1.6' fill='none' stroke-linecap='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 12px center;
  padding-right: 32px;
}

/* ---- Story rail items ---- */
.story-item {
  position: relative;
  display: flex; align-items: center; gap: 11px;
  width: 100%; text-align: left;
  padding: 11px 12px; margin-bottom: 4px;
  border-radius: 10px;
  border: 1px solid transparent;
  background: transparent; color: var(--night-text);
  font-family: var(--font-body);
  transition: background var(--dur), border-color var(--dur), transform var(--dur);
}
.story-item:hover { background: rgba(255,255,255,.05); }
.story-item.is-active {
  background: linear-gradient(90deg, rgba(199,155,67,.18), rgba(199,155,67,.04));
  border-color: rgba(199,155,67,.4);
}
.story-item__star {
  flex: none; width: 22px; height: 22px; position: relative;
}
.story-item__star svg { width: 100%; height: 100%; }
.story-item__body { min-width: 0; flex: 1; }
.story-item__name {
  font-family: var(--font-display); font-size: 18px; font-weight: 600;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; color: var(--night-text);
}
.story-item.is-active .story-item__name { color: var(--gold-bright); }
.story-item__meta { font-family: var(--font-ui); font-size: 11px; color: var(--night-text-soft); opacity: .75; }

/* ---- Cards / sections ---- */
.card {
  background: var(--parchment-raised);
  border: 1px solid var(--parchment-edge);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
}

.panel-head {
  display: flex; align-items: center; gap: 10px;
  padding: 16px 18px 12px;
  border-bottom: 1px solid var(--parchment-edge);
}
.panel-head h3 { font-size: 21px; }
.panel-head .panel-sub { font-family: var(--font-ui); font-size: 11px; color: var(--ink-faint); }
.panel-body { padding: 16px 18px; overflow-y: auto; flex: 1; min-height: 0; }
.panel-foot {
  padding: 12px 18px; border-top: 1px solid var(--parchment-edge);
  display: flex; gap: 10px; justify-content: flex-end;
}

/* ---- Empty states ---- */
.empty {
  margin: auto; max-width: 440px; text-align: center; padding: 40px 24px;
  display: flex; flex-direction: column; align-items: center; gap: 12px;
}
.empty__glyph { width: 96px; height: 96px; opacity: .9; }
.empty h2 { font-size: 30px; }
.empty p { color: var(--ink-soft); }

/* ---- Toasts ---- */
.toasts {
  position: fixed; right: 20px; bottom: 20px; z-index: 1200;
  display: flex; flex-direction: column; gap: 10px; max-width: 380px;
}
.toast {
  display: flex; align-items: flex-start; gap: 10px;
  background: var(--night); color: var(--night-text);
  border: 1px solid var(--night-line); border-left: 3px solid var(--gold);
  border-radius: 10px; padding: 12px 14px; box-shadow: var(--shadow-lg);
  font-family: var(--font-ui); font-size: 13.5px;
  animation: toast-in var(--dur) var(--ease);
}
.toast.is-error { border-left-color: var(--danger); }
.toast.is-success { border-left-color: var(--success); }
.toast.is-notice { border-left-color: var(--gold); }
.toast.is-notice .icon { color: var(--gold-bright); }
.toast__msg { flex: 1; }
.toast__close { background: none; border: none; color: var(--night-text-soft); cursor: pointer; font-size: 16px; }
@keyframes toast-in { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: none; } }

/* ---- Modal ---- */
.modal-scrim {
  position: fixed; inset: 0; z-index: 1000;
  background: rgba(23, 29, 51, .5);
  backdrop-filter: blur(3px);
  display: flex; align-items: center; justify-content: center; padding: 24px;
  animation: fade-in var(--dur) var(--ease);
}
@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }
.modal {
  width: 100%; max-width: 560px; max-height: 88vh;
  display: flex; flex-direction: column;
  background: var(--parchment);
  border: 1px solid var(--parchment-edge);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  overflow: hidden;
  animation: modal-in var(--dur) var(--ease);
}
.modal--wide { max-width: 720px; }
@keyframes modal-in { from { opacity: 0; transform: translateY(12px) scale(.98); } to { opacity: 1; transform: none; } }
.modal__head {
  display: flex; align-items: center; gap: 12px;
  padding: 18px 22px; border-bottom: 1px solid var(--parchment-edge);
}
.modal__head h2 { font-size: 24px; }
.modal__head .modal__sub { font-family: var(--font-ui); font-size: 12px; color: var(--ink-faint); }
.modal__body { padding: 20px 22px; overflow-y: auto; }
.modal__foot {
  display: flex; gap: 10px; justify-content: flex-end;
  padding: 16px 22px; border-top: 1px solid var(--parchment-edge);
  background: var(--parchment-sunk);
}
.modal__foot .spacer { flex: 1; }

/* ---- Loading: constellation-themed ---- */
/* Inline spinner = a lone star orbiting a faint ring. */
.spinner {
  --sp-size: 16px;
  --sp-color: var(--gold-bright);
  --sp-track: var(--gold-glow);
  width: var(--sp-size); height: var(--sp-size);
  display: inline-block; position: relative; vertical-align: -3px;
}
.spinner::before {
  content: ""; position: absolute; inset: 0; border-radius: 50%;
  border: 1.5px solid var(--sp-track);
}
.spinner::after {
  content: ""; position: absolute; top: 0; left: 50%;
  width: calc(var(--sp-size) * .3); height: calc(var(--sp-size) * .3);
  margin-left: calc(var(--sp-size) * -.15);
  border-radius: 50%; background: var(--sp-color);
  box-shadow: 0 0 calc(var(--sp-size) * .4) var(--sp-color);
  transform-origin: calc(var(--sp-size) * .15) calc(var(--sp-size) * .5);
  animation: orbit .85s linear infinite;
}
.spinner--night { --sp-track: rgba(255,255,255,.22); --sp-color: var(--gold-bright); }
@keyframes orbit { to { transform: rotate(360deg); } }

/* Larger loader = a constellation woven star-by-star, repeating. */
.cloader { display: inline-block; line-height: 0; }
.cloader svg { width: 100%; height: auto; display: block; overflow: visible; }
.cloader__line {
  stroke: var(--gold); stroke-width: 1.6; fill: none; stroke-linecap: round; stroke-linejoin: round;
  stroke-dasharray: 100; stroke-dashoffset: 100;
  filter: drop-shadow(0 0 3px var(--gold-glow));
  animation: cloader-draw 2.4s ease-in-out infinite;
}
.cloader__star {
  fill: var(--gold-bright); transform-box: fill-box; transform-origin: center;
  animation: cloader-tw 1.7s ease-in-out infinite;
}
@keyframes cloader-draw {
  0%   { stroke-dashoffset: 100; }
  45%  { stroke-dashoffset: 0; }
  60%  { stroke-dashoffset: 0; }
  100% { stroke-dashoffset: -100; }
}
@keyframes cloader-tw {
  0%, 100% { opacity: .35; }
  50%      { opacity: 1; }
}

@media (prefers-reduced-motion: reduce) {
  .spinner::after { animation: none; top: auto; bottom: 0; }
  .cloader__line { animation: none; stroke-dashoffset: 0; }
  .cloader__star { animation: none; opacity: .85; }
}

.mono {
  font-family: "SFMono-Regular", ui-monospace, "JetBrains Mono", Menlo, Consolas, monospace;
  font-size: 12.5px;
}

.badge-count {
  display: inline-flex; align-items: center; justify-content: center;
  min-width: 18px; height: 18px; padding: 0 5px; border-radius: 999px;
  font-family: var(--font-ui); font-size: 10.5px; font-weight: 700;
  background: var(--night-soft); color: var(--night-text);
}
.tab.is-active .badge-count { background: var(--gold); color: #2a2008; }
