/* ============================================================
   mica-sistema.css — El sistema Mica · the component layer
   ------------------------------------------------------------
   Standalone. Supersedes sistema-patch.css. Imports the token
   source of truth and builds every component on it. NO raw colour,
   size, radius, duration, shadow, or z-index appears below — only
   var(--token). This is the layer a broader refactor of the app's
   mica.css folds into: same Catalan vocabulary, same conventions.

   Structure
     0. Import + base document
     1. Typography
     2. Elevation utilities (esmalt i vitrall — 5 levels)
     3. Focus + reduced-motion contract
     4. Buttons
     5. Forms & fields
     6. Chips, tags, flags
     7. Navigation (topbar, tabbar)
     8. Cards & surfaces
     9. Tables
    10. Progress & feedback
    11. Overlays — sheet & modal (teatre)
    12. Named state patterns (.is-ocupat .buit .avis .is-triat + opacity)
    13. Ornament (grout, trencadís rule)
    Toast is a documented NON-pattern — see §12; the app speaks inline.

   Core law: TILES GLAZE · INKS WRITE · FILLS CARRY WHITE.
   Elevation law: RAISED = ACTIONABLE.
   ============================================================ */

@import url("mica-tokens.css");

/* ============================================================
   0 · BASE DOCUMENT
   ============================================================ */
*, *::before, *::after { box-sizing: border-box; }

html { color-scheme: light dark; -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-body);
  font-size: var(--fs-4);
  line-height: var(--lh-body);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

a { color: var(--accent); text-decoration: none; }
a:hover { color: var(--accent-ink); }

::selection { background: color-mix(in srgb, var(--accent) 20%, transparent); }

hr, .grout-divider {
  border: 0;
  height: 1px;
  background: var(--line);
  margin: var(--space-5) 0;
}

/* ============================================================
   1 · TYPOGRAPHY
   ============================================================ */
.display, h1, h2, h3 {
  font-family: var(--font-display);
  line-height: var(--lh-display);
  letter-spacing: var(--tracking-display);
  color: var(--text);
  margin: 0 0 var(--space-3);
  text-wrap: pretty;
}
h1 { font-size: var(--fs-8); font-weight: 700; }
h2 { font-size: var(--fs-7); font-weight: 700; }
h3 { font-size: var(--fs-6); font-weight: 600; }

/* The signature flourish: an italic display line in accent ink. */
.display--flourish { font-style: italic; font-weight: 600; color: var(--accent); }

.lead { font-size: var(--fs-5); line-height: var(--lh-ui); color: var(--text); }
.measure { max-width: var(--measure); }

.overline {
  font-family: var(--font-body);
  font-size: var(--fs-1);
  font-weight: 700;
  letter-spacing: var(--tracking-caps);
  text-transform: uppercase;
  color: var(--muted-firm);
}

.hint, .faint, .footnote { color: var(--muted); }
.hint { font-size: var(--fs-3); line-height: var(--lh-ui); }
.footnote { font-size: var(--fs-2); line-height: var(--lh-ui); }
.faint { color: var(--muted-firm); }   /* muted that survives tinted grounds */

code, kbd, samp, .mono { font-family: var(--font-mono); font-size: 0.92em; }

/* ============================================================
   2 · ELEVATION — esmalt i vitrall (raised = actionable)
   Utilities so any surface can declare its level; components below
   use them or inline the same recipe. Nesting de-escalates.
   ============================================================ */

/* 0 · pla — no ground, no shadow. Prose, briefs, hints, dividers. */
.nivell-0 { background: none; box-shadow: none; border: 0; }

/* 1 · esmaltat — glass ground + edge hairline + inset light, no cast. */
.nivell-1 {
  background: var(--glass-1);
  border: 1px solid var(--edge-soft);
  border-radius: var(--radius);
  box-shadow: var(--shadow-1);
}

/* 2 · alçat — level 1 + a cast. Cards you act on. */
.nivell-2 {
  background: var(--glass-2);
  border: 1px solid var(--edge-soft);
  border-radius: var(--radius);
  box-shadow: var(--shadow-2);
}

/* 3 · vidre — level 2 + backdrop blur. Heroes only, ≤3 per view.
   Every backdrop-filter ships its @supports-not opaque fallback. */
.nivell-3 {
  position: relative;
  background: var(--glass-2);
  border: 1px solid var(--rim);
  border-radius: var(--radius-l);
  box-shadow: var(--shadow-3);
  backdrop-filter: var(--blur-vidre);
  -webkit-backdrop-filter: var(--blur-vidre);
  overflow: hidden;
}
.nivell-3::before {   /* the sheen — top-left glaze highlight */
  content: "";
  position: absolute; inset: 0;
  background: var(--sheen);
  pointer-events: none;
}
@supports not (backdrop-filter: blur(1px)) {
  .nivell-3 { background: var(--surface); }
}

/* 4 · teatre — full-screen, own light, top of l'escala. */
.nivell-4 {
  background: var(--viewer-backdrop);
  color: #f8efe5;
  border-radius: var(--radius-l);
  box-shadow: var(--shadow-teatre);
}

/* ============================================================
   3 · FOCUS + REDUCED-MOTION CONTRACT
   ============================================================ */
:where(a, button, summary, input, textarea, select, [tabindex]):focus-visible {
  outline: 3px solid var(--focus-ring);
  outline-offset: 3px;
  border-radius: var(--radius-s);
}
/* Documented exceptions live with their components (tab inset ring,
   route target suppressed, composite lens ring, theatre rings). */

/* The contract: motion never carries sole meaning; every keyframe/
   transition ships a co-located PRM rule; ambient loops die under PRM.
   Component sections below each restate their own PRM guard. */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
    scroll-behavior: auto !important;
  }
}

/* ============================================================
   4 · BUTTONS
   ============================================================ */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  min-height: 44px;                 /* hit-target floor */
  padding: 0 var(--space-5);
  font-family: var(--font-body);
  font-size: var(--fs-4);
  font-weight: 600;
  line-height: 1;
  border: 1px solid var(--edge-soft);
  border-radius: var(--radius-s);
  background: var(--bar-track);
  color: var(--text);
  cursor: pointer;
  transition: transform var(--dur-1) var(--ease-glass),
              box-shadow var(--dur-1) var(--ease-glass),
              background var(--dur-1) var(--ease-glass);
}
.btn:hover { transform: translateY(-1px); box-shadow: var(--shadow-2); }
.btn:active { transform: translateY(0); box-shadow: var(--shadow-1); }
.btn:disabled { opacity: var(--op-inert); cursor: default; transform: none; box-shadow: none; }

/* primary — fills carry white; the fill deepens in dark (--accent-fill). */
.btn-primary {
  border-color: transparent;
  color: #fff;
  background: linear-gradient(140deg,
    color-mix(in srgb, var(--accent-fill) 88%, var(--accent-2)),
    var(--accent-fill));
  box-shadow: var(--shadow-1), var(--shadow-pop);
}
.btn-primary:hover { box-shadow: var(--shadow-2), var(--shadow-pop); }

/* next — the fill-safe blue precedent (advance). */
.btn-next { border-color: transparent; color: #fff; background: var(--advance); }

/* ghost — flat, ink label, hairline. */
.btn-ghost { background: transparent; border-color: var(--line); color: var(--text); }
.btn-ghost:hover { border-color: var(--accent); color: var(--accent-ink); box-shadow: none; }

/* audio / tool — accent ink on soft ground. */
.btn-audio { background: var(--accent-soft); border-color: transparent; color: var(--accent-ink); }
.btn-audio.speaking { animation: btn-speak var(--dur-3) var(--ease-glass) infinite; }
@keyframes btn-speak { 50% { box-shadow: 0 0 0 4px color-mix(in srgb, var(--accent) 18%, transparent); } }

.btn-small { min-height: 34px; padding: 0 var(--space-4); font-size: var(--fs-3); }

@media (prefers-reduced-motion: reduce) {
  .btn:hover, .btn:active { transform: none; }
  .btn-audio.speaking { animation: none; }
}

/* ============================================================
   5 · FORMS & FIELDS
   ============================================================ */
.camp { display: grid; gap: var(--space-2); }
.camp > label, .etiqueta {
  font-size: var(--fs-2);
  font-weight: 600;
  color: var(--muted-firm);
}

/* Integration fix (19 Jul 2026): the element half of this list sits in :where()
   so it carries zero specificity. Bare fields still get the system look, but an
   app class (.cloze-gap's inline underlined gap, .xat-input's autogrow box)
   must be able to override element defaults during the phased migration —
   input[type="text"] at (0,1,1) was silently beating every (0,1,0) class in
   mica.css. */
.answer-input, :where(input[type="text"], input[type="email"], input[type="search"],
input[type="password"], input[type="number"], textarea, select) {
  width: 100%;
  min-height: 44px;
  padding: var(--space-3) var(--space-4);
  font-family: var(--font-body);
  font-size: var(--fs-4);          /* never below 16px — iOS law */
  line-height: var(--lh-ui);
  color: var(--text);
  background: var(--surface-sunk); /* recessed fill carries the boundary */
  border: 1.5px solid var(--line);
  border-radius: var(--radius-s);
  transition: border-color var(--dur-1) var(--ease-glass),
              box-shadow var(--dur-1) var(--ease-glass);
}
:where(textarea) { min-height: 96px; resize: vertical; }
::placeholder { color: var(--muted); opacity: 1; }

/* The other half of «cap camp de Mica demana una adreça» (dom.js). The JS half
   tells Safari what a field IS so its contact heuristic never has to read the
   sentence around an inline cloze; this removes the affordance itself, so a
   field it classifies anyway cannot grow a contacts button inside it. Neither
   half is sufficient alone — the button is WebKit's, but the dropdown is
   Safari's own and only the naming reaches that. Inert in every other engine. */
input::-webkit-contacts-auto-fill-button,
textarea::-webkit-contacts-auto-fill-button {
  display: none !important;
  visibility: hidden;
  pointer-events: none;
  width: 0; height: 0; margin: 0;
}

.answer-input:focus, :where(input, textarea, select):focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 18%, transparent);
}
.answer-input:disabled, :where(input, textarea):disabled {
  opacity: var(--op-inert); cursor: default;
}

/* correct-settle / miss-shake — colour + motion, each with a static twin */
.answer-input.is-correct { border-color: var(--ok); }
.answer-input.is-miss { border-color: var(--miss); animation: field-shake var(--dur-2) var(--ease-glass); }
@keyframes field-shake { 25% { transform: translateX(-4px); } 75% { transform: translateX(4px); } }
@media (prefers-reduced-motion: reduce) { .answer-input.is-miss { animation: none; } }

/* ============================================================
   6 · CHIPS, TAGS, FLAGS
   ============================================================ */
.chip {
  display: inline-flex; align-items: center; gap: var(--space-2);
  min-height: 34px; padding: 0 var(--space-4);
  font-size: var(--fs-3); font-weight: 600; line-height: 1;
  color: var(--text);
  background: var(--surface);
  border: 1.5px solid var(--line);
  border-radius: var(--radius-s);
  cursor: pointer;
  transition: border-color var(--dur-1) var(--ease-glass), background var(--dur-1) var(--ease-glass);
}
.chip:hover { border-color: var(--accent); }
.chip-used { opacity: var(--op-spent); pointer-events: none; }   /* consumed */

/* flags — 11px bold bands; each hue writes through its -ink variant. */
.flag-chip {
  display: inline-flex; align-items: center;
  padding: var(--space-1) var(--space-3);
  font-size: var(--fs-1); font-weight: 700;
  letter-spacing: 0.02em; border-radius: var(--radius-pill);
}
.flag-natiu   { color: var(--tile-green-ink); background: color-mix(in srgb, var(--tile-green) 14%, var(--surface)); }
.flag-tolerat { color: var(--tile-amber-ink); background: color-mix(in srgb, var(--tile-amber) 16%, var(--surface)); }
.flag-evita   { color: var(--miss);            background: color-mix(in srgb, var(--miss) 12%, var(--surface)); }

/* ============================================================
   7 · NAVIGATION
   ============================================================ */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  gap: var(--space-4);
  padding: var(--space-3) var(--space-5);
  background: var(--glass-2);
  border-bottom: 1px solid var(--edge-soft);
  backdrop-filter: var(--blur-vidre);
  -webkit-backdrop-filter: var(--blur-vidre);
  position: sticky; top: 0; z-index: var(--z-chrome);
}
@supports not (backdrop-filter: blur(1px)) { .topbar { background: var(--surface); } }
.brand {
  font-family: var(--font-display); font-style: italic; font-weight: 600;
  font-size: var(--fs-6); color: var(--accent);
}

.tabbar {
  display: flex; gap: var(--space-1);
  padding: var(--space-1);
  background: var(--glass-2);
  border: 1px solid var(--edge-soft);
  border-radius: var(--radius-pill);
  box-shadow: var(--shadow-1);
}
.tab {
  display: inline-flex; align-items: center; justify-content: center; gap: var(--space-2);
  min-height: 40px; padding: 0 var(--space-4);
  font-size: var(--fs-3); font-weight: 600; color: var(--muted-firm);
  background: transparent; border: 0; border-radius: var(--radius-pill);
  cursor: pointer;
  transition: color var(--dur-1) var(--ease-glass), background var(--dur-1) var(--ease-glass);
}
.tab:hover { color: var(--text); }
.tab.active, .tab.is-current {
  color: var(--accent-ink);
  background: var(--accent-soft);
}
.tab:focus-visible { outline-offset: -3px; }   /* inset ring — pills flex edge-to-edge */

/* ============================================================
   8 · CARDS & SURFACES
   ============================================================ */
.targeta {                    /* the default actionable card — alçat */
  display: block;
  padding: var(--space-5);
  background: var(--glass-2);
  border: 1px solid var(--edge-soft);
  border-radius: var(--radius);
  box-shadow: var(--shadow-2);
  color: inherit;
  transition: transform var(--dur-1) var(--ease-glass), box-shadow var(--dur-1) var(--ease-glass);
}
a.targeta:hover, .targeta.is-link:hover { transform: translateY(-2px); box-shadow: var(--shadow-3); }
.targeta.is-locked { opacity: var(--op-locked); }   /* locked CONTENT */

.hero {                       /* vidre hero surface — porta / finestra */
  position: relative;
  padding: var(--space-6);
  background: var(--glass-2);
  border: 1px solid var(--rim);
  border-radius: var(--radius-l);
  box-shadow: var(--shadow-3);
  backdrop-filter: var(--blur-vidre);
  -webkit-backdrop-filter: var(--blur-vidre);
  overflow: hidden;
}
.hero::before { content: ""; position: absolute; inset: 0; background: var(--sheen); pointer-events: none; }
@supports not (backdrop-filter: blur(1px)) { .hero { background: var(--surface); } }
.hero > * { position: relative; }     /* content above the sheen */

@media (prefers-reduced-motion: reduce) {
  a.targeta:hover, .targeta.is-link:hover { transform: none; }
}

/* ============================================================
   9 · TABLES — ink-on-paper, hairline rows (la gramàtica family)
   ============================================================ */
.taula { width: 100%; border-collapse: collapse; font-size: var(--fs-3); }
.taula caption {
  text-align: left; font-family: var(--font-display); font-weight: 600;
  font-size: var(--fs-6); color: var(--text); padding-bottom: var(--space-3);
}
.taula th {
  text-align: left; font-size: var(--fs-1); font-weight: 700;
  letter-spacing: var(--tracking-caps); text-transform: uppercase;
  color: var(--muted-firm);
  padding: var(--space-2) var(--space-3);
  border-bottom: 1.5px solid var(--line);
}
.taula td {
  padding: var(--space-3);
  border-bottom: 1px solid var(--line);
  color: var(--text);
}
.taula tbody tr:hover { background: color-mix(in srgb, var(--accent) 5%, transparent); }
.taula tr.is-triat td { background: color-mix(in srgb, var(--accent-soft) 68%, var(--surface)); }

/* ============================================================
   10 · PROGRESS & FEEDBACK
   ============================================================ */
.bar { height: 8px; border-radius: var(--radius-pill); background: var(--bar-track); overflow: hidden; }
.bar-fill {
  height: 100%; border-radius: var(--radius-pill);
  background: linear-gradient(90deg, var(--accent-2), var(--accent));
  transition: width var(--dur-3) var(--ease-glass);
}

.feedback {
  padding: var(--space-4) var(--space-5);
  border-radius: var(--radius-m);
  border: 1px solid var(--edge-soft);
}
.feedback.ok   { background: var(--ok-soft); }
.feedback.miss { background: var(--miss-soft); }
.verdict { font-family: var(--font-display); font-weight: 700; font-size: var(--fs-5); line-height: 1.2; }
.feedback.ok .verdict   { color: var(--ok); }    /* large text — legal at ≥19px bold */
.feedback.miss .verdict { color: var(--miss); }
.why { font-size: var(--fs-3); color: var(--text); margin-top: var(--space-2); }

/* ============================================================
   11 · OVERLAYS — sheet & modal (teatre, level 4)
   ============================================================ */
.overlay {
  position: fixed; inset: 0; z-index: var(--z-sheet);
  display: flex; align-items: flex-end; justify-content: center;
  background: color-mix(in srgb, var(--viewer-backdrop) 62%, transparent);
  backdrop-filter: blur(3px);
  -webkit-backdrop-filter: blur(3px);
}
.overlay.is-centered { align-items: center; }

.sheet {                     /* phone bottom-sheet */
  width: 100%; max-width: var(--w-focus);
  max-height: 88vh; overflow: auto;
  padding: var(--space-6) var(--space-5);
  background: var(--surface);
  border-radius: var(--radius-l) var(--radius-l) 0 0;
  box-shadow: var(--shadow-teatre);
  animation: sheet-up var(--dur-2) var(--ease-glass);
}
.modal {                     /* desktop centred dialog */
  width: 100%; max-width: 520px; margin: var(--space-5);
  padding: var(--space-6);
  background: var(--surface);
  border-radius: var(--radius-l);
  box-shadow: var(--shadow-teatre);
  animation: modal-in var(--dur-2) var(--ease-pop);
}
@keyframes sheet-up { from { transform: translateY(100%); } }
@keyframes modal-in { from { transform: scale(0.96); opacity: 0; } }
@media (prefers-reduced-motion: reduce) {
  .sheet, .modal { animation: none; }
}

/* ============================================================
   12 · NAMED STATE PATTERNS  (§9 of SISTEMA.md — the gap patterns)
   Toast/notification is a documented NON-PATTERN: the app speaks
   inline, in place. For "that didn't save" use .avis, never a toast.
   ============================================================ */

/* l'espera · saving — a control held inert for a sub-second local
   save. No spinner (ops are instant & local); dim + a quiet pulse. */
.is-ocupat {
  opacity: var(--op-inert);
  cursor: default;
  pointer-events: none;
  animation: ocupat-pulse 1.2s var(--ease-glass) infinite;
}
@keyframes ocupat-pulse { 50% { opacity: calc(var(--op-inert) * 0.7); } }

/* el buit · empty state — warm, not apologetic. */
.buit {
  text-align: center;
  color: var(--muted);
  font-family: var(--font-display);
  font-style: italic;
  font-size: var(--fs-5);
  line-height: var(--lh-ui);
  padding: var(--space-6) var(--space-4);
}

/* l'avís · inline error — flat (level 0), --miss-soft family, retry
   rides along as a .btn-small. Never a toast. */
.avis {
  display: flex; align-items: center; gap: var(--space-3);
  padding: var(--space-3) var(--space-4);
  color: var(--miss);
  background: var(--miss-soft);
  border: 1px solid color-mix(in srgb, var(--miss) 24%, transparent);
  border-radius: var(--radius-m);
  font-size: var(--fs-3); line-height: var(--lh-ui);
}
.avis .btn-small { margin-left: auto; flex: 0 0 auto; color: var(--miss); border-color: color-mix(in srgb, var(--miss) 40%, transparent); background: transparent; }

/* la tria · selection — the one "selected" look. Label stays --text;
   only decorative accents take --accent-ink. */
.is-triat {
  border-color: var(--accent) !important;
  background: color-mix(in srgb, var(--accent-soft) 68%, var(--surface));
  color: var(--text);
}

/* opacity meanings, resolved:
   --op-inert 0.5 (inert control) · --op-locked 0.55 (locked content)
   · --op-spent 0.25 (consumed). Use via .is-ocupat / .is-locked /
   .chip-used above, or the token directly. */

@media (prefers-reduced-motion: reduce) { .is-ocupat { animation: none; } }

/* ============================================================
   13 · ORNAMENT — trencadís, restrained
   ============================================================ */
/* A grouted rule: a hairline with a small ceramic lozenge node. */
.grout-rule {
  display: flex; align-items: center; gap: var(--space-3);
  color: var(--line);
}
.grout-rule::before, .grout-rule::after { content: ""; flex: 1; height: 1px; background: currentColor; }
.grout-rule > .node {
  width: 9px; height: 9px; border-radius: 2px;
  background: var(--accent); transform: rotate(45deg);
  box-shadow: inset 0 1px 0 rgba(255,255,255,0.35);
}
