/* 레이아웃: 넓은 화면은 왼쪽 사이드바(파라미터) + 본문(실행·히스토리), 좁은 화면(휴대폰)은 사이드바가 서랍처럼 열린다.
   색: 솔레니 = 청록, 가상 환자 = 남보라, 오류 = 빨강(문제)·주황(주의). 화면 전체에서 같은 뜻으로 쓴다. */
:root {
  --bg: #f4f6f7;
  --surface: #ffffff;
  --sunken: #eceff1;
  --ink: #15212a;
  --muted: #5d6b76;
  --line: #d6dde2;
  --accent: #2d4fc4;
  --soleni: #0b7c71;
  --soleni-weak: #e1f2ef;
  --patient: #4b47c2;
  --patient-weak: #e9e8fb;
  --bad: #c0362c;
  --bad-weak: #fbe9e7;
  --warn: #a35f00;
  --warn-weak: #fdf1dc;
  --ok: #1f7a3d;
  --radius: 12px;
  --font: -apple-system, BlinkMacSystemFont, "Apple SD Gothic Neo", "Pretendard", "Malgun Gothic", "Noto Sans KR", system-ui, sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0e1418; --surface: #151d23; --sunken: #1c262d; --ink: #e3eaee; --muted: #93a3ae; --line: #2a363e;
    --accent: #8fa7ff; --soleni: #45c3b1; --soleni-weak: #12302c; --patient: #a29dff; --patient-weak: #23214a;
    --bad: #ff7d70; --bad-weak: #3a1a17; --warn: #f0a94a; --warn-weak: #34260f; --ok: #5cc77d;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0e1418; --surface: #151d23; --sunken: #1c262d; --ink: #e3eaee; --muted: #93a3ae; --line: #2a363e;
  --accent: #8fa7ff; --soleni: #45c3b1; --soleni-weak: #12302c; --patient: #a29dff; --patient-weak: #23214a;
  --bad: #ff7d70; --bad-weak: #3a1a17; --warn: #f0a94a; --warn-weak: #34260f; --ok: #5cc77d;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body { background: var(--bg); color: var(--ink); font-family: var(--font); font-size: 15px; line-height: 1.55;
       padding-top: env(safe-area-inset-top, 0px); -webkit-text-size-adjust: 100%; }
button, input, select, textarea { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.error { color: var(--bad); }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
h2, h3 { margin: 0; text-wrap: balance; }
h2 { font-size: 1.25rem; }
h3 { font-size: 1rem; }
[hidden] { display: none !important; }

/* 버튼 */
button { cursor: pointer; }
.primary { background: var(--accent); color: #fff; border: 0; border-radius: 10px; padding: 9px 16px; font-weight: 600; }
.primary:disabled { opacity: .5; cursor: default; }
.primary.big { padding: 12px 22px; font-size: 1.05rem; min-width: 140px; }
.primary.stop { background: var(--bad); }
.ghost { background: transparent; border: 1px solid var(--line); border-radius: 8px; padding: 6px 12px; }
.danger { background: transparent; border: 1px solid var(--bad); color: var(--bad); border-radius: 8px; padding: 6px 12px; }
.icon-btn { background: var(--sunken); border: 0; border-radius: 8px; padding: 6px 12px; font-weight: 600; }

/* 상단 */
.topbar { position: sticky; top: env(safe-area-inset-top, 0px); z-index: 20; display: flex; align-items: center; gap: 12px;
          padding: 10px 16px; background: var(--surface); border-bottom: 1px solid var(--line); }
.brand { display: flex; align-items: baseline; gap: 8px; min-width: 0; flex: 1; }
.brand strong { white-space: nowrap; }
.brand span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tabs { display: flex; gap: 4px; background: var(--sunken); padding: 3px; border-radius: 10px; }
.tabs button { border: 0; background: transparent; padding: 6px 14px; border-radius: 8px; font-weight: 600; color: var(--muted); }
.tabs button[aria-selected="true"] { background: var(--surface); color: var(--ink); box-shadow: 0 1px 2px rgba(0,0,0,.08); }

/* 본문 배치 */
.layout { display: grid; grid-template-columns: 360px minmax(0, 1fr); min-height: calc(100vh - 56px); }
main { min-width: 0; padding: 18px 20px 48px; display: grid; align-content: start; }
main > section { display: grid; gap: 14px; min-width: 0; max-width: 980px; }

/* 사이드바 */
.sidebar { background: var(--surface); border-right: 1px solid var(--line); overflow-y: auto; height: calc(100vh - 56px);
           position: sticky; top: 56px; display: flex; flex-direction: column; }
.sidebar-head { display: flex; justify-content: space-between; align-items: center; padding: 12px 16px; border-bottom: 1px solid var(--line); }
#sidebar-body { flex: 1; }
.sidebar-foot { padding: 12px 16px 20px; border-top: 1px solid var(--line); }
.group { border-bottom: 1px solid var(--line); }
.group > summary { list-style: none; display: flex; align-items: center; gap: 8px; padding: 12px 16px; font-weight: 700; cursor: pointer; }
.group > summary::-webkit-details-marker { display: none; }
.group > summary::after { content: "▸"; margin-left: auto; color: var(--muted); transition: transform .15s; }
.group[open] > summary::after { transform: rotate(90deg); }
.when { font-size: 11px; font-weight: 600; padding: 1px 7px; border-radius: 999px; background: var(--sunken); color: var(--muted); white-space: nowrap; }
.when.live { background: var(--soleni-weak); color: var(--soleni); }
.group-body { padding: 0 16px 14px; display: grid; gap: 12px; }
.field { display: grid; gap: 5px; min-width: 0; }
.field > .label { display: flex; justify-content: space-between; gap: 8px; font-size: 13px; font-weight: 600; }
.field > .label .val { font-family: var(--mono); font-weight: 500; color: var(--muted); }
.field .help { font-size: 12px; color: var(--muted); line-height: 1.45; }
.field select, .field input[type="text"], .field textarea, .field input[type="password"] {
  width: 100%; border: 1px solid var(--line); background: var(--bg); border-radius: 8px; padding: 7px 9px; min-width: 0; }
.field textarea { resize: vertical; min-height: 64px; }
.field input[type="range"] { width: 100%; accent-color: var(--accent); }
.field .tag { font-size: 11px; color: var(--muted); font-weight: 500; }
.seg { display: flex; flex-wrap: wrap; gap: 4px; }
.seg button { flex: 1 1 auto; border: 1px solid var(--line); background: var(--bg); border-radius: 8px; padding: 5px 8px; font-size: 13px; min-width: 0; }
.seg button[aria-pressed="true"] { background: var(--ink); color: var(--surface); border-color: var(--ink); }
.toggle { display: flex; align-items: flex-start; gap: 10px; font-size: 14px; cursor: pointer; }
.toggle input { appearance: none; flex: 0 0 auto; width: 38px; height: 22px; border-radius: 999px; background: var(--line); position: relative; margin: 1px 0 0; cursor: pointer; transition: background .15s; }
.toggle input::after { content: ""; position: absolute; top: 3px; left: 3px; width: 16px; height: 16px; border-radius: 50%; background: #fff; transition: transform .15s; }
.toggle input:checked { background: var(--accent); }
.toggle input:checked::after { transform: translateX(16px); }
.toggle .t { display: grid; gap: 2px; }
.checks { display: flex; flex-wrap: wrap; gap: 6px 14px; font-size: 14px; }
.checks label { display: inline-flex; gap: 6px; align-items: center; }
.two { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

/* 페르소나 문항 */
.items { display: grid; gap: 8px; }
.item { display: grid; gap: 4px; padding: 8px; border-radius: 8px; background: var(--bg); }
.item .item-top { display: flex; justify-content: space-between; align-items: center; gap: 8px; font-size: 13px; font-weight: 600; }
.item .seg button { padding: 3px 6px; font-size: 12px; }
.item input { font-size: 13px !important; padding: 5px 8px !important; }
.total { display: flex; align-items: baseline; gap: 8px; padding: 8px 10px; border-radius: 8px; background: var(--patient-weak); color: var(--patient); font-weight: 700; }
.row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }

/* 실행: 상태 */
.status-card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; display: grid;
               grid-template-columns: minmax(0, 1fr) auto; gap: 14px; }
.status-main { display: grid; gap: 6px; align-content: start; min-width: 0; }
.status-side { display: grid; gap: 8px; justify-items: end; align-content: start; }
.state-pill { justify-self: start; font-size: 1.35rem; font-weight: 800; padding: 6px 16px; border-radius: 999px; background: var(--sunken); color: var(--muted); }
.state-hint { color: var(--muted); font-size: 14px; }
.clock { font-family: var(--mono); font-size: 1.4rem; font-variant-numeric: tabular-nums; }
.status-card[data-state="soleni"] .state-pill { background: var(--soleni-weak); color: var(--soleni); }
.status-card[data-state="patient"] .state-pill { background: var(--patient-weak); color: var(--patient); }
.status-card[data-state="thinking"] .state-pill, .status-card[data-state="connecting"] .state-pill { background: var(--warn-weak); color: var(--warn); }
.status-card[data-state="listening"] .state-pill { background: var(--soleni-weak); color: var(--soleni); opacity: .75; }
.meters { grid-column: 1 / -1; display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.meter { display: grid; gap: 4px; font-size: 12px; color: var(--muted); }
.meter .bar { height: 8px; border-radius: 999px; background: var(--sunken); overflow: hidden; }
.meter .bar i { display: block; height: 100%; width: 0; background: var(--soleni); transition: width .08s linear; }
.meter:nth-child(2) .bar i { background: var(--patient); }
.notice { grid-column: 1 / -1; padding: 8px 12px; border-radius: 8px; background: var(--warn-weak); color: var(--warn); font-size: 14px; }
/* 기다리는 시간 */
.waits { grid-column: 1 / -1; display: flex; flex-wrap: wrap; align-items: center; gap: 4px 10px; font-size: 14px; min-height: 26px; }
.waits .w-sec { font-family: var(--mono); font-size: 1.15rem; font-variant-numeric: tabular-nums; }
.waits .w-sec.over { color: var(--bad); }
.waits .w-bar { flex: 1 1 120px; height: 8px; border-radius: 999px; background: var(--sunken); overflow: hidden; }
.waits .w-bar i { display: block; height: 100%; background: var(--warn); }
/* 오류 알림: 카드 위에 겹쳐 띄워 화면이 밀리지 않게 */
.status-card { position: relative; }
.ev-toast { position: absolute; bottom: -16px; left: 50%; transform: translateX(-50%); z-index: 5; padding: 7px 14px; border-radius: 999px;
            font-weight: 700; white-space: nowrap; box-shadow: 0 4px 14px rgba(0, 0, 0, .15); }
.ev-toast.bad { background: var(--bad-weak); color: var(--bad); border: 1px solid var(--bad); }
.ev-toast.warn { background: var(--warn-weak); color: var(--warn); border: 1px solid var(--warn); }
@keyframes flash-bad { from { box-shadow: 0 0 0 4px var(--bad); } to { box-shadow: 0 0 0 0 transparent; } }
@keyframes flash-warn { from { box-shadow: 0 0 0 4px var(--warn); } to { box-shadow: 0 0 0 0 transparent; } }
.status-card.flash-bad { animation: flash-bad 1.2s ease-out; }
.status-card.flash-warn { animation: flash-warn 1.2s ease-out; }
@media (max-width: 560px) {
  .status-card { grid-template-columns: 1fr; }
  .status-side { grid-template-columns: auto 1fr; align-items: center; justify-items: stretch; }
  .clock { justify-self: start; }
  .primary.big { width: 100%; }
}

/* 오류 카운터 */
.counters { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { display: inline-flex; align-items: center; gap: 6px; padding: 4px 10px; border-radius: 999px; font-size: 13px; font-weight: 600;
        background: var(--sunken); color: var(--muted); border: 1px solid transparent; white-space: nowrap; }
.chip b { font-variant-numeric: tabular-nums; }
.chip.bad.on { background: var(--bad-weak); color: var(--bad); }
.chip.warn.on { background: var(--warn-weak); color: var(--warn); }
button.chip[aria-pressed="true"] { border-color: currentColor; }

/* 대화 */
.feed { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; display: grid; gap: 10px; min-height: 200px; align-content: start; }
.feed-empty ol { margin: 8px 0 0; padding-left: 20px; color: var(--muted); }
.feed-empty li + li { margin-top: 6px; }
.bubble { max-width: 82%; padding: 9px 12px; border-radius: 14px; display: grid; gap: 2px; min-width: 0; overflow-wrap: anywhere; }
.bubble .who { font-size: 11.5px; font-weight: 700; display: flex; gap: 8px; align-items: center; }
.bubble.soleni { justify-self: start; background: var(--soleni-weak); border-bottom-left-radius: 4px; }
.bubble.soleni .who { color: var(--soleni); }
.bubble.patient { justify-self: end; background: var(--patient-weak); border-bottom-right-radius: 4px; }
.bubble.patient .who { color: var(--patient); }
.bubble.live { opacity: .75; }
.bubble.live .text::after { content: " …"; }
.bubble.echo { opacity: .6; text-decoration: line-through; }
.bubble .cut { color: var(--muted); text-decoration: line-through; }
.bubble.discarded { opacity: .45; }
.bubble .seek, .ev .seek { border: 0; background: none; padding: 0; color: var(--accent); font-weight: 600; font-size: 12px; font-family: var(--mono); }
.ev { justify-self: center; display: flex; flex-wrap: wrap; align-items: baseline; gap: 6px 10px; font-size: 13px; padding: 6px 12px; border-radius: 10px;
      background: var(--sunken); border-left: 3px solid var(--muted); max-width: 100%; }
.ev.bad { background: var(--bad-weak); border-left-color: var(--bad); }
.ev.warn { background: var(--warn-weak); border-left-color: var(--warn); }
.ev .ev-label { font-weight: 700; }
.ev .ev-action { color: var(--muted); }
.ev .ev-result { font-weight: 600; }
.ev .ev-result.ok { color: var(--ok); }
.ev .ev-result.no { color: var(--bad); }

/* 히스토리 */
.history-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; }
.filters { display: flex; flex-wrap: wrap; gap: 6px; }
.filters .chip { cursor: pointer; }
#history-list { display: grid; gap: 10px; }
.s-card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; display: grid; gap: 6px; text-align: left; width: 100%; }
.s-card:hover { border-color: var(--accent); }
.s-card .top { display: flex; justify-content: space-between; gap: 10px; flex-wrap: wrap; }
.s-card .when-text { font-weight: 700; font-variant-numeric: tabular-nums; }
.empty { color: var(--muted); padding: 24px; text-align: center; background: var(--surface); border-radius: var(--radius); border: 1px dashed var(--line); }
.detail { display: grid; gap: 14px; }
.panel { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; display: grid; gap: 10px; min-width: 0; }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 4px 14px; font-size: 14px; margin: 0; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; min-width: 0; overflow-wrap: anywhere; }
.table-wrap { overflow-x: auto; }
table { border-collapse: collapse; width: 100%; font-size: 14px; }
th, td { text-align: left; vertical-align: top; padding: 8px 10px; border-top: 1px solid var(--line); }
thead th { border-top: 0; font-size: 12px; color: var(--muted); }
td.t { font-family: var(--mono); white-space: nowrap; }
audio { width: 100%; }
pre { white-space: pre-wrap; overflow-wrap: anywhere; font-family: var(--mono); font-size: 12.5px; background: var(--bg); padding: 12px; border-radius: 8px; max-height: 60vh; overflow: auto; margin: 0; }
details.raw > summary { cursor: pointer; color: var(--muted); font-size: 14px; }

/* 페르소나 */
.persona-form { display: grid; gap: 12px; }
.sub-head { margin: 18px 0 8px; font-size: 15px; display: flex; gap: 8px; align-items: baseline; flex-wrap: wrap; }
.card-list { display: grid; gap: 10px; grid-template-columns: repeat(auto-fill, minmax(min(100%, 300px), 1fr)); }
.p-card .top strong { overflow-wrap: anywhere; }
.p-card .clamp { display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.chip.ok.on { background: var(--sunken); color: var(--ok); }
.link { background: none; border: 0; padding: 0; color: var(--accent); cursor: pointer; font-weight: 600; }
#persona-edit-view .items { grid-template-columns: repeat(auto-fill, minmax(min(100%, 320px), 1fr)); }

/* 대화 상자·로그인 */
dialog { border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); color: var(--ink); width: min(760px, 94vw); padding: 14px; }
dialog::backdrop { background: rgba(0,0,0,.4); }
.dialog-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 6px; }
.login { position: fixed; inset: 0; background: var(--bg); display: grid; place-items: center; z-index: 50; padding: 16px; }
.login-card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 20px; display: grid; gap: 10px; width: min(360px, 100%); }
.login-card input { border: 1px solid var(--line); border-radius: 8px; padding: 9px; background: var(--bg); }
@media (prefers-reduced-motion: reduce) { * { transition: none !important; animation: none !important; } }

/* 실행 탭의 대화는 따로 스크롤한다 (상태 표시와 종료 버튼이 화면 밖으로 밀려나지 않게) */
#tab-run .feed { max-height: calc(100vh - 380px); min-height: 240px; overflow-y: auto; }
td.result { white-space: nowrap; font-weight: 600; }
td.result.ok { color: var(--ok); }
td.result.no { color: var(--bad); }

/* ── 좁은 화면 (휴대폰·태블릿 세로): 반드시 파일 끝에 둔다 (위의 기본 규칙을 덮어써야 한다) ── */
.only-narrow { display: none !important; }
@media (max-width: 900px) {
  .layout { grid-template-columns: 1fr; }
  .only-narrow { display: flex !important; }
  .sidebar { position: fixed; top: 0; left: 0; bottom: 0; height: 100%; width: min(92vw, 400px); z-index: 40;
             transform: translateX(-102%); transition: transform .22s ease;
             padding-top: env(safe-area-inset-top, 0px); padding-bottom: env(safe-area-inset-bottom, 0px); }
  .sidebar.open { transform: none; box-shadow: 0 0 40px rgba(0, 0, 0, .25); }
  .backdrop { position: fixed; inset: 0; background: rgba(0, 0, 0, .35); z-index: 30; }
  main { padding: 14px 16px calc(40px + env(safe-area-inset-bottom, 0px)); }
  .brand span { display: none; }
  #tab-run .feed { max-height: none; min-height: 200px; overflow-y: visible; }
  /* 대화가 길어져 페이지가 내려가도 상태·종료 버튼·기다리는 시간·오류 알림이 보이게 상단바 아래에 고정 */
  #tab-run .status-card { position: sticky; top: calc(env(safe-area-inset-top, 0px) + var(--topbar-h, 56px)); z-index: 10;
                          box-shadow: 0 6px 14px -10px rgba(0, 0, 0, .35); }
  .bubble { max-width: 92%; }
}
@media (max-width: 640px) {
  /* 오류 표 → 이벤트마다 카드 한 장 */
  .events-table thead { display: none; }
  .events-table, .events-table tbody { display: block; }
  .events-table tr { display: grid; grid-template-columns: auto minmax(0, 1fr); gap: 4px 10px; align-items: center;
                     padding: 10px 0; border-top: 1px solid var(--line); }
  .events-table td { display: block; border: 0; padding: 0; min-width: 0; }
  .events-table td:nth-child(n+3) { grid-column: 1 / -1; }
  .events-table td.result:not(:empty)::before { content: "결과: "; color: var(--muted); font-weight: 400; }
}
@media (max-width: 480px) {
  /* 탭이 세 개라 좁은 휴대폰에서도 한 줄에 들어가게 */
  .tabs button { padding: 6px 9px; }
}
