/* web-platform 1d — builder + live-preview styling.
   Geometry hints mirror layout.json (letter, 0.5in margins, 11pt) so the
   on-screen preview roughly tracks the compiled PDF. It is an approximation,
   not a LaTeX renderer: the golden guarantee is section ORDER/HEADINGS, which
   app.js takes verbatim from GET /api/layout. */

/* web-ui-redesign Phase 2 (D-A4) — `sheet` last, so chrome can never outrank
   the frozen preview geometry (D6). */
@layer tokens, chrome, sheet;

@layer tokens {
  :root {
    /* Confirmed WCAG 2.2 AA palette (D9) — bound 1:1 to
       openspec/changes/web-ui-redesign/preproposal.yaml `confirmed_palette`.
       Every other named token below is derived/decorative and deliberately
       does NOT use the `--color-` prefix, which is reserved for these 8. */
    --color-background: #FDF8F4;
    --color-surface: #FFFFFF;
    --color-text-body: #2D2A26;
    --color-text-muted: #6B625A;
    --color-accent: #B8502F;
    --color-state-ok: #4E8A6B;
    --color-border-1: #F0E4D8;
    --color-border-2: #EADDD2;

    /* Derived tokens, not part of confirmed_palette. */
    --on-accent: #FFFFFF;
    --border-input: #6B625A; /* = --color-text-muted; 5.97:1 on white, the sole boundary of an input */
    --focus-ring: var(--color-accent);
    --state-warn: #B45309; /* 5.02:1 on white */
    --state-critical: #B91C1C; /* 6.47:1 on white */
    --banner-bg: #FEF3C7;
    --banner-border: #F59E0B;
    --degraded-bg: #FEF9C3;
    --degraded-border: #EAB308;
    --accepted-bg: #F0FDF4;

    /* Sheet (frozen, D6) — current values, unchanged; re-expressed as
       tokens only so rule "every color literal lives in `tokens`" holds. */
    --sheet-surface: #FFFFFF;
    --sheet-ink: #1A1A1A;
    --sheet-sub: #3F3F46;
    --sheet-meta: #52525B;
    --sheet-rule: #D4D4D8;
  }
}

@layer chrome {
  * { box-sizing: border-box; }

  body {
    margin: 0;
    font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    color: var(--color-text-body);
    background: var(--color-background);
  }

  #banner {
    padding: 0.75rem 1rem;
    background: var(--banner-bg);
    border-bottom: 1px solid var(--banner-border);
    font-size: 0.9rem;
  }
  #banner[hidden] { display: none; }

  /* web-ui-redesign Phase 3 (D-A6) — mode shell chrome. `[hidden]` needs
     `!important` because `#crear`/`#modebar` set `display` explicitly, which
     otherwise beats the UA `[hidden]` rule. */
  [hidden] { display: none !important; }

  /* web-ui-redesign Phase 10 — a11y sweep. Keyboard-focus ring on every
     interactive element, painted from the existing `--focus-ring` token
     (D-A4: `--focus-ring: var(--color-accent)`), never a new color literal.
     `:focus-visible` (not `:focus`) keeps the ring keyboard-only, matching
     mouse-interaction expectations. */
  :focus-visible {
    outline: 2px solid var(--focus-ring);
    outline-offset: 2px;
  }

  /* Screen-reader-only utility: visually hidden but still in the
     accessibility tree, unlike `display: none` / `[hidden]`. Standard clip
     pattern (WebAIM/APG). */
  .visually-hidden {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
  }

  /* Top bar: Palita brand on the left, mode switcher on the right. */
  #topbar {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: 0.5rem 1rem;
    padding: 0.6rem 1.5rem;
    border-bottom: 1px solid var(--color-border-1);
    background: var(--color-surface);
  }
  .brand { display: flex; align-items: center; gap: 0.5rem; min-width: 0; }
  .brand-icon { width: 1.9rem; height: 1.9rem; flex: none; color: var(--color-accent); }
  .brand-name {
    font-size: 1.35rem;
    font-weight: 800;
    letter-spacing: -0.02em;
    color: var(--color-text-body);
  }
  .brand-tagline {
    font-size: 0.85rem;
    color: var(--color-text-muted);
    padding-left: 0.6rem;
    border-left: 1px solid var(--color-border-2);
  }
  /* Phones: brand (name over tagline) beside the mode buttons, one row. */
  @media (max-width: 600px) {
    #topbar { flex-wrap: nowrap; padding: 0.5rem 1rem; }
    .brand {
      display: grid;
      grid-template-columns: auto 1fr;
      column-gap: 0.4rem;
      align-items: center;
    }
    .brand-icon { grid-row: span 2; }
    .brand-name { font-size: 1.15rem; line-height: 1.1; }
    .brand-tagline { padding-left: 0; border-left: 0; font-size: 0.72rem; line-height: 1.2; }
    #modebar button { font-size: 0.85rem; padding: 0.3rem 0.6rem; }
  }

  #modebar {
    display: flex;
    gap: 0.5rem;
  }
  #modebar button[aria-pressed="true"] {
    background: var(--color-accent);
    border-color: var(--color-accent);
    color: var(--on-accent);
  }

  #crear,
  #revisar {
    display: flex;
    flex-direction: column;
  }
  #crear-heading,
  #revisar-heading {
    margin: 0;
    padding: 1rem 1.5rem 0;
    font-size: 1.1rem;
  }

  /* Revisar workspace chrome (web-ui-shell "Revisar presents a two-panel
     workspace"): the moved-in #review section gets the same horizontal
     rhythm as .crear-workspace; the left/right extracted-text-vs-findings
     columns themselves are #review-panes below (pre-existing). */
  #revisar #review {
    padding: 0 1.5rem 1.5rem;
  }

  .crear-workspace {
    display: grid;
    grid-template-columns: minmax(320px, 1fr) minmax(320px, 1fr);
    gap: 1.5rem;
    padding: 1.5rem;
    align-items: start;
  }
  @media (max-width: 820px) {
    .crear-workspace { grid-template-columns: 1fr; }
  }

  /* Persistent bottom bar (web-ui-shell): draft-check status + primary
     actions, sticky at the bottom of the Crear workspace while it scrolls. */
  #actionbar {
    position: sticky;
    bottom: 0;
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 1rem;
    padding: 0.75rem 1.5rem;
    background: var(--color-surface);
    border-top: 1px solid var(--color-border-1);
  }
  #actionbar #checks {
    max-width: none;
    margin: 0;
    padding: 0;
    flex: 1 1 auto;
  }
  #actionbar #exports {
    padding: 0;
  }
  /* Phones: keep the sticky bar to two short rows so the form stays usable. */
  @media (max-width: 600px) {
    #actionbar { gap: 0.5rem; padding: 0.5rem 1rem; }
    #actionbar #checks { flex-basis: 100%; }
    #actionbar #exports { gap: 0.5rem; }
    #actionbar button { font-size: 0.85rem; padding: 0.3rem 0.6rem; }
    #save-status { display: none; }
  }

  #builder fieldset,
  #builder .section {
    border: 1px solid var(--color-border-2);
    border-radius: 6px;
    margin: 0 0 1rem;
    padding: 0.75rem 1rem 1rem;
    background: var(--color-surface);
  }

  #builder legend,
  #builder .section > h2 {
    font-size: 0.95rem;
    font-weight: 600;
    margin: 0 0 0.5rem;
    padding: 0 0.25rem;
  }

  #builder label {
    display: block;
    margin: 0.4rem 0;
    font-size: 0.85rem;
    color: var(--color-text-muted);
  }

  #builder input[type="text"],
  #builder input[type="email"],
  #builder input[type="tel"],
  #builder textarea {
    width: 100%;
    margin-top: 0.15rem;
    padding: 0.4rem 0.5rem;
    border: 1px solid var(--border-input);
    border-radius: 4px;
    font: inherit;
  }

  #builder textarea { min-height: 4.5rem; resize: vertical; }

  .hint {
    margin: 0.4rem 0;
    font-size: 0.8rem;
    color: var(--color-text-muted);
  }

  .entry {
    border: 1px dashed var(--color-border-2);
    border-radius: 4px;
    padding: 0.5rem 0.75rem;
    margin: 0.5rem 0;
  }

  .entry-actions,
  .section-actions {
    display: flex;
    gap: 0.5rem;
    margin-top: 0.5rem;
  }

  button {
    font: inherit;
    padding: 0.35rem 0.75rem;
    border: 1px solid var(--border-input);
    border-radius: 4px;
    background: var(--color-surface);
    cursor: pointer;
  }
  button:hover { background: var(--color-background); }
  button:disabled,
  button:disabled:hover {
    cursor: not-allowed;
    opacity: 0.55;
    filter: none;
  }
  button[aria-busy="true"] { cursor: progress; }

  /* Accordion triggers: full-width rows with an open/closed marker, instead
     of free-floating buttons sized to their label. */
  #builder-accordion h3 { margin: 0 0 0.5rem; font-size: 1rem; }
  #builder-accordion h3 > button {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%;
    padding: 0.6rem 0.9rem;
    border-color: var(--color-border-2);
    border-radius: 6px;
    font-weight: 600;
    text-align: left;
  }
  #builder-accordion h3 > button::after {
    content: "+";
    font-size: 1.2rem;
    line-height: 1;
    color: var(--color-text-muted);
  }
  #builder-accordion h3 > button[aria-expanded="true"] {
    border-color: var(--color-accent);
    color: var(--color-accent);
  }
  #builder-accordion h3 > button[aria-expanded="true"]::after { content: "\2212"; }
  /* The trigger already names each panel; its fieldset legend stays for
     assistive tech only (same clip pattern as .visually-hidden). */
  #builder-accordion legend {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
  }
  .entry-actions button { font-size: 0.85rem; padding: 0.25rem 0.6rem; }
  .entry-actions .entry-remove { margin-left: auto; color: var(--state-critical); }
  .entry-add { margin-top: 0.25rem; }

  #save-status {
    font-size: 0.85rem;
    color: var(--color-text-muted);
    opacity: 0;
    transition: opacity 0.3s;
  }
  #save-status.visible { opacity: 1; }
  #tailor-run { margin-bottom: 0.75rem; }
  #tailor-status { color: var(--color-text-muted); font-style: italic; }
  #review label[for="review-file"] {
    display: block;
    margin: 0.5rem 0 0.25rem;
    font-size: 0.85rem;
    color: var(--color-text-muted);
  }

  /* Primary actions carry the accent fill (D3/D9). */
  #tailor-btn,
  #tailor-run,
  #export-pdf {
    background: var(--color-accent);
    border-color: var(--color-accent);
    color: var(--on-accent);
  }
  #tailor-btn:hover,
  #tailor-run:hover,
  #export-pdf:hover { filter: brightness(0.92); }

  #exports {
    display: flex;
    gap: 0.75rem;
    padding: 1rem 1.5rem 2rem;
  }

  /* web-platform 1e — live check panel. Counts + findings only, never a score. */
  #checks {
    max-width: 1100px;
    margin: 0 auto;
    padding: 1rem 1.25rem 1.5rem;
  }
  #checks-summary {
    font-size: 1rem;
    font-weight: 600;
    margin: 0 0 .5rem;
  }
  .checks-head { display: flex; align-items: center; gap: 0.75rem; }
  .checks-head #checks-summary { margin: 0; }
  #checks-toggle { font-size: 0.85rem; padding: 0.2rem 0.6rem; }
  #checks-list { list-style: none; margin: 0.5rem 0 0; padding: 0; max-height: 35vh; overflow: auto; }
  #checks-list summary { cursor: pointer; font-size: 0.85rem; color: var(--color-text-muted); }
  #checks-list > li.check { padding: .35rem 0; border-top: 1px solid var(--color-border-1); }
  #checks-list .check-label { font-weight: 600; }
  /* Pass state is a non-text indicator (web-accessibility: --color-state-ok
     is 4.06:1, which fails 4.5:1 as body text); the label itself renders in
     --color-text-body and the pass state is carried by the dot alone. */
  #checks-list li.ok .check-label { color: var(--color-text-body); }
  #checks-list li.ok .check-label::before {
    content: "";
    display: inline-block;
    width: 0.5em;
    height: 0.5em;
    margin-right: 0.4em;
    border-radius: 50%;
    background: var(--color-state-ok);
  }
  #checks-list li.warn .check-label { color: var(--state-warn); }
  #checks-list li.skip .check-label { color: var(--color-text-muted); }

  /* web-ui-redesign Phase 7 (D-A5) — tailoring overlay: full-screen native
     <dialog>. Only existing/derived tokens are used here, no new color
     literal (D-A4: every color literal lives in the `tokens` layer, and this
     chrome introduces none at all). */
  #tailor-overlay {
    position: fixed;
    inset: 0;
    width: 100vw;
    height: 100vh;
    max-width: none;
    max-height: none;
    margin: 0;
    padding: 1.5rem;
    border: none;
    background: var(--color-background);
    overflow: auto;
  }
  #tailor-overlay #tailor-title {
    margin: 0 2.5rem 1rem 0;
    font-size: 1.1rem;
  }
  #tailor-overlay #tailor-close {
    position: absolute;
    top: 1.5rem;
    right: 1.5rem;
  }

  /* web-platform 1f — job posting + tailoring diff. */
  #posting {
    max-width: 1100px;
    margin: 0 auto;
    padding: 1rem 1.25rem 1.5rem;
  }
  #posting textarea {
    width: 100%;
    box-sizing: border-box;
    font: inherit;
    margin: 0.5rem 0;
  }
  #tailor-degraded {
    background: var(--degraded-bg);
    border: 1px solid var(--degraded-border);
    padding: 0.5rem 0.75rem;
    border-radius: 4px;
  }
  .diff-row {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    gap: 0.75rem;
    border-top: 1px solid var(--color-border-1);
    padding: 0.5rem 0;
  }
  .diff-col h4 { margin: 0 0 0.25rem; font-size: 0.85rem; color: var(--color-text-muted); }
  .diff-col ul { margin: 0; padding-left: 1.1rem; }
  .diff-item { border-bottom: 1px solid var(--color-border-1); }
  .diff-item:last-child { border-bottom: 0; }
  .diff-actions {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0 0 0.5rem;
  }
  .diff-status { font-size: 0.85rem; color: var(--color-text-muted); }
  .diff-item.accepted { background: var(--accepted-bg); }
  .diff-item.accepted .diff-status { color: var(--color-text-body); }
  .diff-item.accepted .diff-status::before {
    content: "";
    display: inline-block;
    width: 0.5em;
    height: 0.5em;
    margin-right: 0.4em;
    border-radius: 50%;
    background: var(--color-state-ok);
  }
  .diff-item.rejected { opacity: 0.6; }
  .rejection {
    border-left: 3px solid var(--state-warn);
    padding: 0.35rem 0 0.35rem 0.6rem;
    margin: 0.5rem 0;
  }
  .rejection-why { font-weight: 600; color: var(--state-warn); margin: 0 0 0.2rem; }
  .rejection-bullet { margin: 0.1rem 0; color: var(--color-text-muted); }

  @media (max-width: 720px) {
    .diff-row { grid-template-columns: 1fr; }
  }
  #checks-list ul { margin: .25rem 0 0; padding-left: 1.1rem; font-size: .9rem; color: var(--color-text-muted); }

  /* --- CV review panel (web-platform 3b) ----------------------------------- */
  #review-panes {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1rem;
    margin-top: 0.75rem;
  }
  #review-text {
    white-space: pre-wrap;
    max-height: 24rem;
    overflow: auto;
    background: var(--color-surface);
    border: 1px solid var(--color-border-1);
    padding: 0.5rem;
    font-size: 0.82rem;
  }
  #review-observations ul { margin: 0.25rem 0; padding-left: 1.1rem; font-size: 0.9rem; }
  #review-observations .disagreement { color: var(--state-warn); }
  #review-observations .finding { display: block; margin: 0.3rem 0; }
  #review-observations .finding-critical strong { color: var(--state-critical); }
  #review-observations .finding-warn strong { color: var(--state-warn); }
  #review-degraded { color: var(--state-warn); font-size: 0.9rem; }
  #review-privacy { color: var(--color-text-muted); }

  /* Preview extras kept out of the frozen `sheet` layer. */
  #preview .preview-empty {
    margin: 2rem 0;
    text-align: center;
    font-style: italic;
    color: var(--color-text-muted);
  }
  #preview .p-skills > div { margin: 0.1rem 0; }
  @media (max-width: 720px) {
    #review-panes { grid-template-columns: 1fr; }
  }
}

@layer sheet {
  #preview {
    position: sticky;
    top: 1.5rem;
    background: var(--sheet-surface);
    border: 1px solid var(--sheet-rule);
    border-radius: 6px;
    padding: 0.5in;
    min-height: 60vh;
    font-size: 11pt;
  }

  #preview-contact { text-align: center; margin-bottom: 0.75rem; }
  #preview-contact .name { font-size: 1.5rem; font-weight: 700; }
  #preview-contact .line { font-size: 0.85rem; color: var(--sheet-sub); }
  #preview-contact img.photo {
    float: right;
    width: 150px;
    height: 200px;
    object-fit: cover;
    border: 1px solid var(--sheet-rule);
  }

  #preview-sections h3 {
    font-size: 1rem;
    border-bottom: 1px solid var(--sheet-ink);
    margin: 0.9rem 0 0.35rem;
    padding-bottom: 0.1rem;
  }

  #preview-sections .p-entry { margin: 0.35rem 0; }
  #preview-sections .p-entry .head { font-weight: 600; }
  #preview-sections .p-entry .sub { font-style: italic; color: var(--sheet-sub); }
  #preview-sections .p-entry .dates { float: right; color: var(--sheet-meta); font-size: 0.85rem; }
  #preview-sections ul { margin: 0.25rem 0 0.25rem 1.1rem; padding: 0; }
}
