/* Mondopie — single stylesheet, no build step. */

@layer reset, tokens, base, components;

@layer reset {
  *, *::before, *::after { box-sizing: border-box; }
  * { margin: 0; }
  input, button { font: inherit; }
  img, svg { display: block; max-width: 100%; }
}

@layer tokens {
  :root {
    --bg: #f2f7f6;
    --surface: #ffffff;
    --text: #17211f;
    --text-muted: #4f6360;
    --brand: #0e7490;
    --brand-strong: #155e75;
    --brand-contrast: #ffffff;
    --accent: #b45309;
    --accent-soft: #fdf1e3;
    --border: #dde7e4;
    --danger: #b91c1c;
    --danger-bg: #fef2f2;
    --ok: #15803d;
    --focus: #2563eb;
    --radius: 10px;
    --shadow: 0 1px 3px rgb(0 0 0 / 0.08), 0 4px 16px rgb(0 0 0 / 0.06);
  }
  @media (prefers-color-scheme: dark) {
    :root {
      --bg: #0d1514;
      --surface: #15201e;
      --text: #eef5f3;
      --text-muted: #9db3ad;
      --brand: #26b6d4;
      --brand-strong: #4dd0e8;
      --brand-contrast: #062a33;
      --accent: #f0a24b;
      --accent-soft: #2c2113;
      --border: #2b3a37;
      --danger: #f87171;
      --danger-bg: #35191a;
      --ok: #4ade80;
      --focus: #60a5fa;
      --shadow: 0 1px 3px rgb(0 0 0 / 0.4);
    }
  }
}

@layer base {
  html { color-scheme: light dark; }
  body {
    background: var(--bg);
    color: var(--text);
    font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
    line-height: 1.5;
    min-height: 100dvh;
  }
  h1, h2, h3 { line-height: 1.2; text-wrap: balance; }
  a { color: var(--brand); }
  :focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
  @media (prefers-reduced-motion: reduce) {
    *, *::before, *::after { animation: none !important; transition: none !important; }
  }
}

@layer components {
  .page-center {
    min-height: 100dvh;
    display: grid;
    place-content: center;
    justify-items: center;
    gap: 1.5rem;
    padding: 1.5rem;
    text-align: center;
  }
  .brand-mark { display: flex; align-items: center; gap: 0.6rem; }
  .brand-mark img { inline-size: 2rem; }
  .brand-mark .name { font-size: 1.5rem; font-weight: 700; letter-spacing: -0.02em; }
  .tagline { color: var(--text-muted); max-inline-size: 32ch; }

  .card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 1.5rem;
    inline-size: min(24rem, 100%);
    text-align: start;
  }
  .card h2 { font-size: 1.1rem; margin-block-end: 1rem; }

  .field { display: grid; gap: 0.3rem; margin-block-end: 0.9rem; }
  .field label { font-size: 0.875rem; font-weight: 600; }
  .field input {
    padding: 0.55rem 0.7rem;
    border: 1px solid var(--border);
    border-radius: 6px;
    background: var(--bg);
    color: var(--text);
    inline-size: 100%;
  }
  .field .hint { font-size: 0.8rem; color: var(--text-muted); }

  .button {
    display: inline-flex;
    justify-content: center;
    align-items: center;
    gap: 0.4rem;
    padding: 0.55rem 1rem;
    border-radius: 6px;
    border: 1px solid transparent;
    background: var(--brand);
    color: var(--brand-contrast);
    font-weight: 600;
    cursor: pointer;
    inline-size: 100%;
  }
  .button:hover { background: var(--brand-strong); }
  .button[disabled] { opacity: 0.6; cursor: wait; }
  .button.secondary {
    background: transparent;
    color: var(--text);
    border-color: var(--border);
    inline-size: auto;
  }
  .button.secondary:hover { background: var(--bg); }

  .form-links { display: flex; justify-content: center; gap: 1rem; margin-block-start: 1rem; font-size: 0.875rem; }
  .link-button {
    background: none; border: none; padding: 0;
    color: var(--brand); cursor: pointer; text-decoration: underline; font-size: inherit;
  }

  .form-error {
    color: var(--danger);
    background: var(--danger-bg);
    border-radius: 6px;
    padding: 0.6rem 0.8rem;
    font-size: 0.875rem;
    margin-block-end: 0.9rem;
  }
  .form-error:empty { display: none; padding: 0; margin: 0; }
  .form-notice { color: var(--ok); font-size: 0.875rem; margin-block-end: 0.9rem; }

  /* Authenticated shell — fixed height: the nav never moves; each view
     scrolls inside `main`, never the document. */
  .shell { block-size: 100dvh; display: grid; grid-template-rows: auto 1fr; overflow: hidden; }
  .shell > header {
    display: flex; align-items: center; gap: 1.25rem;
    padding: 0.75rem clamp(1rem, 4vw, 2rem);
    background: var(--surface);
    border-block-end: 1px solid var(--border);
  }
  .shell > header .brand-mark img { inline-size: 1.5rem; }
  .shell > header .brand-mark .name { font-size: 1.1rem; }
  .shell nav { display: flex; gap: 0.25rem; flex-wrap: wrap; }
  .shell nav a {
    text-decoration: none; color: var(--text-muted);
    padding: 0.35rem 0.7rem; border-radius: 6px; font-size: 0.925rem;
  }
  .shell nav a:hover { color: var(--text); background: var(--bg); }
  .shell nav a[aria-current="page"] { color: var(--brand); font-weight: 600; }
  .shell nav a.admin-link { color: var(--brand); }
  .spacer { flex: 1; }
  .shell main { padding: clamp(1rem, 4vw, 2rem); inline-size: min(52rem, 100%); margin-inline: auto; min-block-size: 0; overflow-y: auto; }
  .shell main h1 { font-size: 1.4rem; margin-block-end: 1rem; }

  .kv { display: grid; grid-template-columns: max-content 1fr; gap: 0.4rem 1.25rem; }
  .kv dt { color: var(--text-muted); font-size: 0.9rem; }
  .kv dd { font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }

  .badge {
    display: inline-block; padding: 0.1rem 0.55rem; border-radius: 999px;
    font-size: 0.75rem; font-weight: 600;
    background: var(--brand); color: var(--brand-contrast);
  }

  .table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); }
  table { border-collapse: collapse; inline-size: 100%; font-size: 0.9rem; }
  th, td { text-align: start; padding: 0.6rem 0.9rem; border-block-end: 1px solid var(--border); white-space: nowrap; }
  tbody tr:last-child > * { border-block-end: none; }
  th { color: var(--text-muted); font-weight: 600; font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.04em; }

  .toolbar { display: flex; align-items: center; gap: 0.75rem; margin-block: 1rem; }
  .muted { color: var(--text-muted); font-size: 0.875rem; }

  /* Decision partner dashboard: three panels inside the fixed shell —
     Decision State rail | conversation spine | Receipts rail — every panel
     scrolls internally, the composer spans the full conversation width. */
  .shell main.partner-main {
    display: grid;
    grid-template-areas: "state convo receipts";
    grid-template-columns: minmax(15rem, 21rem) minmax(20rem, 1fr) minmax(17rem, 23rem);
    gap: 1.1rem;
    inline-size: 100%;
    max-inline-size: none;
    overflow: hidden;
    block-size: 100%;
  }
  .partner-main h1 { margin-block-end: 0.25rem; }
  .convo { grid-area: convo; }
  .state-rail { grid-area: state; }
  .receipts { grid-area: receipts; }

  @media (max-width: 1200px) and (min-width: 901px) {
    .shell main.partner-main {
      grid-template-areas: "convo receipts" "convo state";
      grid-template-columns: minmax(20rem, 1fr) minmax(16rem, 21rem);
      grid-template-rows: minmax(0, 1.2fr) minmax(0, 1fr);
    }
  }
  .convo { display: flex; flex-direction: column; min-block-size: 0; }
  .convo-head { flex: none; }
  .thread {
    flex: 1; min-block-size: 0;
    display: flex; flex-direction: column; gap: 0.6rem;
    overflow-y: auto; padding-block: 0.5rem;
  }
  .recall-note { margin-block-end: 0.5rem; }
  .bubble {
    max-inline-size: 85%; padding: 0.6rem 0.9rem; border-radius: 12px;
    white-space: pre-wrap; overflow-wrap: anywhere;
  }
  .bubble.user { align-self: flex-end; background: var(--brand); color: var(--brand-contrast); }
  .bubble.partner {
    align-self: flex-start; background: var(--surface);
    border: 1px solid var(--border);
  }
  .bubble.provisional { opacity: 0.65; font-style: italic; }
  .bubble.provisional::after { content: '▍'; animation: blink 1s step-end infinite; }
  @keyframes blink { 50% { opacity: 0; } }
  .bubble.bubble-error { border-color: var(--danger); color: var(--danger); font-style: normal; }

  /* Rendered partner markdown (safe subset: strong/em/code, bullets, headings) */
  .md-gap { block-size: 0.65em; }
  .md-bullet { padding-inline-start: 1.1em; text-indent: -1.1em; }
  .md-bullet::before { content: '•  '; color: var(--brand); font-weight: 700; }
  .md-heading { font-weight: 700; margin-block: 0.25em 0.1em; }
  .bubble code {
    font-family: ui-monospace, "SF Mono", Menlo, monospace; font-size: 0.85em;
    background: var(--bg); border-radius: 4px; padding: 0 0.3em;
  }

  /* Verdict-aware modes: a brief reads as a document, a challenge announces
     itself, review/pattern get their own tone. */
  .bubble-mode {
    display: block; font-size: 0.72rem; font-weight: 700;
    text-transform: uppercase; letter-spacing: 0.06em;
    color: var(--brand); margin-block-end: 0.35rem;
  }
  .bubble.bubble-brief {
    max-inline-size: 100%; border-inline-start: 3px solid var(--brand);
    background: var(--surface); box-shadow: var(--shadow);
  }
  .bubble.bubble-challenge { border-inline-start: 3px solid var(--danger); }
  .bubble.bubble-challenge .bubble-mode { color: var(--danger); }
  .bubble.bubble-review { border-inline-start: 3px solid var(--ok); }
  .bubble.bubble-review .bubble-mode { color: var(--ok); }
  .bubble.bubble-pattern { border-inline-start: 3px solid var(--text-muted); }
  .bubble.bubble-pattern .bubble-mode { color: var(--text-muted); }
  .chip-mode-challenge { border-color: var(--danger); color: var(--danger); }
  .chip-mode-brief { border-color: var(--brand); color: var(--brand); }
  .chip-mode-review { border-color: var(--ok); color: var(--ok); }

  .composer { display: flex; gap: 0.5rem; margin-block-start: 0.75rem; }
  .composer input {
    flex: 1; padding: 0.55rem 0.7rem; border: 1px solid var(--border);
    border-radius: 6px; background: var(--surface); color: var(--text);
  }
  .composer .button { inline-size: auto; }

  .turn-footer { display: flex; align-items: center; gap: 0.6rem; margin-block-start: -0.25rem; }
  .chip {
    display: inline-block; padding: 0.05rem 0.55rem; border-radius: 999px;
    font-size: 0.75rem; border: 1px solid var(--border); color: var(--text-muted);
  }
  .chip-applied { border-color: var(--ok); color: var(--ok); }
  .chip-reinforced { border-color: var(--brand); color: var(--brand); }
  .chip-superseded { border-color: var(--text-muted); }
  .chip-rejected { border-color: var(--danger); color: var(--danger); }
  .chip-choice { cursor: pointer; background: var(--surface); color: var(--brand); border-color: var(--brand); }
  .choices { display: flex; flex-wrap: wrap; gap: 0.4rem; }

  .memory-strip {
    background: var(--surface); border: 1px solid var(--border);
    border-inline-start: 3px solid var(--accent);
    border-radius: var(--radius); padding: 0.5rem 0.9rem; font-size: 0.875rem;
  }
  .memory-strip summary { cursor: pointer; color: var(--text-muted); }
  .memory-line { margin-block: 0.35rem; }

  .stamp-platform-ledgered { background: var(--brand); color: var(--brand-contrast); }
  .stamp-platform-metered { background: var(--surface); border: 1px solid var(--brand); color: var(--brand); }
  .stamp-app-attested { background: var(--surface); border: 1px dashed var(--text-muted); color: var(--text-muted); }
}

@layer components {
  /* Side rails (Decision State + Receipts): dense dashboard panels on
     desktop, slide-up sheets on mobile. */
  .receipts, .state-rail {
    min-block-size: 0; overflow-y: auto;
    background: var(--surface); border: 1px solid var(--border);
    border-radius: var(--radius); padding: 0.75rem 0.9rem;
    font-size: 0.8rem;
  }
  .receipts h2, .state-rail h2 { font-size: 0.95rem; }

  /* Decision State rail */
  .sp-content { display: grid; gap: 0.6rem; }
  .sp-section h3 {
    font-size: 0.68rem; text-transform: uppercase; letter-spacing: 0.06em;
    color: var(--text-muted); margin-block-end: 0.25rem;
  }
  .sp-list { list-style: none; padding: 0; display: grid; gap: 0.3rem; }
  .sp-list li {
    display: flex; align-items: baseline; gap: 0.4rem;
    padding: 0.3rem 0.45rem; border-radius: 6px; background: var(--bg);
    line-height: 1.35;
  }
  .sp-facts { display: grid; gap: 0.2rem; padding: 0.3rem 0.45rem; border-radius: 6px; background: var(--bg); }
  .sp-fact { display: flex; gap: 0.5rem; align-items: baseline; }
  .sp-fact-key { color: var(--text-muted); flex: none; min-inline-size: 5.5rem; font-size: 0.72rem; }
  .pill {
    flex: none; padding: 0 0.45rem; border-radius: 999px;
    font-size: 0.66rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.03em;
  }
  .pill-info { background: var(--brand); color: var(--brand-contrast); }
  .pill-ok { background: var(--ok); color: var(--surface); }
  .pill-danger { background: var(--danger); color: var(--surface); }
  .pill-muted { background: var(--border); color: var(--text-muted); }

  /* Update animations: a brief amber wash when a panel refreshes, a small
     rise as new bubbles land. Globally disabled under prefers-reduced-motion. */
  .flash { animation: flash-wash 0.9s ease-out; }
  @keyframes flash-wash {
    0% { background-color: var(--accent-soft); box-shadow: inset 2px 0 0 var(--accent); }
    100% { background-color: transparent; box-shadow: inset 0 0 0 transparent; }
  }
  .bubble, .memory-strip, .turn-footer { animation: rise 0.22s ease-out; }
  @keyframes rise {
    from { opacity: 0; transform: translateY(5px); }
    to { opacity: 1; transform: none; }
  }
  .r-head { display: flex; align-items: center; justify-content: space-between; margin-block-end: 0.5rem; }
  .r-close { display: none; }
  .r-content { display: grid; gap: 0.75rem; }
  .receipt-card {
    background: var(--bg); border: 1px solid var(--border);
    border-radius: 8px; padding: 0.6rem 0.75rem;
  }
  .receipt-card h3 {
    font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.05em;
    color: var(--text-muted); margin-block-end: 0.45rem;
  }
  .r-row { display: flex; align-items: baseline; gap: 0.5rem; margin-block: 0.25rem; flex-wrap: wrap; }
  .r-label { color: var(--text-muted); flex: none; min-inline-size: 6.5rem; font-size: 0.72rem; }
  .r-value { font-weight: 600; }
  .r-trust { display: flex; align-items: center; gap: 0.5rem; margin-block: 0.3rem; flex-wrap: wrap; }
  .r-check { color: var(--ok); font-weight: 700; }
  .r-cross { color: var(--text-muted); }
  .r-chips { display: flex; flex-wrap: wrap; gap: 0.4rem; margin-block-end: 0.35rem; }
  .chip-source { border-color: var(--brand); color: var(--brand); font-weight: 600; }
  .r-micro {
    font-family: ui-monospace, "SF Mono", Menlo, monospace;
    font-size: 0.64rem; color: var(--text-muted);
    margin: -0.1rem 0 0.3rem; overflow-wrap: anywhere;
  }
  .stamp { padding: 0.05rem 0.5rem; border-radius: 999px; font-size: 0.68rem; font-weight: 650; white-space: nowrap; margin-inline-start: auto; }
  .mobile-only { display: none; }
  .sheet-backdrop { position: fixed; inset: 0; background: rgb(0 0 0 / 0.45); z-index: 40; }

  @media (max-width: 900px) {
    .shell main.partner-main { grid-template-areas: "convo"; grid-template-columns: minmax(0, 1fr); }
    .mobile-only { display: inline-flex; }
    .r-close { display: inline-flex; }
    .receipts, .state-rail {
      position: fixed; inset: auto 0 0 0; z-index: 50;
      max-block-size: 80dvh; border-radius: var(--radius) var(--radius) 0 0;
      transform: translateY(105%); transition: transform 0.25s ease;
      box-shadow: 0 -8px 30px rgb(0 0 0 / 0.25);
    }
    .receipts.open, .state-rail.open { transform: none; }
  }
}
