/* ==========================================================================
   Inkwell components
   Every border in this file is drawn by the sketch engine (rough.js), not by
   CSS. Classes here own layout, spacing, colour and state; `data-sketch`
   attributes own the outline.
   ========================================================================== */

/* --------------------------------------------------------------------------
   Sketch layer positioning
   The SVG sits behind the element's content and never intercepts input.
   -------------------------------------------------------------------------- */
.sketch { position: relative; }
.sketch > .sketch-layer {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  z-index: 0;
  pointer-events: none;
  overflow: visible;
}
.sketch > :not(.sketch-layer) { position: relative; z-index: 1; }

/* --------------------------------------------------------------------------
   Paper scraps and cards
   -------------------------------------------------------------------------- */
.card {
  position: relative;
  background: var(--paper-lift);
  padding: var(--sp-5);
  margin: var(--sp-4) 0;
  transform: rotate(var(--tilt-a));
  transition: transform var(--dur-base) var(--ease);
}
.card:nth-child(even) { transform: rotate(var(--tilt-b)); }
.card:nth-child(3n) { transform: rotate(var(--tilt-c)); }
.card:hover { transform: rotate(0deg); }

/* Rotation is decorative. Anything the user reads or taps stays upright. */
.card--flat,
.card:has(input), .card:has(button), .card:has(a) { transform: none; }

.card--note { background: #FDF6D8; }          /* sticky note */
[data-theme='dark'] .card--note { background: #2A3026; }

.card--index {
  background: var(--paper-lift);
  background-image: repeating-linear-gradient(
    to bottom,
    transparent 0,
    transparent calc(var(--lh-body) * 1rem - 1px),
    var(--line-faint) calc(var(--lh-body) * 1rem - 1px),
    var(--line-faint) calc(var(--lh-body) * 1rem)
  );
  padding-left: var(--sp-6);
}
/* A drawn red margin line down the left, like an index card. */
.card--index::before {
  content: '';
  position: absolute;
  left: 34px;
  top: 0;
  bottom: 0;
  width: 1.5px;
  background: var(--marker-coral);
  opacity: 0.55;
}

/* Washi tape at the top of a card. */
.tape {
  position: absolute;
  top: -10px;
  left: 50%;
  width: 74px;
  height: 22px;
  transform: translateX(-50%) rotate(var(--tilt-d));
  background: var(--marker-yellow);
  opacity: 0.7;
  z-index: 2;
}
.tape--mint { background: var(--marker-mint); }
.tape--sky  { background: var(--marker-sky); }
.tape--coral{ background: var(--marker-coral); }

/* --------------------------------------------------------------------------
   Buttons
   -------------------------------------------------------------------------- */
.btn {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--sp-2);
  min-height: var(--tap-min);
  padding: var(--sp-2) var(--sp-5);
  font-family: var(--font-body);
  font-size: var(--fs-base);
  color: var(--ink);
  background: transparent;
  border: 0;
  cursor: pointer;
  text-decoration: none;
  transition: transform var(--dur-quick) var(--ease);
  -webkit-tap-highlight-color: transparent;
}
.btn:active { transform: translateY(1px); }
.btn[disabled], .btn[aria-disabled='true'] {
  opacity: 0.45;
  cursor: not-allowed;
  transform: none;
}

/* Primary: a marker scribble that was coloured in quickly, slightly offset. */
.btn--primary .sketch-layer { transform: translate(-1px, 1px); }
.btn--primary:active .sketch-layer { transform: translate(0, 0); }

.btn--danger { color: var(--status-error); }
.btn--ghost { color: var(--ink-soft); }
/* Compact variant. The large target is the default so touch always meets the
   44px minimum; only a precise pointer (mouse/trackpad) gets the tighter size. */
.btn--sm { min-height: var(--tap-min); padding: var(--sp-1) var(--sp-3); font-size: var(--fs-sm); }
@media (pointer: fine) { .btn--sm { min-height: 36px; } }
.btn--icon {
  min-width: var(--tap-min);
  padding: var(--sp-2);
}

.btn-row { display: flex; gap: var(--sp-3); flex-wrap: wrap; align-items: center; }

/* --------------------------------------------------------------------------
   Forms — writing on lined paper
   -------------------------------------------------------------------------- */
.field { margin-bottom: var(--sp-5); }

.field > label,
.field-label {
  display: block;
  font-family: var(--font-hand);
  font-size: var(--fs-xl);
  line-height: var(--lh-snug);
  margin-bottom: var(--sp-1);
  color: var(--ink);
}

.input, .textarea, .select {
  display: block;
  width: 100%;
  min-height: var(--tap-min);
  padding: var(--sp-2) var(--sp-3);
  font-family: var(--font-long);
  font-size: var(--fs-base);
  line-height: var(--lh-body);
  color: var(--ink);
  background: transparent;
  border: 0;
  border-radius: 0;
  transition: box-shadow var(--dur-base) var(--ease);
}
.input::placeholder, .textarea::placeholder { color: var(--pencil); opacity: 1; }
.textarea { min-height: 120px; resize: vertical; }
.select { min-height: var(--tap-min); cursor: pointer; }

/* Focus re-draws the underline thicker with a highlight stroke. */
.sketch:focus-within .sketch-layer { filter: none; }
.field:focus-within .sketch-layer path {
  stroke: var(--ink);
  stroke-width: var(--stroke-active);
}

/* Inline errors: handwriting with a drawn arrow. */
.field-error {
  display: flex;
  align-items: flex-start;
  gap: var(--sp-2);
  margin-top: var(--sp-1);
  font-family: var(--font-hand);
  font-size: var(--fs-lg);
  color: var(--status-error);
}
.field-error::before {
  content: '↳';
  font-size: var(--fs-xl);
  line-height: 1;
}

.hint { font-size: var(--fs-sm); color: var(--pencil); margin-top: var(--sp-1); }

/* Range slider: a hand-drawn groove with a value bubble. */
.range-row { display: flex; align-items: center; gap: var(--sp-4); }
.range-row input[type='range'] {
  flex: 1 1 auto;
  min-height: var(--tap-min);
  accent-color: var(--marker-coral);
  cursor: pointer;
}
.range-value {
  font-family: var(--font-hand);
  font-size: var(--fs-xl);
  min-width: 4.5ch;
  text-align: right;
}

/* --------------------------------------------------------------------------
   Toggle — a hand-drawn pill that fills with a scribble when on
   -------------------------------------------------------------------------- */
.toggle {
  display: inline-flex;
  align-items: center;
  gap: var(--sp-3);
  min-height: var(--tap-min);
  cursor: pointer;
  user-select: none;
}
.toggle input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.toggle-track {
  position: relative;
  width: 56px;
  height: 30px;
  flex: 0 0 auto;
}
.toggle-knob {
  position: absolute;
  top: 4px;
  left: 4px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: var(--paper-lift);
  transition: transform var(--dur-base) var(--ease);
  z-index: 2;
}
.toggle input:checked ~ .toggle-track .toggle-knob { transform: translateX(26px); }
.toggle input:focus-visible ~ .toggle-track { box-shadow: 0 0 0 var(--focus-w) var(--marker-sky); }

/* --------------------------------------------------------------------------
   Status
   -------------------------------------------------------------------------- */
.status-dot {
  display: inline-block;
  width: 14px;
  height: 14px;
  flex: 0 0 auto;
  border-radius: 50%;
}
.status-dot--online  { background: var(--status-online); }
.status-dot--offline { background: transparent; box-shadow: inset 0 0 0 2px var(--status-offline); }
.status-dot--error   { background: transparent; position: relative; }
.status-dot--error::before,
.status-dot--error::after {
  content: '';
  position: absolute;
  left: 50%; top: 50%;
  width: 14px; height: 2px;
  background: var(--status-error);
}
.status-dot--error::before { transform: translate(-50%, -50%) rotate(45deg); }
.status-dot--error::after  { transform: translate(-50%, -50%) rotate(-45deg); }
.status-dot--pending { background: var(--status-pending); animation: pulse 1.4s var(--ease) infinite; }
/* Paused is a deliberate state, not a failure: a half-filled dot reads as
   "switched off on purpose" rather than "offline" or "broken". */
.status-dot--paused  { background: var(--status-offline); opacity: 0.45; }

@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.35; } }

.badge, .tag {
  display: inline-flex;
  align-items: center;
  gap: var(--sp-1);
  font-family: var(--font-hand);
  font-size: var(--fs-lg);
  line-height: 1.3;
  padding: 0 var(--sp-2);
  color: var(--ink);
  white-space: nowrap;
}
.badge { background: var(--paper-deep); }

/* Importance as hand-drawn stars. */
.stars { display: inline-flex; gap: 2px; font-size: var(--fs-sm); color: var(--marker-coral); }

/* --------------------------------------------------------------------------
   Navigation
   -------------------------------------------------------------------------- */
.shell {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  gap: var(--sp-6);
  max-width: var(--shell-max);
  margin: 0 auto;
  padding: var(--sp-6) var(--sp-5) var(--sp-7);
  position: relative;
  z-index: 1;
}
@media (max-width: 1023px) {
  .shell { grid-template-columns: 1fr; padding: var(--sp-5) var(--sp-4) calc(var(--tabbar-h) + var(--sp-5)); }
}

/* Grid items default to `min-width: auto`, which means a descendant with a
   large min-content width (a table, a nowrap button row, a long code block)
   pushes the 1fr track wider than the viewport and the whole page scrolls
   sideways on a phone. `min-width: 0` lets the track honour the viewport and
   makes the offending child scroll or wrap instead of the page. */
.shell > * { min-width: 0; }

.sidebar { position: sticky; top: var(--sp-6); align-self: start; }
@media (max-width: 1023px) { .sidebar { display: none; } }

.brand {
  font-family: var(--font-hand);
  font-size: var(--fs-3xl);
  font-weight: var(--fw-bold);
  margin-bottom: var(--sp-5);
  display: inline-block;
}

.nav-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: var(--sp-1); }
.nav-link {
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  min-height: var(--tap-min);
  padding: var(--sp-1) var(--sp-3);
  font-family: var(--font-hand);
  font-size: var(--fs-xl);
  color: var(--ink-soft);
  text-decoration: none;
}
.nav-link:hover { color: var(--ink); }
.nav-link[aria-current='page'] { color: var(--ink); font-weight: var(--fw-bold); }
.nav-icon { width: 22px; height: 22px; flex: 0 0 22px; }

/* Phone: a drawn bottom tab bar, thumb-reachable. */
.tabbar {
  display: none;
  position: fixed;
  left: 0; right: 0; bottom: 0;
  z-index: 40;
  background: var(--paper);
  padding-bottom: env(safe-area-inset-bottom, 0);
  box-shadow: 0 -1px 0 var(--line-faint);
}
@media (max-width: 1023px) { .tabbar { display: block; } }
.tabbar-inner {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 1fr;
  height: var(--tabbar-h);
  align-items: center;
  max-width: 520px;
  margin: 0 auto;
}
.tab {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  height: 100%;
  min-width: var(--tap-min);
  font-family: var(--font-hand);
  font-size: var(--fs-sm);
  color: var(--pencil);
  text-decoration: none;
}
.tab[aria-current='page'] { color: var(--ink); }
.tab .nav-icon { width: 24px; height: 24px; }

/* --------------------------------------------------------------------------
   Page header
   -------------------------------------------------------------------------- */
.page-head { margin-bottom: var(--sp-6); }
.page-title { position: relative; display: inline-block; }
.page-title .sketch-layer { z-index: -1; }
.page-sub { color: var(--pencil); font-size: var(--fs-base); margin-top: var(--sp-1); }

.toolbar {
  display: flex;
  gap: var(--sp-3);
  align-items: center;
  flex-wrap: wrap;
  margin-bottom: var(--sp-5);
}
.toolbar .grow { min-width: 200px; }

/* --------------------------------------------------------------------------
   Grid of cards
   -------------------------------------------------------------------------- */
.card-grid {
  display: grid;
  gap: var(--sp-5);
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
}
@media (max-width: 767px) { .card-grid { grid-template-columns: 1fr; gap: var(--sp-4); } }

/* Bot card: a character sketch. */
.bot-card { display: flex; gap: var(--sp-4); align-items: flex-start; }
.bot-avatar {
  position: relative;
  width: 64px; height: 64px;
  flex: 0 0 64px;
  display: grid;
  place-items: center;
  font-family: var(--font-hand);
  font-size: var(--fs-2xl);
  overflow: hidden;
}
.bot-avatar img { width: 100%; height: 100%; object-fit: cover; border-radius: 50%; }
.bot-meta { min-width: 0; flex: 1 1 auto; }
.bot-name {
  font-family: var(--font-hand);
  font-size: var(--fs-2xl);
  line-height: var(--lh-tight);
  margin: 0;
  overflow-wrap: anywhere;
}

/* --------------------------------------------------------------------------
   Tables and logs
   -------------------------------------------------------------------------- */
.table-wrap { overflow-x: auto; }
.table { width: 100%; border-collapse: collapse; font-size: var(--fs-sm); }
.table th, .table td { padding: var(--sp-2) var(--sp-3); text-align: left; vertical-align: top; }
.table thead th {
  font-family: var(--font-hand);
  font-size: var(--fs-lg);
  font-weight: var(--fw-medium);
  color: var(--ink-soft);
}
.table tbody tr + tr td { box-shadow: inset 0 1px 0 var(--line-faint); }
/* Actions inside a cell stay on one line. A wrapped button stack makes every
   row tall enough to break the table's scanability. */
.table .btn-row { flex-wrap: nowrap; }

.log {
  font-family: 'Cascadia Mono', 'Consolas', ui-monospace, monospace;
  font-size: var(--fs-sm);
  line-height: 1.5;
  background: var(--paper-deep);
  padding: var(--sp-3);
  max-height: 340px;
  overflow-y: auto;
  overflow-wrap: anywhere;
  margin: 0;
  list-style: none;
}
.log li { padding: 1px 0; }
.log-time { color: var(--pencil); }

/* Only the message itself keeps its own line breaks. Pre-wrapping the whole
   list would render the template's own indentation as blank space, which put
   a screenful of nothing between two lines of conversation. */
.log-text { white-space: pre-wrap; }

/* --------------------------------------------------------------------------
   Memory entries
   -------------------------------------------------------------------------- */
.memory-card { position: relative; padding: var(--sp-4) var(--sp-4) var(--sp-4) var(--sp-5); }
.memory-text { font-size: var(--fs-base); margin: 0 0 var(--sp-2); overflow-wrap: anywhere; }
.memory-foot {
  display: flex;
  gap: var(--sp-3);
  align-items: center;
  flex-wrap: wrap;
  font-size: var(--fs-sm);
  color: var(--pencil);
}

/* --------------------------------------------------------------------------
   Modals, toasts, empty states, loading
   -------------------------------------------------------------------------- */
.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 60;
  background: rgba(34, 37, 46, 0.28);
  display: grid;
  place-items: center;
  padding: var(--sp-4);
}
.modal {
  position: relative;
  width: min(560px, 100%);
  max-height: 86vh;
  overflow-y: auto;
  background: var(--paper-lift);
  padding: var(--sp-6);
  /* Torn-paper edge: an irregular clip, not a straight border. */
  clip-path: polygon(
    0% 12px, 3% 0px, 97% 3px, 100% 14px,
    99% 88%, 100% 100%, 2% 99%, 1% 96%
  );
}
.modal--danger .modal-title { color: var(--status-error); }

.toast-stack {
  position: fixed;
  z-index: 70;
  right: var(--sp-4);
  bottom: calc(var(--tabbar-h) + var(--sp-4));
  display: flex;
  flex-direction: column;
  gap: var(--sp-2);
  max-width: min(360px, calc(100vw - var(--sp-6)));
}
@media (min-width: 1024px) { .toast-stack { bottom: var(--sp-5); } }
.toast {
  position: relative;
  background: var(--paper-lift);
  padding: var(--sp-3) var(--sp-4);
  font-size: var(--fs-sm);
  animation: toast-in var(--dur-base) var(--ease);
}
.toast--error { color: var(--status-error); }
.toast--ok { color: var(--ink); }
@keyframes toast-in {
  from { opacity: 0; transform: translateY(6px) rotate(var(--tilt-c)); }
  to   { opacity: 1; transform: translateY(0) rotate(0); }
}

.empty {
  display: grid;
  place-items: center;
  gap: var(--sp-3);
  text-align: center;
  padding: var(--sp-7) var(--sp-4);
  color: var(--pencil);
}
.empty-doodle { width: 96px; height: 96px; opacity: 0.8; }
.empty-hint { font-family: var(--font-hand); font-size: var(--fs-xl); color: var(--ink-soft); }

.skeleton {
  background: linear-gradient(90deg, var(--paper-deep) 25%, var(--paper-lift) 50%, var(--paper-deep) 75%);
  background-size: 200% 100%;
  animation: shimmer 1.3s linear infinite;
  border-radius: var(--radius-wobble);
  min-height: 1em;
}
@keyframes shimmer { from { background-position: 200% 0; } to { background-position: -200% 0; } }

.loading-doodle { width: 40px; height: 40px; }

/* HTMX request indicator, drawn. */
.htmx-indicator { opacity: 0; transition: opacity var(--dur-base) var(--ease); }
.htmx-request .htmx-indicator, .htmx-request.htmx-indicator { opacity: 1; }
.htmx-request .hide-on-request { opacity: 0.4; pointer-events: none; }

/* --------------------------------------------------------------------------
   Disclosure
   A native <details> carries the inspector's edit and delete forms. It needs no
   JavaScript, keyboard support comes free, and a page of cards stays short
   enough to scan.
   -------------------------------------------------------------------------- */
details > summary {
  cursor: pointer;
  min-height: var(--tap-min);
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  font-family: var(--font-hand);
  font-size: var(--fs-xl);
  line-height: var(--lh-snug);
  color: var(--ink-soft);
  list-style: none;
  user-select: none;
}
details > summary::-webkit-details-marker { display: none; }
details > summary::before {
  content: '▸';
  font-size: var(--fs-sm);
  color: var(--pencil);
}
details[open] > summary { color: var(--ink); }
details[open] > summary::before { content: '▾'; }

/* --------------------------------------------------------------------------
   Filter chips
   -------------------------------------------------------------------------- */
.chips { display: flex; gap: var(--sp-2); flex-wrap: wrap; }
.chip {
  font-family: var(--font-hand);
  font-size: var(--fs-lg);
  min-height: var(--tap-min);
  /* A short label like "All" is narrower than a fingertip; the width floor
     matters as much as the height one. */
  min-width: var(--tap-min);
  justify-content: center;
  padding: 0 var(--sp-3);
  display: inline-flex;
  align-items: center;
  gap: var(--sp-1);
  cursor: pointer;
  color: var(--ink-soft);
  background: transparent;
  border: 0;
}
/* Same rule as .btn--sm: keep the 44px target on touch, tighten on a mouse. */
@media (pointer: fine) { .chip { min-height: 34px; } }
.chip[aria-pressed='true'] { color: var(--ink); font-weight: var(--fw-bold); }
.chip[aria-pressed='true'] .sketch-layer { opacity: 1; }
.chip .sketch-layer { opacity: 0.5; }

/* --------------------------------------------------------------------------
   Login / access denied
   -------------------------------------------------------------------------- */
.gate {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: var(--sp-5);
  position: relative;
  z-index: 1;
}
.gate-inner { width: min(420px, 100%); text-align: center; }
.gate-title { font-size: var(--fs-4xl); margin-bottom: var(--sp-3); }
.gate-doodle { width: 140px; height: 140px; margin: 0 auto var(--sp-4); display: block; }

/* The login gate has no sidebar, so the theme and sound toggles live in a
   fixed corner instead. Kept out of the flow so the card stays centred. */
.gate-utility {
  position: fixed;
  top: var(--sp-4);
  right: var(--sp-4);
  display: flex;
  gap: var(--sp-2);
  z-index: 2;
}
.gate-card { text-align: left; }
.gate-card .gate-title { text-align: center; }
.gate-actions { display: flex; flex-direction: column; gap: var(--sp-3); margin-top: var(--sp-6); }
.gate-actions .btn { width: 100%; justify-content: center; }
.gate-note { margin-top: var(--sp-5); text-align: center; }
.gate-steps { text-align: left; margin-top: var(--sp-4); padding-left: var(--sp-5); }
.gate-steps li { margin-bottom: var(--sp-2); }
.gate-steps code, .gate-code {
  font-family: var(--font-mono);
  font-size: var(--fs-sm);
  background: var(--paper-deep);
  border-radius: var(--radius-sm);
  padding: 2px var(--sp-2);
  overflow-wrap: anywhere;
}
.gate-code {
  display: block;
  padding: var(--sp-3);
  margin-top: var(--sp-3);
  white-space: pre-wrap;
  text-align: left;
  color: var(--ink-soft);
}

@media (prefers-reduced-motion: reduce) {
  .status-dot--pending, .skeleton { animation: none; }
  .toast { animation: none; }
}