*, *::before, *::after { box-sizing: border-box; }
/* Components set their own display (flex/grid); the hidden attribute must still win. */
[hidden] { display: none !important; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--paper) var(--grain);
  color: var(--ink);
  font: 400 var(--text-base)/1.65 var(--font-body);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  font-feature-settings: "tnum" 1;
}
h1, h2, h3, h4 { font-family: var(--font-display); font-weight: 600; letter-spacing: .01em; margin: 0; color: var(--ink); }
h1 { font-size: var(--text-2xl); line-height: 1.25; }
h2 { font-size: var(--text-xl); line-height: 1.3; }
h3 { font-size: var(--text-lg); line-height: 1.35; }
h4 { font-size: var(--text-md); }
p { margin: 0; }
a { color: var(--indigo); text-decoration: none; }
a:hover { text-decoration: underline; text-underline-offset: 3px; }
img { display: block; max-width: 100%; }
button, input, textarea, select { font: inherit; color: inherit; }
::selection { background: var(--rose-soft); color: var(--rose-deep); }
:focus-visible { outline: 2px solid var(--rose); outline-offset: 2px; border-radius: 4px; }

.eyebrow { font: 600 11px/1.2 var(--font-body); letter-spacing: .16em; text-transform: uppercase; color: var(--ink-3); }
.muted { color: var(--ink-3); }
.hand { font-family: var(--font-hand); }
.numeral { font-family: var(--font-numeral); font-variant-numeric: lining-nums tabular-nums; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.stack { display: flex; flex-direction: column; gap: var(--space-3); }
.row { display: flex; align-items: center; gap: var(--space-2); }
.spacer { flex: 1; }
.clamp-2 { display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.clamp-3 { display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; }

/* ---- buttons ---- */
.btn {
  --btn-bg: var(--sheet-raised); --btn-fg: var(--ink); --btn-border: var(--rule);
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 34px; padding: 0 14px; border-radius: 999px;
  border: 1px solid var(--btn-border); background: var(--btn-bg); color: var(--btn-fg);
  font-size: var(--text-sm); font-weight: 500; white-space: nowrap; cursor: pointer;
  transition: transform var(--duration-fast) var(--ease-out), background var(--duration-fast), border-color var(--duration-fast), box-shadow var(--duration-fast);
}
.btn:hover { border-color: var(--ink-4); box-shadow: 0 4px 14px -8px rgba(28, 34, 31, .35); }
.btn:active { transform: translateY(1px) scale(.985); }
.btn[disabled] { opacity: .45; cursor: not-allowed; box-shadow: none; }
.btn.primary { --btn-bg: var(--ink); --btn-fg: var(--sheet); --btn-border: var(--ink); }
.btn.primary:hover { --btn-bg: #2b332f; }
.btn.rose { --btn-bg: var(--rose); --btn-fg: #fff; --btn-border: var(--rose); }
.btn.rose:hover { --btn-bg: var(--rose-deep); }
.btn.ghost { --btn-bg: transparent; --btn-border: transparent; }
.btn.ghost:hover { --btn-bg: rgba(28, 34, 31, .05); box-shadow: none; }
.btn.small { height: 28px; padding: 0 10px; font-size: var(--text-xs); }
.btn.icon { width: 34px; padding: 0; }
.btn.small.icon { width: 28px; }
.btn .kbd { font-size: 11px; color: var(--ink-3); border: 1px solid var(--rule); border-radius: 4px; padding: 0 4px; }

/* ---- form ---- */
.field { display: flex; flex-direction: column; gap: 6px; font-size: var(--text-sm); color: var(--ink-2); }
.field > span { font-weight: 500; }
.input, .textarea, .select {
  width: 100%; border: 1px solid var(--rule); border-radius: var(--radius-sm); background: var(--sheet-raised);
  padding: 8px 11px; font-size: var(--text-base); color: var(--ink); transition: border-color var(--duration-fast), box-shadow var(--duration-fast);
}
.textarea { resize: vertical; min-height: 84px; line-height: 1.6; }
.input:focus, .textarea:focus, .select:focus { outline: none; border-color: var(--ink-3); box-shadow: 0 0 0 3px rgba(176, 72, 90, .12); }
.search {
  display: flex; align-items: center; gap: 8px; height: 36px; padding: 0 12px; border-radius: 999px;
  border: 1px solid var(--rule); background: var(--sheet-raised); min-width: 220px;
}
.search input { border: 0; background: transparent; outline: none; flex: 1; min-width: 0; }
.search svg { flex: none; color: var(--ink-3); }

/* ---- chips, badges, seals ---- */
.chip {
  display: inline-flex; align-items: center; gap: 5px; height: 26px; padding: 0 10px; border-radius: 999px;
  border: 1px solid var(--rule); background: var(--sheet-raised); font-size: var(--text-xs); color: var(--ink-2); cursor: pointer;
  transition: background var(--duration-fast), border-color var(--duration-fast), color var(--duration-fast);
}
.chip:hover { border-color: var(--ink-4); color: var(--ink); }
.chip.active, .chip[aria-pressed="true"] { background: var(--ink); border-color: var(--ink); color: var(--sheet); }
.chip .count { color: inherit; opacity: .6; font-variant-numeric: tabular-nums; }
.tag { display: inline-flex; align-items: center; height: 22px; padding: 0 8px; border-radius: 4px; font-size: 11.5px; background: var(--paper-deep); color: var(--ink-2); white-space: nowrap; }
.tag.rose { background: var(--rose-soft); color: var(--rose-deep); }
.tag.celadon { background: var(--celadon-soft); color: #3c5c4c; }
.tag.ochre { background: var(--ochre-soft); color: #7c5a22; }
.tag.indigo { background: var(--indigo-soft); color: var(--indigo); }
.tag.outline { background: transparent; border: 1px solid var(--rule); }
.seal {
  display: inline-grid; place-items: center; min-width: 22px; height: 22px; padding: 0 5px; border-radius: 3px;
  font: 600 12px/1 var(--font-display); color: #fff; background: var(--vermilion); letter-spacing: .04em;
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, .25);
}
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--ink-4); flex: none; }

/* ---- AI annotation: vermilion marginal note in brush script ---- */
.annotation {
  position: relative; padding: 2px 0 2px 14px; font: 15px/1.55 var(--font-hand); color: var(--rose-deep);
}
.annotation::before { content: ""; position: absolute; left: 0; top: 6px; bottom: 6px; width: 3px; border-radius: 3px; background: linear-gradient(var(--rose), rgba(176, 72, 90, .25)); }
.annotation .by { font: 500 10.5px/1 var(--font-body); letter-spacing: .12em; color: var(--rose); margin-right: 6px; vertical-align: 1px; }

/* ---- surfaces ---- */
.sheet { background: var(--sheet); border: 1px solid var(--rule-soft); border-radius: var(--radius-lg); box-shadow: var(--shadow-paper); }
.panel-title { display: flex; align-items: baseline; gap: 10px; margin-bottom: var(--space-4); }
.panel-title h3 { font-size: var(--text-md); }
.panel-title .spacer + * { font-size: var(--text-xs); }
.rule { height: 1px; background: var(--rule-soft); border: 0; margin: var(--space-4) 0; }
.empty { display: grid; place-items: center; gap: 10px; text-align: center; padding: var(--space-7) var(--space-5); color: var(--ink-3); }
.empty h3 { font-size: var(--text-lg); color: var(--ink-2); }

/* ---- table ---- */
.table { width: 100%; border-collapse: collapse; font-size: var(--text-sm); }
.table th { text-align: left; font-weight: 500; color: var(--ink-3); font-size: var(--text-xs); letter-spacing: .06em; padding: 8px 10px; border-bottom: 1px solid var(--rule); white-space: nowrap; }
.table td { padding: 10px; border-bottom: 1px solid var(--rule-soft); vertical-align: top; }
.table tr:hover td { background: rgba(255, 253, 248, .7); }
.table .num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }

/* ---- overlays ---- */
.drawer-backdrop, .modal-backdrop {
  position: fixed; inset: 0; background: rgba(28, 34, 31, .28); backdrop-filter: blur(2px); z-index: 40;
  animation: fade-in var(--duration) var(--ease-out);
}
.drawer {
  position: fixed; top: 0; right: 0; bottom: 0; width: min(560px, 100vw); z-index: 41; background: var(--sheet);
  border-left: 1px solid var(--rule); box-shadow: var(--shadow-float); display: flex; flex-direction: column;
  animation: drawer-in var(--duration) var(--ease-out);
}
.drawer header { display: flex; align-items: flex-start; gap: 12px; padding: 20px 22px 14px; border-bottom: 1px solid var(--rule-soft); }
.drawer header h2 { font-size: var(--text-lg); flex: 1; }
.drawer .drawer-body { flex: 1; overflow: auto; padding: 18px 22px 28px; }
.drawer footer { padding: 14px 22px; border-top: 1px solid var(--rule-soft); display: flex; gap: 8px; flex-wrap: wrap; background: var(--sheet-raised); }
.modal {
  position: fixed; left: 50%; top: 12vh; transform: translateX(-50%); z-index: 41; width: min(560px, calc(100vw - 32px));
  max-height: 76vh; overflow: auto; background: var(--sheet); border-radius: var(--radius-lg); border: 1px solid var(--rule);
  box-shadow: var(--shadow-float); padding: 22px 24px; animation: modal-in var(--duration) var(--ease-out);
}
.modal h2 { font-size: var(--text-lg); margin-bottom: 14px; }
.modal .actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; }
.toast-stack { position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%); z-index: 60; display: flex; flex-direction: column; gap: 8px; align-items: center; pointer-events: none; }
.toast {
  pointer-events: auto; background: var(--ink); color: var(--sheet); padding: 10px 16px; border-radius: 999px; font-size: var(--text-sm);
  box-shadow: var(--shadow-lift); animation: toast-in var(--duration) var(--ease-out); display: flex; gap: 10px; align-items: center;
}
.toast.error { background: var(--rose-deep); }
.toast button { background: none; border: 0; color: #f3d4d8; cursor: pointer; font-weight: 600; }

/* ---- loading ---- */
.skeleton { background: linear-gradient(90deg, var(--paper-deep) 0%, #f4efe6 50%, var(--paper-deep) 100%); background-size: 200% 100%; animation: shimmer 1.4s linear infinite; border-radius: 6px; }
.spinner { width: 14px; height: 14px; border-radius: 50%; border: 2px solid rgba(28, 34, 31, .15); border-top-color: var(--rose); animation: spin .8s linear infinite; }

@keyframes fade-in { from { opacity: 0; } }
@keyframes rise-in { from { opacity: 0; transform: translateY(8px); } }
@keyframes drawer-in { from { transform: translateX(36px); opacity: 0; } }
@keyframes modal-in { from { transform: translate(-50%, 10px); opacity: 0; } }
@keyframes toast-in { from { transform: translateY(10px); opacity: 0; } }
@keyframes shimmer { to { background-position: -200% 0; } }
@keyframes spin { to { transform: rotate(360deg); } }
@keyframes ink-bloom { 0% { box-shadow: 0 0 0 0 rgba(176, 72, 90, .35); } 100% { box-shadow: 0 0 0 14px rgba(176, 72, 90, 0); } }

.reveal > * { animation: rise-in 520ms var(--ease-out) both; }
.reveal > *:nth-child(2) { animation-delay: 40ms; }
.reveal > *:nth-child(3) { animation-delay: 80ms; }
.reveal > *:nth-child(4) { animation-delay: 120ms; }
.reveal > *:nth-child(5) { animation-delay: 160ms; }
.reveal > *:nth-child(6) { animation-delay: 200ms; }
.reveal > *:nth-child(n+7) { animation-delay: 240ms; }
@media (prefers-reduced-motion: reduce) {
  .reveal > *, .drawer, .modal, .toast { animation: none !important; }
}
