:root {
  --bg: #f6f7fb;
  --surface: #ffffff;
  --surface-2: #f1f3f9;
  --border: #e3e6ef;
  --text: #141824;
  --muted: #5d6478;
  --accent: #4f46e5;
  --accent-ink: #ffffff;
  --accent-soft: #eef0ff;
  --low: #15803d;
  --low-bg: #e8f7ee;
  --medium: #a16207;
  --medium-bg: #fdf5dc;
  --high: #c2410c;
  --high-bg: #fff0e6;
  --critical: #b91c1c;
  --critical-bg: #fde8e8;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(20, 24, 36, 0.04), 0 4px 16px rgba(20, 24, 36, 0.05);
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  --serif: "Iowan Old Style", "Charter", Georgia, serif;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1117;
    --surface: #161a23;
    --surface-2: #1d2230;
    --border: #2a3040;
    --text: #e8eaf1;
    --muted: #9aa1b5;
    --accent: #8b85ff;
    --accent-ink: #0e1117;
    --accent-soft: #23234a;
    --low: #4ade80;
    --low-bg: #12301f;
    --medium: #facc15;
    --medium-bg: #332a0c;
    --high: #fb923c;
    --high-bg: #3a2010;
    --critical: #f87171;
    --critical-bg: #3d1414;
    --shadow: none;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.55 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}
button, input, textarea { font: inherit; color: inherit; }
h1, h2, h3 { margin: 0; line-height: 1.25; }
h1 { font-size: clamp(24px, 4vw, 32px); letter-spacing: -0.02em; }
h2 { font-size: 16px; margin-bottom: 12px; }
h3 { font-size: 15px; }
.hidden { display: none !important; }
.muted { color: var(--muted); font-weight: 400; }
.small { font-size: 13px; }

.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 14px 24px; border-bottom: 1px solid var(--border); background: var(--surface);
  position: sticky; top: 0; z-index: 5;
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand-name { font-weight: 700; font-size: 18px; letter-spacing: -0.01em; }
.brand-tag { color: var(--muted); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.badge {
  font-size: 12px; padding: 4px 10px; border-radius: 999px; background: var(--surface-2);
  color: var(--muted); border: 1px solid var(--border); white-space: nowrap;
}
.badge.on { color: var(--accent); background: var(--accent-soft); border-color: transparent; }

main { max-width: 1240px; margin: 0 auto; padding: 28px 24px 40px; }
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 20px;
}

/* Input */
.input-card { max-width: 860px; margin: 0 auto; padding: 32px; }
.lede { color: var(--muted); margin: 10px 0 24px; max-width: 70ch; }
.tabs { display: flex; gap: 4px; align-items: center; margin-bottom: 10px; flex-wrap: wrap; }
.tab {
  background: none; border: 0; padding: 8px 12px; border-radius: 8px; cursor: pointer; color: var(--muted); font-weight: 500;
}
.tab.active { background: var(--surface-2); color: var(--text); }
.link-btn { margin-left: auto; background: none; border: 0; color: var(--accent); cursor: pointer; padding: 8px 4px; font-weight: 500; }
.link-btn:hover { text-decoration: underline; }
textarea {
  width: 100%; border: 1px solid var(--border); border-radius: 10px; background: var(--surface-2);
  padding: 14px; resize: vertical;
}
#contract-text { min-height: 260px; font: 13px/1.6 var(--mono); }
textarea:focus, .tab:focus-visible, button:focus-visible, .dropzone:focus-within { outline: 2px solid var(--accent); outline-offset: 2px; }
.char-count { text-align: right; font-size: 12px; color: var(--muted); margin-top: 4px; }
.dropzone {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 10px; min-height: 220px;
  border: 2px dashed var(--border); border-radius: 12px; cursor: pointer; color: var(--muted); text-align: center; padding: 20px;
  transition: border-color .15s, background .15s;
}
.dropzone:hover, .dropzone.drag { border-color: var(--accent); background: var(--accent-soft); color: var(--text); }
.dropzone.has-file { border-style: solid; border-color: var(--accent); color: var(--text); }

.options { display: grid; grid-template-columns: 1fr; gap: 16px; margin-top: 20px; }
fieldset { border: 0; padding: 0; margin: 0; }
legend { font-weight: 600; font-size: 13px; margin-bottom: 8px; }
.segmented { display: flex; flex-wrap: wrap; gap: 8px; }
.segmented label { cursor: pointer; }
.segmented input { position: absolute; opacity: 0; pointer-events: none; }
.segmented span {
  display: inline-block; padding: 8px 14px; border-radius: 999px; border: 1px solid var(--border);
  background: var(--surface); font-size: 14px; transition: all .12s;
}
.segmented label:hover span { border-color: var(--accent); }
.segmented input:checked + span { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.segmented input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }
.segmented.compact span { padding: 6px 12px; font-size: 13px; }
.more { margin-top: 18px; }
.more summary { cursor: pointer; color: var(--muted); font-size: 14px; }
.more textarea { margin-top: 8px; }
.actions { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-top: 24px; flex-wrap: wrap; }
.primary {
  background: var(--accent); color: var(--accent-ink); border: 0; border-radius: 10px; padding: 12px 24px;
  font-weight: 600; cursor: pointer; transition: transform .08s, opacity .15s;
}
.primary:hover { opacity: .92; }
.primary:active { transform: translateY(1px); }
.primary:disabled { opacity: .5; cursor: not-allowed; }
.ghost {
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 7px 12px;
  cursor: pointer; font-size: 14px;
}
.ghost:hover { border-color: var(--accent); }
.error { color: var(--critical); background: var(--critical-bg); padding: 10px 12px; border-radius: 8px; margin: 16px 0 0; }

/* Loading */
.loading { max-width: 520px; margin: 60px auto; text-align: center; padding: 40px; }
.scanner {
  width: 120px; height: 150px; margin: 0 auto 20px; border: 1px solid var(--border); border-radius: 8px; position: relative; overflow: hidden;
  background: repeating-linear-gradient(to bottom, transparent 0 14px, var(--border) 14px 15px), var(--surface-2);
}
.scan-line {
  position: absolute; left: 0; right: 0; height: 3px; background: var(--accent); box-shadow: 0 0 14px var(--accent);
  animation: scan 1.6s ease-in-out infinite alternate;
}
@keyframes scan { from { top: 4%; } to { top: 94%; } }
@media (prefers-reduced-motion: reduce) { .scan-line { animation: none; top: 50%; } }

/* Results */
.results-toolbar { display: flex; justify-content: space-between; gap: 8px; margin-bottom: 16px; flex-wrap: wrap; }
.toolbar-right { display: flex; gap: 8px; flex-wrap: wrap; }
.notice { background: var(--medium-bg); color: var(--text); border-radius: 10px; padding: 10px 14px; margin: 0 0 16px; font-size: 14px; }

.overview { display: flex; gap: 24px; align-items: center; margin-bottom: 16px; }
.gauge { position: relative; width: 120px; height: 120px; flex: 0 0 auto; }
.gauge svg { width: 100%; height: 100%; transform: rotate(-90deg); }
.gauge circle { fill: none; stroke-width: 10; }
.gauge-track { stroke: var(--surface-2); }
.gauge-fill { stroke: var(--accent); stroke-linecap: round; stroke-dasharray: 326.7; stroke-dashoffset: 326.7; transition: stroke-dashoffset .8s ease, stroke .3s; }
.gauge-label { position: absolute; inset: 0; display: flex; align-items: baseline; justify-content: center; padding-top: 40px; }
.gauge-label span { font-size: 32px; font-weight: 700; letter-spacing: -0.03em; }
.gauge-label small { color: var(--muted); }
.overview-body { min-width: 0; flex: 1; }
.overview-head { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-bottom: 6px; }
.contract-type { font-weight: 600; }
.overview p { margin: 6px 0 10px; }
.parties { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 10px; }
.parties span { font-size: 13px; background: var(--surface-2); border-radius: 6px; padding: 2px 8px; }
.severity-counts { display: flex; flex-wrap: wrap; gap: 8px; }

.pill {
  display: inline-flex; align-items: center; gap: 6px; font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: .04em;
  padding: 3px 9px; border-radius: 999px; white-space: nowrap;
}
.pill::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.pill.low { color: var(--low); background: var(--low-bg); }
.pill.medium { color: var(--medium); background: var(--medium-bg); }
.pill.high { color: var(--high); background: var(--high-bg); }
.pill.critical { color: var(--critical); background: var(--critical-bg); }

.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; margin-bottom: 24px; }
.priorities { margin: 0; padding-left: 22px; }
.priorities li { margin: 6px 0; }
.priorities li::marker { color: var(--accent); font-weight: 700; }
.key-dates { list-style: none; margin: 0; padding: 0; }
.key-dates li { padding: 8px 0; border-top: 1px solid var(--border); font-size: 14px; }
.key-dates li:first-child { border-top: 0; padding-top: 0; }
.key-dates strong { display: block; font-size: 13px; }
.empty { color: var(--muted); font-size: 14px; }

.workspace { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 20px; align-items: start; }
.col-head { display: flex; flex-direction: column; gap: 8px; margin-bottom: 12px; }
.col-head h2 { margin: 0; }
.filters { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  font-size: 13px; border: 1px solid var(--border); background: var(--surface); border-radius: 999px; padding: 4px 10px; cursor: pointer;
}
.chip.active { background: var(--text); color: var(--bg); border-color: var(--text); }
.chip .n { opacity: .65; margin-left: 4px; }

.clause {
  background: var(--surface); border: 1px solid var(--border); border-left: 4px solid var(--border);
  border-radius: 12px; padding: 16px; margin-bottom: 12px; cursor: pointer; transition: box-shadow .15s, border-color .15s;
}
.clause:hover { box-shadow: var(--shadow); }
.clause.low { border-left-color: var(--low); }
.clause.medium { border-left-color: var(--medium); }
.clause.high { border-left-color: var(--high); }
.clause.critical { border-left-color: var(--critical); }
.clause.selected { outline: 2px solid var(--accent); outline-offset: 1px; }
.clause header { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; margin-bottom: 8px; }
.clause header h3 { flex-basis: 100%; }
.cat { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; }
.quote {
  margin: 8px 0; padding: 8px 12px; border-left: 3px solid var(--border); background: var(--surface-2);
  font: 14px/1.55 var(--serif); color: var(--muted); border-radius: 0 6px 6px 0;
}
.plain { margin: 8px 0; font-weight: 500; }
.why, .suggest, .redline { font-size: 14px; margin-top: 10px; }
.why strong, .suggest strong, .redline strong { font-size: 12px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); }
.why p, .suggest p, .redline p { margin: 2px 0 0; }
.redline { background: var(--accent-soft); border-radius: 8px; padding: 10px 12px; }
.redline p { font-family: var(--serif); }
.redline-head { display: flex; justify-content: space-between; align-items: center; }
.copy-btn { font-size: 12px; background: var(--surface); border: 1px solid var(--border); border-radius: 6px; padding: 2px 8px; cursor: pointer; }

.missing {
  border: 1px dashed var(--border); border-radius: 12px; padding: 14px 16px; margin-bottom: 10px; background: var(--surface);
}
.missing h3 { margin-bottom: 4px; }
.missing p { margin: 4px 0; font-size: 14px; }
.missing .lang { font-family: var(--serif); background: var(--surface-2); padding: 8px 10px; border-radius: 6px; }
.section-gap { margin-top: 28px; }

.doc-col { position: sticky; top: 76px; }
.doc-view {
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 24px 28px;
  max-height: calc(100vh - 140px); overflow: auto; white-space: pre-wrap; font: 14px/1.7 var(--serif); word-wrap: break-word;
}
mark.hl { background: var(--medium-bg); color: inherit; border-radius: 3px; padding: 1px 0; cursor: pointer; border-bottom: 2px solid var(--medium); }
mark.hl.low { background: var(--low-bg); border-color: var(--low); }
mark.hl.high { background: var(--high-bg); border-color: var(--high); }
mark.hl.critical { background: var(--critical-bg); border-color: var(--critical); }
mark.hl.active { outline: 2px solid var(--accent); outline-offset: 2px; }
.meta { margin-top: 24px; text-align: right; }

.disclaimer { max-width: 1240px; margin: 0 auto; padding: 0 24px 32px; color: var(--muted); font-size: 12px; text-align: center; }

.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--text); color: var(--bg);
  padding: 8px 16px; border-radius: 8px; font-size: 14px; z-index: 20;
}

@media (min-width: 720px) { .options { grid-template-columns: 1fr 1fr; } }
@media (max-width: 960px) {
  .workspace { grid-template-columns: 1fr; }
  .doc-col { position: static; }
  .doc-view { max-height: 60vh; }
}
@media (max-width: 640px) {
  main { padding: 16px 16px 32px; }
  .topbar { padding: 12px 16px; }
  .brand-tag { display: none; }
  .input-card { padding: 20px; }
  .grid-2 { grid-template-columns: 1fr; }
  .overview { flex-direction: column; align-items: flex-start; }
  .link-btn { margin-left: 0; }
}
@media print {
  .topbar, .results-toolbar, #input-view, .filters, .copy-btn, .doc-col { display: none !important; }
  body { background: #fff; }
  .workspace { grid-template-columns: 1fr; }
  .clause { break-inside: avoid; }
}

.clause .details { display: none; }
.clause.open .details { display: block; }
.clause.open .expand { display: none; }
.expand { background: none; border: 0; padding: 4px 0 0; color: var(--accent); font-size: 13px; font-weight: 600; cursor: pointer; }
.clause .plain { margin: 4px 0; }
@media print { .clause .details { display: block; } .expand { display: none; } }

.apikey-details .apikey { margin-top: 10px; }
.apikey { margin-top: 20px; padding: 16px; border: 1px solid var(--border); border-radius: 12px; background: var(--surface-2); }
.apikey-label { display: block; font-weight: 600; font-size: 13px; margin-bottom: 8px; }
.apikey-row { display: flex; gap: 8px; }
.apikey-row input {
  flex: 1; min-width: 0; border: 1px solid var(--border); border-radius: 8px; background: var(--surface);
  padding: 9px 12px; font: 14px var(--mono);
}
.apikey-row input:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
.apikey-row input.invalid { border-color: var(--critical); }
.apikey-meta { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-top: 8px; font-size: 13px; flex-wrap: wrap; }
.apikey-meta a { color: var(--accent); font-weight: 500; }
.check { display: inline-flex; align-items: center; gap: 6px; cursor: pointer; }
.apikey-note { margin: 8px 0 0; }

/* Paid scan packs */
.buy { margin-top: 16px; padding: 14px 16px; border: 1px solid var(--border); border-radius: 12px; background: var(--surface-2); }
.buy p { margin: 0; }
.buy p + p { margin-top: 6px; }
.buy .small-btn { padding: 6px 14px; font-size: 14px; margin-left: 8px; }
#pack-link { word-break: break-all; }
