@layer reset, tokens, base, components, pages, responsive;

@layer reset {
  *, *::before, *::after { box-sizing: border-box; }
  html { -webkit-text-size-adjust: 100%; }
  body, h1, h2, h3, p { margin: 0; }
  button, input, select { font: inherit; }
  img, svg { display: block; max-width: 100%; }
  [hidden] { display: none !important; }
}

@layer tokens {
  :root {
    color-scheme: light;
    --bg: #f5f7fa;
    --surface: #ffffff;
    --surface-subtle: #f8fafc;
    --ink: #172033;
    --ink-strong: #0b1220;
    --muted: #647084;
    --muted-strong: #475569;
    --line: #dfe5ec;
    --line-strong: #cdd6e1;
    --brand: #0f766e;
    --brand-strong: #115e59;
    --brand-soft: #e7f6f3;
    --brand-line: #99d5cc;
    --accent: #d97706;
    --accent-soft: #fff7e8;
    --danger: #b42318;
    --danger-soft: #fff0ee;
    --success: #16794c;
    --success-soft: #eaf8f0;
    --info: #2563eb;
    --info-soft: #eef4ff;
    --radius-sm: 10px;
    --radius-md: 16px;
    --radius-lg: 24px;
    --shadow-sm: 0 1px 2px rgba(15, 23, 42, .04), 0 8px 24px rgba(15, 23, 42, .05);
    --shadow-lg: 0 24px 70px rgba(15, 23, 42, .10);
    --page: 1160px;
    --gutter: clamp(16px, 4vw, 40px);
    --section-gap: clamp(24px, 4vw, 48px);
    --card-pad: clamp(20px, 3vw, 34px);
  }
}

@layer base {
  body {
    min-height: 100vh;
    background:
      radial-gradient(circle at 8% 0%, rgba(15, 118, 110, .07), transparent 30rem),
      linear-gradient(180deg, #fbfcfd 0, var(--bg) 24rem);
    color: var(--ink);
    font-family: "Aptos", "Segoe UI Variable", "Segoe UI", sans-serif;
    font-size: 16px;
    line-height: 1.6;
  }
  a { color: var(--brand-strong); text-underline-offset: 3px; }
  a:hover { color: var(--brand); }
  button, a { -webkit-tap-highlight-color: transparent; }
  button:focus-visible, a:focus-visible, input:focus-visible, select:focus-visible {
    outline: 3px solid rgba(15, 118, 110, .25);
    outline-offset: 2px;
  }
  h1, h2, h3 { color: var(--ink-strong); line-height: 1.15; letter-spacing: -.025em; text-wrap: balance; }
  h1 { font-size: clamp(2.15rem, 5vw, 4.5rem); }
  h2 { font-size: clamp(1.45rem, 2.7vw, 2rem); }
  h3 { font-size: 1.1rem; }
  p { text-wrap: pretty; }
  .page-shell { width: min(var(--page), calc(100% - (2 * var(--gutter)))); margin-inline: auto; }
  .page-main { padding-block: clamp(28px, 5vw, 64px) clamp(56px, 8vw, 96px); }
  .muted { color: var(--muted); }
  .tiny { color: var(--muted); font-size: .86rem; }
  .eyebrow {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 18px;
    color: var(--brand-strong);
    font-size: .78rem;
    font-weight: 750;
    letter-spacing: .08em;
    text-transform: uppercase;
  }
  .eyebrow::before { content: ""; width: 24px; height: 2px; border-radius: 2px; background: var(--brand); }
}

@layer components {
  .site-header {
    position: sticky;
    z-index: 20;
    top: 0;
    border-bottom: 1px solid rgba(205, 214, 225, .82);
    background: rgba(251, 252, 253, .88);
    backdrop-filter: blur(16px);
  }
  .header-inner { min-height: 72px; display: flex; align-items: center; justify-content: space-between; gap: 24px; }
  .brand { display: inline-flex; align-items: center; gap: 11px; color: var(--ink-strong); font-weight: 800; text-decoration: none; letter-spacing: -.02em; }
  .brand-mark { width: 38px; height: 38px; display: grid; place-items: center; border-radius: 12px; background: var(--ink-strong); color: white; box-shadow: 0 8px 18px rgba(11, 18, 32, .14); }
  .brand-mark svg { width: 20px; height: 20px; }
  .site-nav { display: flex; align-items: center; justify-content: flex-end; gap: 6px; }
  .nav-link, .nav-button {
    min-height: 44px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border: 0;
    border-radius: var(--radius-sm);
    padding: 9px 12px;
    background: transparent;
    color: var(--muted-strong);
    font-weight: 650;
    text-decoration: none;
    cursor: pointer;
  }
  .nav-link:hover, .nav-button:hover { background: #edf1f5; color: var(--ink-strong); }
  .nav-user { margin-inline: 6px 2px; color: var(--muted); font-size: .9rem; }
  .nav-form { display: inline-flex; }
  .panel { border: 1px solid var(--line); border-radius: var(--radius-lg); background: var(--surface); box-shadow: var(--shadow-sm); }
  .panel-pad { padding: var(--card-pad); }
  .button {
    min-height: 46px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 9px;
    border: 1px solid transparent;
    border-radius: 12px;
    padding: 11px 17px;
    background: var(--ink-strong);
    color: white;
    font-weight: 750;
    line-height: 1.2;
    text-align: center;
    text-decoration: none;
    cursor: pointer;
    transition: transform .16s ease, box-shadow .16s ease, background-color .16s ease;
  }
  .button:hover { transform: translateY(-1px); background: #1e293b; color: white; box-shadow: 0 10px 24px rgba(15, 23, 42, .14); }
  .button-primary { background: var(--brand); }
  .button-primary:hover { background: var(--brand-strong); }
  .button-secondary { border-color: var(--line-strong); background: var(--surface); color: var(--ink); }
  .button-secondary:hover { background: var(--surface-subtle); color: var(--ink-strong); }
  .button-danger { border-color: #f0cbc6; background: var(--surface); color: var(--danger); }
  .button-danger:hover { background: var(--danger-soft); color: var(--danger); }
  .button:disabled { transform: none; box-shadow: none; opacity: .55; cursor: not-allowed; }
  .field { display: grid; gap: 8px; }
  .field + .field { margin-top: 20px; }
  label, .label { color: var(--ink-strong); font-size: .94rem; font-weight: 750; }
  input, select {
    min-height: 48px;
    width: 100%;
    border: 1px solid var(--line-strong);
    border-radius: 12px;
    padding: 11px 13px;
    background: var(--surface);
    color: var(--ink-strong);
  }
  input:hover, select:hover { border-color: #aeb9c7; }
  input::placeholder { color: #8a96a7; }
  .help { color: var(--muted); font-size: .88rem; line-height: 1.5; }
  .error-message { margin-top: 16px; color: var(--danger); font-weight: 700; }
  .page-heading { max-width: 760px; margin-bottom: clamp(24px, 4vw, 40px); }
  .page-heading p { max-width: 650px; margin-top: 12px; color: var(--muted); font-size: 1.04rem; }
  .badge { display: inline-flex; align-items: center; min-height: 28px; border-radius: 999px; padding: 4px 10px; font-size: .78rem; font-weight: 750; }
  .status-done { background: var(--success-soft); color: var(--success); }
  .status-running, .status-queued { background: var(--info-soft); color: var(--info); }
  .status-failed { background: var(--danger-soft); color: var(--danger); }
}

@layer pages {
  .hero { display: grid; grid-template-columns: minmax(0, 1.12fr) minmax(340px, .88fr); gap: clamp(20px, 3.5vw, 42px); align-items: start; }
  .intro-panel { padding: clamp(26px, 5vw, 58px); background: var(--ink-strong); color: #d8e1ed; overflow: hidden; position: relative; box-shadow: var(--shadow-lg); }
  .intro-panel::after { content: ""; position: absolute; width: 290px; height: 290px; right: -160px; bottom: -170px; border: 46px solid rgba(94, 234, 212, .12); border-radius: 50%; }
  .intro-panel h1 { max-width: 760px; color: white; font-size: clamp(2.5rem, 5vw, 4.6rem); }
  .intro-copy { max-width: 660px; margin-top: 24px; color: #b9c5d4; font-size: clamp(1rem, 1.4vw, 1.13rem); }
  .intro-panel .eyebrow { color: #7dd3c7; }
  .intro-panel .eyebrow::before { background: #5eead4; }
  .workflow { position: relative; z-index: 1; display: grid; gap: 0; margin-top: clamp(32px, 5vw, 56px); border-top: 1px solid rgba(255,255,255,.14); }
  .workflow-item { display: grid; grid-template-columns: 38px 1fr; gap: 14px; padding-block: 17px; border-bottom: 1px solid rgba(255,255,255,.14); }
  .workflow-number { color: #5eead4; font-size: .78rem; font-weight: 800; letter-spacing: .06em; }
  .workflow-item strong { display: block; color: white; }
  .workflow-item span:last-child { display: block; margin-top: 2px; color: #91a0b2; font-size: .9rem; }
  .upload-panel { position: sticky; top: 96px; padding: var(--card-pad); }
  .upload-panel h2 { margin-bottom: 8px; }
  .upload-panel > .muted { margin-bottom: 24px; }
  .file-field { padding: 16px; border: 1px dashed #aab7c5; border-radius: var(--radius-md); background: var(--surface-subtle); }
  .file-field input { min-height: auto; padding: 0; border: 0; background: transparent; }
  .file-field input::file-selector-button { min-height: 40px; margin-right: 12px; border: 0; border-radius: 10px; padding: 8px 12px; background: var(--ink-strong); color: white; font-weight: 700; cursor: pointer; }
  .upload-submit { width: 100%; margin-top: 24px; }
  .trust-note { display: flex; gap: 9px; margin-top: 18px; color: var(--muted); font-size: .84rem; }
  .trust-note svg { flex: 0 0 auto; width: 17px; margin-top: 4px; color: var(--brand); }

  .status-card { padding: var(--card-pad); }
  .status-top { display: flex; align-items: flex-start; justify-content: space-between; gap: 24px; }
  .job-reference { display: inline-block; margin-bottom: 8px; color: var(--muted); font-family: ui-monospace, "SFMono-Regular", Consolas, monospace; font-size: .78rem; word-break: break-all; }
  .phase { margin-top: 12px; color: var(--muted-strong); font-size: 1.04rem; font-weight: 650; }
  .progress-value { color: var(--brand-strong); font-size: clamp(2.1rem, 5vw, 3.3rem); font-weight: 800; letter-spacing: -.05em; line-height: 1; white-space: nowrap; }
  .progress-track { height: 10px; margin-top: 26px; border-radius: 999px; background: #e5eaf0; overflow: hidden; }
  .progress-fill { height: 100%; width: 0; border-radius: inherit; background: var(--brand); transition: width .35s ease; }
  .progress-meta { display: flex; justify-content: space-between; gap: 16px; margin-top: 10px; color: var(--muted); font-size: .88rem; }
  .timing-strip { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 18px; margin-top: 20px; padding-top: 18px; border-top: 1px solid var(--line); }
  .timing-stat { min-width: 0; }
  .timing-stat span { display: block; margin-bottom: 4px; color: var(--muted); font-size: .8rem; font-weight: 700; letter-spacing: .02em; }
  .timing-stat strong { display: block; overflow-wrap: anywhere; color: var(--ink-strong); font-size: clamp(1rem, 2vw, 1.2rem); font-variant-numeric: tabular-nums; }
  .status-grid { display: grid; grid-template-columns: minmax(0, .8fr) minmax(0, 1.2fr); gap: 20px; margin-top: 20px; }
  .status-section { min-width: 0; padding: var(--card-pad); }
  .status-section h2 { margin-bottom: 16px; font-size: 1.2rem; }
  .messages, .findings, .job-list, .user-list { list-style: none; margin: 0; padding: 0; }
  .messages { display: grid; gap: 8px; max-height: 440px; overflow-y: auto; scroll-behavior: smooth; padding-right: 4px; }
  .messages li { position: relative; padding: 11px 12px 11px 31px; border-radius: 10px; background: var(--surface-subtle); color: var(--muted-strong); font-size: .9rem; }
  .messages li::before { content: ""; position: absolute; left: 13px; top: 18px; width: 6px; height: 6px; border-radius: 50%; background: var(--brand); }
  .messages .message-end { height: 1px; padding: 0; background: transparent; }
  .messages .message-end::before { display: none; }
  .result-panel { display: none; margin-top: 20px; padding: var(--card-pad); }
  .result-panel > .summary { max-width: 760px; margin-top: 10px; color: var(--muted); }
  .quality-warning { margin-top: 16px; padding: 14px 16px; border-left: 4px solid #d97706; border-radius: 8px; background: #fff3d6; color: #7c4206; font-weight: 750; }
  .quality-warning-inline { color: #8a4b08; font-weight: 750; }
  .coverage-card { margin-top: 22px; padding: clamp(18px, 3vw, 24px); border: 1px solid var(--brand-line); border-radius: var(--radius-lg); background: var(--brand-soft); }
  .coverage-card h3 { margin-top: 4px; }
  .coverage-card > p { max-width: 760px; margin-top: 8px; color: var(--muted-strong); }
  .coverage-card form, .coverage-card > .button { display: inline-flex; margin-top: 16px; margin-right: 8px; }
  .coverage-card [hidden] { display: none; }
  .metrics.coverage-metrics { grid-template-columns: repeat(4, minmax(0, 1fr)); margin-top: 18px; }
  .metrics { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 12px; margin-top: 24px; }
  .metric { padding: 17px; border: 1px solid var(--line); border-radius: var(--radius-md); background: var(--surface-subtle); color: var(--muted); font-size: .85rem; }
  .metric strong { display: block; margin-bottom: 3px; color: var(--ink-strong); font-size: 1.8rem; line-height: 1.1; }
  .classification { margin-top: 16px; color: var(--muted); font-size: .9rem; }
  .findings-heading { margin-top: 28px; margin-bottom: 12px; }
  .findings { display: grid; gap: 10px; }
  .findings li { padding: 14px 16px; border: 1px solid #ead9b7; border-radius: 12px; background: var(--accent-soft); }
  .finding-meta { margin-bottom: 3px; color: #925c08; font-size: .78rem; font-weight: 700; text-transform: capitalize; }
  .download-heading { margin-top: 28px; margin-bottom: 12px; }
  .download-actions { display: flex; flex-wrap: wrap; gap: 10px; }
  .secondary-actions { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 12px; padding-top: 12px; border-top: 1px solid var(--line); }

  .job-list, .user-list { display: grid; gap: 12px; }
  .history-groups { display: grid; gap: clamp(30px, 5vw, 48px); }
  .history-group { min-width: 0; }
  .history-group-header { display: flex; align-items: end; justify-content: space-between; gap: 18px; margin: 0 4px 14px; padding-bottom: 12px; border-bottom: 1px solid var(--line); }
  .history-group-header h2 { margin-top: 3px; font-size: clamp(1.25rem, 3vw, 1.65rem); }
  .history-group-label { color: var(--brand-strong); font-size: .72rem; font-weight: 800; letter-spacing: .1em; text-transform: uppercase; }
  .history-group-count { flex: 0 0 auto; border-radius: 999px; padding: 6px 10px; background: var(--surface-subtle); color: var(--muted-strong); font-size: .8rem; font-weight: 700; }
  .history-group-empty { display: flex; align-items: center; justify-content: space-between; gap: 16px; padding: 18px 20px; }
  .history-group-empty p { color: var(--muted); }
  .job-card { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 22px; align-items: center; padding: 20px 22px; }
  .job-title-row { display: flex; align-items: center; flex-wrap: wrap; gap: 9px; }
  .job-title { overflow-wrap: anywhere; color: var(--ink-strong); font-weight: 780; }
  .job-meta { display: flex; flex-wrap: wrap; gap: 6px 16px; margin-top: 8px; color: var(--muted); font-size: .88rem; }
  .job-timing { display: flex; flex-wrap: wrap; gap: 8px 22px; margin-top: 11px; color: var(--muted); font-size: .84rem; }
  .job-timing strong { margin-left: 4px; color: var(--ink-strong); font-size: .9rem; font-variant-numeric: tabular-nums; }
  .job-actions { display: flex; align-items: center; flex-wrap: wrap; justify-content: flex-end; gap: 8px; }
  .empty-state { padding: clamp(32px, 6vw, 64px); text-align: center; }
  .empty-state p { margin-top: 8px; color: var(--muted); }
  .empty-state .button { margin-top: 20px; }

  .users-layout { display: grid; grid-template-columns: minmax(280px, .72fr) minmax(0, 1.28fr); gap: 20px; align-items: start; }
  .create-user { position: sticky; top: 96px; padding: var(--card-pad); }
  .create-user h2 { margin-bottom: 20px; }
  .create-user .button { width: 100%; margin-top: 22px; }
  .user-card { padding: 20px 22px; }
  .user-header { display: flex; justify-content: space-between; gap: 16px; margin-bottom: 16px; }
  .user-header strong { color: var(--ink-strong); font-size: 1.05rem; }
  .user-header > span { color: var(--muted); font-size: .82rem; }
  .user-identity { display: flex; align-items: center; flex-wrap: wrap; gap: 8px; }
  .user-identity .badge { color: var(--brand-strong); font-size: .72rem; }
  .user-identity .role-admin { background: #e8f5f3; }
  .user-identity .role-editor { background: #eef1f5; color: var(--muted-strong); }
  .user-forms { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; align-items: end; }
  .user-forms form { display: grid; gap: 10px; }
  .compact-field { display: grid; gap: 6px; }
  .compact-field label { font-size: .8rem; }
  .delete-form { display: flex; grid-column: 1 / -1; }
  .delete-form .button { white-space: nowrap; }

  .login-page .site-header { position: static; border-bottom: 0; background: transparent; backdrop-filter: none; }
  .login-page .header-inner { justify-content: center; padding-top: 24px; }
  .login-page .page-main { display: grid; place-items: center; min-height: calc(100vh - 96px); padding-block: 32px 64px; }
  .login-card { width: min(100%, 460px); padding: clamp(24px, 6vw, 42px); box-shadow: var(--shadow-lg); }
  .login-card h1 { font-size: clamp(2rem, 8vw, 3rem); }
  .login-card > p { margin-top: 10px; color: var(--muted); }
  .login-form { margin-top: 26px; }
  .login-form .button { width: 100%; margin-top: 24px; }

  .log-overview, .research-claim-list { display: grid; gap: 16px; }
  .log-job-card, .research-claim-card { padding: 20px 22px; }
  .log-job-heading, .research-claim-header, .source-log-top { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; }
  .log-job-heading h2, .research-claim-header h2 { margin-top: 5px; overflow-wrap: anywhere; color: var(--ink-strong); font-size: 1.08rem; }
  .log-metrics { display: flex; flex-wrap: wrap; gap: 8px 20px; margin: 16px 0; color: var(--muted); }
  .log-metrics strong { color: var(--ink-strong); }
  .log-summary-metrics { margin-bottom: 18px; }
  .log-filter { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 18px; }
  .log-live-note, .log-error { margin: 14px 0; padding: 14px 16px; }
  .log-live-note { color: var(--brand-strong); }
  .log-error { border-left: 4px solid var(--danger); background: var(--danger-soft); color: var(--danger); }
  .research-claim-card.has-problem { border-left: 4px solid #d97706; }
  .claim-badges { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 6px; }
  .badge-warning { background: #fff3d6; color: #8a4b08; }
  .badge-success { background: #e8f5f3; color: #17675d; }
  .research-claim-card details { margin-top: 16px; }
  .research-claim-card summary { cursor: pointer; color: var(--ink-strong); font-weight: 760; }
  .query-list { display: grid; gap: 9px; margin: 12px 0 0; padding-left: 22px; }
  .query-list li { padding-left: 4px; }
  .query-list code { display: block; margin-top: 4px; overflow-wrap: anywhere; white-space: normal; }
  .query-purpose { margin-right: 8px; color: var(--muted); font-size: .82rem; text-transform: uppercase; }
  .source-log-list { display: grid; gap: 10px; margin-top: 12px; }
  .source-log-entry { padding: 14px; border: 1px solid var(--line); border-radius: 14px; background: var(--surface-subtle); overflow-wrap: anywhere; }
  .source-log-entry.is-rejected { border-left: 4px solid #d97706; }
  .source-log-entry.is-relevant { border-left: 4px solid #17806f; }
  .source-log-entry a { display: inline-block; margin: 8px 0; word-break: break-all; }
  .source-log-entry p { color: var(--muted); font-size: .88rem; }
  .source-log-entry .rejection-reasons { color: #8a4b08; }
  .pagination { display: flex; align-items: center; justify-content: center; gap: 14px; margin-top: 20px; }
}

@layer responsive {
  @media (max-width: 900px) {
    .hero, .users-layout, .status-grid { grid-template-columns: 1fr; }
    .metrics.coverage-metrics { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    .upload-panel, .create-user { position: static; }
    .intro-panel h1 { max-width: 650px; }
    .user-forms { grid-template-columns: 1fr 1fr; }
    .delete-form { grid-column: 1 / -1; }
  }

  @media (max-width: 700px) {
    :root { --gutter: 16px; --card-pad: 20px; --radius-lg: 20px; }
    body { line-height: 1.55; }
    .site-header { position: static; }
    .header-inner { min-height: auto; align-items: stretch; flex-direction: column; gap: 12px; padding-block: 14px; }
    .site-nav { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 6px; justify-content: stretch; width: 100%; overflow: visible; padding-bottom: 0; }
    .nav-link, .nav-button { width: 100%; padding-inline: 10px; }
    .nav-form { width: 100%; }
    .nav-user { display: none; }
    .page-main { padding-top: 24px; }
    .intro-panel { padding: 26px 22px; }
    .intro-panel h1 { font-size: clamp(2.35rem, 13vw, 3.5rem); }
    .intro-copy { margin-top: 18px; }
    .workflow { margin-top: 30px; }
    .status-top { gap: 16px; }
    .progress-value { font-size: 2.25rem; }
    .progress-meta { align-items: flex-start; flex-direction: column; gap: 2px; }
    .metrics, .metrics.coverage-metrics { grid-template-columns: 1fr; }
    .metric { display: flex; align-items: baseline; gap: 8px; }
    .metric strong { margin-bottom: 0; font-size: 1.4rem; }
    .download-actions, .secondary-actions { display: grid; grid-template-columns: 1fr; }
    .job-card { grid-template-columns: 1fr; gap: 16px; padding: 18px; }
    .history-group-header { align-items: flex-start; flex-direction: column; gap: 8px; }
    .history-group-empty { align-items: stretch; flex-direction: column; }
    .history-group-empty .button { width: 100%; }
    .job-actions { justify-content: flex-start; }
    .job-actions > a, .job-actions > form { flex: 1 1 180px; }
    .job-actions > form { display: flex; }
    .job-actions > form .button { width: 100%; }
    .log-job-heading, .research-claim-header, .source-log-top { flex-direction: column; }
    .claim-badges { justify-content: flex-start; }
    .log-filter .button, .pagination .button { flex: 1 1 150px; }
    .pagination { flex-wrap: wrap; }
    .user-forms { grid-template-columns: 1fr; }
    .delete-form { grid-column: auto; }
    .user-header { align-items: flex-start; flex-direction: column; gap: 3px; }
    .user-forms .button { width: 100%; }
  }

  @media (max-width: 420px) {
    .brand-mark { width: 36px; height: 36px; }
    .nav-link, .nav-button { font-size: .9rem; }
    .status-top { flex-direction: column; }
    .progress-value { order: -1; }
    .button { width: 100%; }
    .file-field input::file-selector-button { display: block; width: 100%; margin: 0 0 10px; }
  }

  @media (prefers-reduced-motion: reduce) {
    *, *::before, *::after { scroll-behavior: auto !important; transition-duration: .01ms !important; }
  }
}
