/* Kingdom Come — shared component primitives. */

* { box-sizing: border-box; }
html { -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--serif-body);
  font-size: var(--text-base);
  line-height: 1.6;
  font-feature-settings: "kern", "liga";
}

a { color: inherit; }
button { font-family: inherit; }

/* Honor the [hidden] HTML attribute even when an element has display:flex/grid
   set elsewhere. Browsers' default `[hidden] { display: none }` is too weak
   to override `.foo { display: flex }`. */
[hidden] { display: none !important; }

/* Paper grain (subtle, opt-in via .paper class on body) */
body.paper::before {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  opacity: 0.4;
  mix-blend-mode: multiply;
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='200' height='200'><filter id='n'><feTurbulence type='fractalNoise' baseFrequency='0.85' numOctaves='2' seed='3'/><feColorMatrix values='0 0 0 0 0.4  0 0 0 0 0.36  0 0 0 0 0.3  0 0 0 0.06 0'/></filter><rect width='100%' height='100%' filter='url(%23n)'/></svg>");
  z-index: 0;
}
[data-theme="dark"] body.paper::before { opacity: 0.3; mix-blend-mode: screen; }

/* Global header */
.kc-header {
  position: relative;
  z-index: 1;
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  padding: var(--space-24) var(--space-32);
  border-bottom: 1px solid var(--hairline);
  max-width: var(--dashboard-width);
  margin: 0 auto;
}
.kc-brand {
  font-family: var(--serif-display);
  font-size: var(--text-xl);
  font-weight: 500;
  font-variation-settings: "opsz" 18;
  letter-spacing: -0.01em;
  text-decoration: none;
  color: var(--ink);
  white-space: nowrap;
}
.kc-wordmark {
  font-family: var(--sans);
  font-size: var(--text-xs);
  color: var(--ink-faint);
  text-transform: uppercase;
  letter-spacing: 0.1em;
  margin-left: var(--space-8);
}
.kc-identity {
  display: flex;
  gap: var(--space-16);
  align-items: baseline;
  font-family: var(--sans);
  font-size: var(--text-sm);
  color: var(--ink-muted);
}
.kc-identity .kc-switch {
  font-size: var(--text-xs);
  color: var(--ink-faint);
  text-decoration: none;
  border-bottom: 1px dotted var(--ink-faint);
  cursor: pointer;
}
.kc-identity .kc-switch:hover { color: var(--accent); border-bottom-color: var(--accent); }

/* Subnav */
.kc-subnav {
  position: relative;
  z-index: 1;
  max-width: var(--dashboard-width);
  margin: 0 auto;
  padding: var(--space-12) var(--space-32) 0;
  display: flex;
  gap: var(--space-24);
  font-family: var(--sans);
  font-size: var(--text-sm);
}
.kc-subnav a {
  color: var(--ink-muted);
  text-decoration: none;
  padding-bottom: var(--space-12);
  border-bottom: 2px solid transparent;
  transition: color var(--motion-micro) ease-out, border-color var(--motion-micro) ease-out;
}
.kc-subnav a[aria-current="page"] {
  color: var(--ink);
  border-bottom-color: var(--accent);
}
.kc-subnav a:hover { color: var(--ink); }

/* Main */
.kc-main {
  position: relative;
  z-index: 1;
  max-width: var(--dashboard-width);
  margin: 0 auto;
  padding: var(--space-48) var(--space-32) var(--space-96);
}
.kc-main.reading { max-width: var(--reading-width); }

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-family: var(--sans);
  font-size: var(--text-sm);
  font-weight: 500;
  height: 40px;
  min-width: 44px;
  padding: 0 18px;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  cursor: pointer;
  text-decoration: none;
  transition: background var(--motion-micro) ease-out, transform var(--motion-micro) ease-out;
}
.btn:active { transform: translateY(1px); }
.btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.btn-primary { background: var(--accent); color: var(--accent-ink); }
.btn-primary:hover { background: color-mix(in srgb, var(--accent) 85%, var(--ink)); }
[data-theme="dark"] .btn-primary:hover { background: color-mix(in srgb, var(--accent) 85%, white); }
.btn-secondary { background: var(--surface); color: var(--ink); border-color: var(--hairline); }
.btn-secondary:hover { background: var(--surface-2); }
.btn-ghost { background: transparent; color: var(--ink); }
.btn-ghost:hover { background: var(--surface-2); }
.btn-ghost.inline { padding: 0; height: auto; min-width: 0; color: var(--ink-muted); }
.btn-ghost.inline:hover { background: transparent; color: var(--accent); }

/* Status pills */
.status-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-family: var(--sans);
  font-size: var(--text-xs);
  font-weight: 500;
  line-height: 1;
  padding: 6px 10px;
  border-radius: var(--radius-pill);
}
.status-pill::before { content: ""; width: 6px; height: 6px; border-radius: 50%; }
.status-thriving { background: var(--status-thriving-soft); color: var(--status-thriving); }
.status-thriving::before { background: var(--status-thriving); }
.status-steady { background: var(--status-steady-soft); color: var(--status-steady); }
.status-steady::before { background: var(--status-steady); }
.status-checkin { background: var(--status-checkin-soft); color: var(--status-checkin); }
.status-checkin::before { background: var(--status-checkin); }
.status-risk { background: var(--status-risk-soft); color: var(--status-risk); }
.status-risk::before { background: var(--status-risk); }

/* Avatar */
.avatar {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  font-family: var(--sans);
  font-weight: 500;
  font-size: 14px;
  color: var(--ink);
  flex-shrink: 0;
}
.avatar.sm { width: 32px; height: 32px; font-size: 12px; }

/* Chat bubbles */
.bubble {
  font-family: var(--serif-body);
  font-size: var(--text-lg);
  line-height: 1.55;
  padding: var(--space-16) var(--space-24);
  border-radius: var(--radius-md);
  max-width: 80%;
  margin-bottom: var(--space-12);
  animation: bubble-in var(--motion-short) ease-out both;
}
.bubble.student { background: var(--accent-soft); color: var(--ink); margin-left: auto; }
.bubble.mentor { background: var(--surface); border: 1px solid var(--hairline); color: var(--ink); }
.bubble.error { background: transparent; border: 1px solid var(--accent); color: var(--accent); }
@keyframes bubble-in { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: translateY(0); } }

/* Memory pills */
.memory-pills {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-8);
  align-items: center;
  margin-bottom: var(--space-24);
  font-family: var(--sans);
  font-size: var(--text-xs);
}
.memory-pills .label {
  color: var(--ink-faint);
  text-transform: uppercase;
  letter-spacing: 0.1em;
}
.memory-pill {
  background: var(--surface-2);
  border: 1px solid var(--hairline);
  border-radius: var(--radius-pill);
  padding: 4px 10px;
  color: var(--ink-muted);
  max-width: 280px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Modal */
.kc-modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(27, 26, 24, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  animation: fade-in var(--motion-medium) ease-out;
}
.kc-modal {
  background: var(--surface);
  border-radius: var(--radius-lg);
  width: 480px;
  max-width: calc(100vw - var(--space-32));
  max-height: 80vh;
  overflow: auto;
  padding: var(--space-32);
  position: relative;
  animation: modal-in var(--motion-medium) ease-out;
}
.kc-modal-close {
  position: absolute;
  top: var(--space-16);
  right: var(--space-16);
  background: transparent;
  border: none;
  font-size: 20px;
  color: var(--ink-muted);
  cursor: pointer;
  padding: 4px 8px;
}
.kc-modal-close:hover { color: var(--ink); }
@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }
@keyframes modal-in { from { opacity: 0; transform: scale(0.96); } to { opacity: 1; transform: scale(1); } }

/* Skeleton loader */
.skeleton {
  background: var(--surface-2);
  border-radius: var(--radius-md);
  animation: skeleton-shimmer 1.5s ease-in-out infinite;
}
@keyframes skeleton-shimmer {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.6; }
}

/* Form inputs (modal) */
.kc-field { margin-bottom: var(--space-16); }
.kc-field label {
  display: block;
  font-family: var(--sans);
  font-size: var(--text-xs);
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--ink-faint);
  margin-bottom: var(--space-8);
}
.kc-field input[type="number"],
.kc-field input[type="text"],
.kc-field select,
.kc-field textarea {
  width: 100%;
  font-family: var(--serif-body);
  font-size: var(--text-base);
  background: var(--bg);
  border: 1px solid var(--hairline);
  border-radius: var(--radius-sm);
  padding: var(--space-12);
  color: var(--ink);
}
.kc-field input:focus,
.kc-field select:focus,
.kc-field textarea:focus {
  outline: 2px solid var(--accent);
  outline-offset: 0;
  border-color: var(--accent);
}
.kc-field input[type="range"] { width: 100%; accent-color: var(--accent); }
.kc-field .range-value {
  font-family: var(--sans);
  font-size: var(--text-sm);
  color: var(--ink-muted);
  text-align: right;
  font-feature-settings: "tnum";
}

/* Error toast (in modal) */
.kc-error {
  background: var(--accent-soft);
  color: var(--accent);
  font-family: var(--sans);
  font-size: var(--text-sm);
  padding: var(--space-12);
  border-radius: var(--radius-sm);
  margin-bottom: var(--space-16);
}

/* Empty state */
.empty-state {
  font-family: var(--serif-body);
  font-style: italic;
  color: var(--ink-muted);
  font-size: var(--text-base);
  padding: var(--space-32);
  text-align: center;
  line-height: 1.5;
}

/* Screen-reader-only text */
.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

/* First-run tour — a quiet three-step card, not a spotlight overlay. */
.kc-tour {
  background: var(--surface);
  border: 1px solid var(--accent);
  border-radius: var(--radius-md);
  padding: var(--space-24);
  margin-bottom: var(--space-32);
}
.kc-tour-label {
  font-family: var(--sans);
  font-size: var(--text-xs);
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--accent);
  margin: 0 0 var(--space-8);
}
.kc-tour-title {
  font-family: var(--serif-display);
  font-weight: 400;
  font-size: var(--text-xl);
  margin: 0 0 var(--space-8);
}
.kc-tour-body {
  font-family: var(--serif-body);
  font-size: var(--text-base);
  color: var(--ink-muted);
  margin: 0 0 var(--space-16);
  max-width: 56ch;
}
.kc-tour-footer { display: flex; align-items: center; gap: var(--space-16); }
.kc-tour-dots { font-size: var(--text-xs); color: var(--ink-faint); margin-right: auto; letter-spacing: 0.2em; }

/* Loading skeletons — two quiet placeholder cards render while a data
   container is still aria-busy; they vanish the moment its JS clears the
   attribute (success and error paths both do). Pulse collapses to instant
   under prefers-reduced-motion below. */
[data-skeleton][aria-busy="true"]::before,
[data-skeleton][aria-busy="true"]::after {
  content: "";
  display: block;
  height: 72px;
  border: 1px solid var(--hairline);
  border-radius: var(--radius-md);
  background: var(--surface-2);
  animation: skeleton-pulse 1.2s ease-in-out infinite alternate;
}
[data-skeleton][aria-busy="true"]::after {
  animation-delay: 0.3s;
  margin-top: var(--space-16);
}
@keyframes skeleton-pulse {
  from { opacity: 0.45; }
  to { opacity: 1; }
}

/* Reduced motion */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

/* Mobile */
@media (max-width: 640px) {
  .kc-header, .kc-subnav, .kc-main { padding-left: var(--space-16); padding-right: var(--space-16); }

  /* Thumb-reach bottom nav (B3, Hallow pattern): the persona subnav docks
     to the bottom edge on phones; main gets clearance so nothing hides
     behind it. Active state moves to a top border (it sits above the text). */
  .kc-subnav {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    max-width: none;
    margin: 0;
    padding: 0 var(--space-16);
    justify-content: space-around;
    gap: var(--space-8);
    background: var(--surface);
    border-top: 1px solid var(--hairline);
    padding-bottom: env(safe-area-inset-bottom);
  }
  .kc-subnav a {
    padding: var(--space-12) var(--space-8);
    border-bottom: none;
    border-top: 2px solid transparent;
  }
  .kc-subnav a[aria-current="page"] {
    border-top-color: var(--accent);
  }
  body:has(.kc-subnav) .kc-main {
    padding-bottom: calc(var(--space-64, 64px) + env(safe-area-inset-bottom));
  }
  .kc-wordmark { display: none; }
  .bubble { max-width: 95%; }
  .kc-modal { padding: var(--space-24); border-radius: var(--radius-md); }
}
