:root {
  --brand-dark: #0A1C40;
  --brand-deep: #071430;
  --brand: #1352B4;
  --brand-strong: #0E3F8E;
  --brand-light: #E8F0FE;
  --surface: #FFFFFF;
  --surface-2: #F4F6F9;
  --surface-3: #ECF0F5;
  --text: #1A202C;
  --text-muted: #54627B;
  --text-on-brand: #FFFFFF;
  --border: #E2E8F0;
  --border-strong: #CBD5E1;
  --success: #10B981;
  --warning: #F59E0B;
  --warning-soft: #FEF6E4;
  --warning-ink: #6B4A05;
  --danger: #EF4444;
  --danger-soft: #FDECEC;
  --danger-ink: #8A1C1C;
  --focus: #1352B4;
  --shadow-1: 0 1px 2px rgba(10, 28, 64, .06);
  --shadow-2: 0 6px 24px rgba(10, 28, 64, .12);
  --shadow-3: -8px 0 32px rgba(10, 28, 64, .16);
  --r-sm: 4px;
  --r-md: 8px;
  --r-lg: 14px;
  --r-pill: 999px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --mono: ui-monospace, "SF Mono", "Cascadia Mono", Consolas, monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --brand-dark: #E8EEF9;
    --brand-deep: #0A1425;
    --brand: #6098F0;
    --brand-strong: #7FADF6;
    --brand-light: #16294A;
    --surface: #131C2C;
    --surface-2: #0C1220;
    --surface-3: #1B2637;
    --text: #EEF2F8;
    --text-muted: #A2B0C6;
    --text-on-brand: #07152E;
    --border: #28344A;
    --border-strong: #3A4964;
    --success: #34D399;
    --warning: #FBBF24;
    --warning-soft: #2A2109;
    --warning-ink: #FBE3AE;
    --danger: #F87171;
    --danger-soft: #331618;
    --danger-ink: #FBC9C9;
    --focus: #8FB6F7;
    --shadow-1: 0 1px 2px rgba(0, 0, 0, .4);
    --shadow-2: 0 6px 24px rgba(0, 0, 0, .5);
    --shadow-3: -8px 0 32px rgba(0, 0, 0, .55);
  }
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html { -webkit-text-size-adjust: 100%; }

body {
  font-family: var(--font);
  background: var(--surface-2);
  color: var(--text);
  font-size: 16px;
  line-height: 1.5;
  text-wrap: pretty;
  min-height: 100dvh;
}

a { color: var(--brand); text-decoration: underline; text-underline-offset: 2px; }
a:hover { color: var(--brand-strong); }

:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
  border-radius: var(--r-sm);
}

.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

[hidden] { display: none !important; }

.wordmark {
  font-weight: 800;
  letter-spacing: .12em;
  font-size: .9rem;
  color: var(--brand-dark);
  display: flex;
  align-items: baseline;
  gap: .45em;
  flex-wrap: wrap;
}
.wordmark .amp { color: var(--brand); }
.wordmark-sub {
  font-weight: 600;
  font-size: .62rem;
  letter-spacing: .3em;
  color: var(--text-muted);
}
.wordmark-sm { font-size: .72rem; letter-spacing: .14em; }

.btn {
  font: inherit;
  font-weight: 600;
  font-size: .95rem;
  border: 1px solid transparent;
  border-radius: var(--r-md);
  padding: 12px 18px;
  min-height: 44px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  cursor: pointer;
  background: none;
  color: var(--text);
  transition: background-color .15s ease, border-color .15s ease, box-shadow .15s ease;
}
.btn-primary { background: var(--brand); color: var(--text-on-brand); }
.btn-primary:hover:not(:disabled) { background: var(--brand-strong); }
.btn-quiet {
  background: var(--surface);
  border-color: var(--border-strong);
  color: var(--text);
}
.btn-quiet:hover:not(:disabled) { background: var(--surface-3); }
.btn-block { width: 100%; }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn-icon {
  min-height: 36px;
  width: 36px;
  padding: 0;
  border-radius: var(--r-md);
  color: var(--text-muted);
}
.btn-icon:hover { background: var(--surface-3); color: var(--text); }

.field { display: flex; flex-direction: column; gap: 6px; }
.field label {
  font-size: .72rem;
  font-weight: 700;
  letter-spacing: .09em;
  text-transform: uppercase;
  color: var(--text-muted);
}
.field label abbr { color: var(--brand); text-decoration: none; }
.field input, .field select {
  font: inherit;
  font-size: 1rem;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--r-md);
  padding: 11px 12px;
  min-height: 44px;
  width: 100%;
}
.field input::placeholder { color: var(--text-muted); opacity: .75; }
.field input:focus-visible, .field select:focus-visible { border-color: var(--brand); }
.field-err { font-size: .82rem; color: var(--danger-ink); }
.field-row { display: grid; gap: 14px; grid-template-columns: 1fr 1fr; }

.screen { min-height: 100dvh; }
#screen-entry { padding: 28px 18px 40px; }
.entry-wrap { max-width: 1000px; margin: 0 auto; display: flex; flex-direction: column; gap: 26px; }
.entry-head { display: flex; flex-direction: column; gap: 12px; max-width: 62ch; }
.entry-head h1 {
  font-size: 1.6rem;
  line-height: 1.22;
  font-weight: 700;
  letter-spacing: -.01em;
  color: var(--brand-dark);
}
.entry-lead { color: var(--text-muted); font-size: 1rem; }

.entry-grid { display: grid; gap: 18px; }
.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow-1);
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.panel-title {
  font-size: 1.02rem;
  font-weight: 700;
  color: var(--brand-dark);
}
.panel-note { font-size: .86rem; color: var(--text-muted); margin-bottom: 10px; }
.panel form { display: flex; flex-direction: column; gap: 14px; }

.case-list { list-style: none; display: flex; flex-direction: column; gap: 8px; }
.case-card {
  font: inherit;
  width: 100%;
  text-align: left;
  cursor: pointer;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-left: 3px solid var(--border-strong);
  border-radius: var(--r-md);
  padding: 12px 14px;
  display: grid;
  gap: 3px;
  color: var(--text);
  transition: border-color .15s ease, background-color .15s ease;
}
.case-card:hover { background: var(--brand-light); border-left-color: var(--brand); }
.case-contest { font-weight: 650; font-size: .95rem; color: var(--brand-dark); }
.case-meta {
  display: flex;
  gap: 10px;
  align-items: baseline;
  font-size: .74rem;
  letter-spacing: .06em;
  text-transform: uppercase;
  color: var(--text-muted);
  font-weight: 650;
}
.case-score { color: var(--brand); }
.case-note { font-size: .86rem; color: var(--text-muted); }

.entry-foot { border-top: 1px solid var(--border); padding-top: 18px; }
.token-form {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 10px;
  align-items: end;
  max-width: 460px;
}
.field-token input { font-family: var(--mono); font-size: .9rem; }
.token-state { grid-column: 1 / -1; font-size: .8rem; color: var(--text-muted); }

#screen-chat {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  min-height: 0;
  background: var(--surface-2);
}
.chat-head {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 10px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-shrink: 0;
}
.chat-head-id { display: flex; flex-direction: column; gap: 3px; min-width: 0; }
.assistant-name {
  font-size: 1rem;
  font-weight: 650;
  color: var(--text);
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}
.activity { display: inline-flex; align-items: center; gap: 6px; }
.activity-dot {
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--success);
  box-shadow: 0 0 0 3px color-mix(in oklch, var(--success) 22%, transparent);
}
.activity-label {
  font-size: .72rem;
  font-weight: 650;
  letter-spacing: .07em;
  text-transform: uppercase;
  color: var(--text-muted);
}
.activity[data-state="busy"] .activity-dot { background: var(--brand); animation: pulse 1.4s ease-in-out infinite; box-shadow: none; }
.activity[data-state="closed"] .activity-dot { background: var(--border-strong); box-shadow: none; }
@keyframes pulse { 0%, 100% { opacity: 1; transform: scale(1); } 50% { opacity: .35; transform: scale(.8); } }

.btn-panel { padding: 8px 12px; min-height: 38px; font-size: .84rem; flex-shrink: 0; }
.btn-panel .btn-panel-label { display: none; }

.disclaimer {
  background: var(--warning-soft);
  color: var(--warning-ink);
  border-bottom: 1px solid var(--border);
  border-top: 2px solid var(--warning);
  padding: 8px 16px;
  font-size: .8rem;
  line-height: 1.35;
  text-align: center;
  flex-shrink: 0;
}

#chat-container {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 20px 16px 8px;
  scroll-behavior: smooth;
}
#messages { display: flex; flex-direction: column; gap: 18px; max-width: 760px; margin: 0 auto; }

.msg { display: flex; flex-direction: column; gap: 5px; max-width: 86%; }
.msg-user { align-self: flex-end; align-items: flex-end; }
.msg-bot { align-self: flex-start; align-items: flex-start; }
.msg-system { align-self: stretch; max-width: 100%; align-items: center; }

.bubble {
  padding: 12px 15px;
  border-radius: var(--r-lg);
  font-size: .97rem;
  line-height: 1.5;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.msg-user .bubble {
  background: var(--brand);
  color: var(--text-on-brand);
  border-bottom-right-radius: var(--r-sm);
}
.msg-bot .bubble {
  background: var(--surface);
  border: 1px solid var(--border);
  border-bottom-left-radius: var(--r-sm);
  box-shadow: var(--shadow-1);
}
.msg-time {
  font-size: .68rem;
  letter-spacing: .06em;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}

.sources { margin-top: 3px; width: 100%; max-width: 480px; }
.sources-label {
  font-size: .66rem;
  font-weight: 700;
  letter-spacing: .13em;
  text-transform: uppercase;
  color: var(--text-muted);
  margin-bottom: 6px;
}
.sources-list { list-style: none; display: flex; flex-direction: column; gap: 6px; }
.source {
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 2px solid var(--border-strong);
  border-radius: var(--r-sm);
}
.source-head {
  font: inherit;
  width: 100%;
  cursor: pointer;
  background: none;
  border: 0;
  color: var(--text);
  text-align: left;
  padding: 9px 11px;
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 2px 8px;
}
.source-name {
  grid-column: 1;
  font-size: .68rem;
  font-weight: 700;
  letter-spacing: .09em;
  text-transform: uppercase;
  color: var(--text-muted);
}
.source-title { grid-column: 1; font-size: .88rem; line-height: 1.35; }
.source-chev { grid-column: 2; grid-row: 1 / span 2; align-self: center; color: var(--text-muted); transition: transform .2s ease; }
.source[data-open="true"] { border-left-color: var(--brand); }
.source[data-open="true"] .source-chev { transform: rotate(180deg); }
.source-body { padding: 0 11px 11px; display: flex; flex-direction: column; gap: 8px; }
.source-snippet {
  font-size: .86rem;
  color: var(--text-muted);
  line-height: 1.5;
  border-left: 2px solid var(--border);
  padding-left: 10px;
}
.source-link { font-size: .84rem; font-weight: 600; }

.typing { max-width: 760px; margin: 14px auto 0; display: flex; }
.typing-inner {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  border-bottom-left-radius: var(--r-sm);
  padding: 12px 15px;
  min-height: 44px;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: .9rem;
  color: var(--text-muted);
  box-shadow: var(--shadow-1);
  transition: opacity .28s ease, transform .28s ease;
}
.typing-inner[data-fade="out"] { opacity: 0; transform: translateY(-3px); }
.typing-dots { display: inline-flex; gap: 4px; align-items: center; }
.typing-dots i {
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--text-muted);
  animation: bounce 1.2s ease-in-out infinite;
}
.typing-dots i:nth-child(2) { animation-delay: .18s; }
.typing-dots i:nth-child(3) { animation-delay: .36s; }
@keyframes bounce { 0%, 60%, 100% { opacity: .3; transform: translateY(0); } 30% { opacity: 1; transform: translateY(-3px); } }
@media (prefers-reduced-motion: reduce) {
  .typing-dots i, .activity[data-state="busy"] .activity-dot { animation: none; }
  #chat-container { scroll-behavior: auto; }
}

.notice {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  padding: 13px 15px;
  max-width: 560px;
  display: flex;
  flex-direction: column;
  gap: 5px;
  align-items: flex-start;
  box-shadow: var(--shadow-1);
}
.notice-title { font-size: .92rem; font-weight: 700; color: var(--text); }
.notice-text { font-size: .88rem; color: var(--text-muted); }
.notice .btn-retry { margin-top: 6px; min-height: 40px; padding: 9px 14px; font-size: .86rem; }
.notice-error { background: var(--danger-soft); border-color: var(--danger); }
.notice-error .notice-title { color: var(--danger-ink); }
.notice-error .notice-text { color: var(--danger-ink); }

.file-bubble {
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--r-lg);
  border-bottom-right-radius: var(--r-sm);
  padding: 11px 14px;
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 230px;
}
.file-icon {
  flex-shrink: 0;
  width: 34px; height: 40px;
  border: 1px solid var(--border-strong);
  border-radius: 3px;
  background: var(--surface-2);
  display: grid;
  place-items: center;
  font-size: .58rem;
  font-weight: 800;
  letter-spacing: .04em;
  color: var(--brand);
}
.file-info { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.file-name { font-size: .92rem; font-weight: 600; overflow-wrap: anywhere; }
.file-size { font-size: .76rem; color: var(--text-muted); font-variant-numeric: tabular-nums; }
.file-progress {
  width: 100%;
  height: 3px;
  background: var(--border);
  border-radius: var(--r-pill);
  overflow: hidden;
}
.file-progress-bar { display: block; height: 100%; width: 0; background: var(--brand); transition: width .2s ease; }

#input-area {
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
  flex-shrink: 0;
}
#chat-form { display: flex; gap: 8px; align-items: center; max-width: 760px; margin: 0 auto; }
#message-input {
  flex: 1;
  min-width: 0;
  font: inherit;
  font-size: 1rem;
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border-strong);
  border-radius: var(--r-pill);
  padding: 12px 16px;
  min-height: 46px;
}
#message-input::placeholder { color: var(--text-muted); opacity: .8; }
#message-input:focus-visible { border-color: var(--brand); background: var(--surface); }
#message-input:disabled { opacity: .6; }
.btn-attach {
  width: 46px;
  height: 46px;
  min-height: 46px;
  padding: 0;
  flex-shrink: 0;
  border: 1px solid var(--border-strong);
  border-radius: 50%;
  color: var(--text-muted);
  background: var(--surface);
}
.btn-attach:hover { background: var(--surface-3); color: var(--text); }
.btn-attach-label { display: none; }
.btn-attach[data-wanted="true"] {
  width: auto;
  padding: 0 16px;
  border-radius: var(--r-pill);
  background: var(--brand-light);
  border-color: var(--brand);
  color: var(--brand-strong);
}
.btn-attach[data-wanted="true"] .btn-attach-label {
  display: inline;
  font-size: .86rem;
  font-weight: 650;
}
.btn-attach[aria-disabled="true"] { opacity: .55; pointer-events: none; }
.btn-send { border-radius: var(--r-pill); padding: 0 18px; min-height: 46px; flex-shrink: 0; }
.btn-send .btn-send-label { display: none; }
.attach-hint {
  max-width: 760px;
  margin: 8px auto 0;
  font-size: .8rem;
  color: var(--brand-strong);
  text-align: center;
}

.closing {
  max-width: 560px;
  margin: 0 auto;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-top: 2px solid var(--brand);
  border-radius: var(--r-md);
  padding: 16px;
  text-align: center;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.closing[data-kind="discarded"] { border-top-color: var(--border-strong); }
.closing-title { font-size: .95rem; font-weight: 700; color: var(--brand-dark); }
.closing-text { font-size: .88rem; color: var(--text-muted); }

.panel-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(10, 28, 64, .45);
  border: 0;
  z-index: 40;
}
.data-panel {
  position: fixed;
  z-index: 50;
  left: 0; right: 0; bottom: 0;
  max-height: 84dvh;
  background: var(--surface);
  border-top: 1px solid var(--border);
  border-radius: var(--r-lg) var(--r-lg) 0 0;
  box-shadow: var(--shadow-2);
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.data-panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 16px 10px;
  border-bottom: 1px solid var(--border);
}
.data-panel-head h2 { font-size: .95rem; font-weight: 700; color: var(--brand-dark); }
.tabs { display: flex; gap: 2px; padding: 8px 12px 0; border-bottom: 1px solid var(--border); }
.tab {
  font: inherit;
  font-size: .82rem;
  font-weight: 650;
  letter-spacing: .04em;
  cursor: pointer;
  background: none;
  border: 0;
  border-bottom: 2px solid transparent;
  color: var(--text-muted);
  padding: 9px 12px;
  min-height: 40px;
}
.tab:hover { color: var(--text); }
.tab[aria-selected="true"] { color: var(--brand); border-bottom-color: var(--brand); }
.pane { overflow-y: auto; padding: 14px 16px 18px; flex: 1; min-height: 0; }

.fields { display: flex; flex-direction: column; }
.field-row-item {
  display: grid;
  grid-template-columns: 42% 1fr;
  gap: 10px;
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
  align-items: baseline;
}
.field-row-item dt {
  font-size: .72rem;
  font-weight: 700;
  letter-spacing: .07em;
  text-transform: uppercase;
  color: var(--text-muted);
}
.field-row-item dd { font-size: .9rem; overflow-wrap: anywhere; }
.field-row-item dd[data-empty="true"] {
  color: var(--text-muted);
  font-style: italic;
}
.field-row-item dd .pending {
  display: inline-block;
  font-style: normal;
  font-size: .68rem;
  font-weight: 700;
  letter-spacing: .08em;
  text-transform: uppercase;
  color: var(--warning-ink);
  background: var(--warning-soft);
  border: 1px solid var(--warning);
  border-radius: var(--r-sm);
  padding: 1px 6px;
}

.log { list-style: none; display: flex; flex-direction: column; gap: 2px; }
.log-row {
  display: grid;
  grid-template-columns: 58px 1fr;
  gap: 4px 10px;
  padding: 7px 0;
  border-bottom: 1px solid var(--border);
  font-size: .86rem;
}
.log-row time {
  font-variant-numeric: tabular-nums;
  font-size: .74rem;
  color: var(--text-muted);
  font-family: var(--mono);
}
.log-who {
  grid-column: 2;
  font-size: .68rem;
  font-weight: 700;
  letter-spacing: .09em;
  text-transform: uppercase;
  color: var(--brand);
}
.log-text { grid-column: 2; color: var(--text); overflow-wrap: anywhere; }
.log-mono .log-text { font-family: var(--mono); font-size: .8rem; color: var(--text-muted); }
.log-empty { font-size: .86rem; color: var(--text-muted); font-style: italic; padding: 6px 0; }

.data-panel-foot { border-top: 1px solid var(--border); padding: 10px 16px calc(10px + env(safe-area-inset-bottom)); }
.session-ref { font-family: var(--mono); font-size: .72rem; color: var(--text-muted); overflow-wrap: anywhere; }

@media (min-width: 680px) {
  #screen-entry { padding: 52px 28px 60px; }
  .entry-head h1 { font-size: 2.05rem; }
  .entry-grid { grid-template-columns: 1fr 1fr; align-items: start; }
  .panel { padding: 24px; }
  .disclaimer { font-size: .84rem; }
  .btn-send .btn-send-label { display: inline; }
  .btn-panel .btn-panel-label { display: inline; }
  .msg { max-width: 74%; }
  #chat-container { padding: 24px 24px 8px; }
  #input-area { padding: 14px 24px calc(14px + env(safe-area-inset-bottom)); }
}

@media (min-width: 900px) {
  .data-panel {
    top: 0; bottom: 0; left: auto; right: 0;
    width: 400px;
    max-height: none;
    border-radius: 0;
    border-top: 0;
    border-left: 1px solid var(--border);
    box-shadow: var(--shadow-3);
  }
  .panel-backdrop { background: rgba(10, 28, 64, .28); }
}
