* { box-sizing: border-box; }
html, body { margin: 0; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; background: #f5f5f7; color: #222; }

/* login */
.login-body { display: grid; place-items: center; min-height: 100vh; }
.login-card { background: white; padding: 32px 28px; border-radius: 12px; box-shadow: 0 2px 16px rgba(0,0,0,.08); width: 280px; text-align: center; }
.login-card h1 { margin: 0 0 16px; font-size: 22px; }
.login-card input { width: 100%; padding: 12px 16px; font-size: 24px; letter-spacing: 8px; text-align: center; border: 1px solid #ddd; border-radius: 8px; margin-bottom: 12px; }
.login-card button { width: 100%; padding: 12px; font-size: 16px; background: #007aff; color: white; border: 0; border-radius: 8px; cursor: pointer; }
.login-card .err { color: #d11; font-size: 13px; margin-top: 12px; }

/* layout */
header { display: flex; justify-content: space-between; align-items: center; padding: 12px 20px; background: white; border-bottom: 1px solid #e5e5ea; }
header h1 { margin: 0; font-size: 18px; }
header .actions button { background: transparent; border: 0; font-size: 18px; cursor: pointer; padding: 4px 8px; }

main { width: 100%; margin: 0; padding: 16px; display: grid; gap: 16px; grid-template-columns: 1fr 1fr; grid-template-areas: "modes modes" "input output"; }
nav#modes { grid-area: modes; display: flex; flex-wrap: wrap; gap: 6px; }
#input-area { grid-area: input; }
#output { grid-area: output; }
@media (max-width: 900px) { main { grid-template-columns: 1fr; grid-template-areas: "modes" "input" "output"; } }
.mode-btn { padding: 8px 14px; border: 1px solid #c8c8cc; background: white; border-radius: 18px; cursor: pointer; font-size: 14px; }
.mode-btn.active { background: #007aff; color: white; border-color: #007aff; }

#input-area { background: white; padding: 12px; border-radius: 8px; border: 1px solid #e5e5ea; }
#prompt { width: 100%; resize: vertical; font-family: ui-monospace, "SF Mono", Menlo, monospace; font-size: 14px; padding: 8px; border: 1px solid #e5e5ea; border-radius: 6px; }
.input-controls { display: flex; gap: 8px; align-items: center; margin-top: 8px; flex-wrap: wrap; }
.input-controls button { padding: 8px 14px; font-size: 14px; border: 1px solid #c8c8cc; background: white; border-radius: 6px; cursor: pointer; }
.input-controls button.primary { background: #007aff; color: white; border-color: #007aff; }
.input-controls button.danger { background: #d11; color: white; border-color: #d11; }
.input-controls button:disabled { opacity: .4; cursor: not-allowed; }
.img-attach { padding: 8px 12px; border: 1px dashed #c8c8cc; border-radius: 6px; cursor: pointer; font-size: 13px; }
#image-name { margin-left: 6px; color: #555; }
#active-mode { margin-top: 8px; font-size: 13px; color: #666; }

#output { background: white; padding: 12px; border-radius: 8px; border: 1px solid #e5e5ea; min-height: 280px; }
.out-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; padding-bottom: 8px; border-bottom: 1px solid #e5e5ea; }
.out-header h2 { margin: 0; font-size: 15px; color: #444; }
.out-controls { display: flex; gap: 6px; }
.out-controls button { font-size: 13px; padding: 4px 10px; background: white; border: 1px solid #c8c8cc; border-radius: 6px; cursor: pointer; }
#result { white-space: pre-wrap; word-break: break-word; font-family: ui-monospace, "SF Mono", Menlo, monospace; font-size: 14px; min-height: 60vh; margin: 0; padding: 8px; background: #fafafa; border-radius: 6px; }
#result:empty::before { content: attr(data-empty); color: #999; font-style: italic; }
#prompt { min-height: 40vh; }

/* 임상 계산기 모달 */
.calc-tabs { display: flex; gap: 6px; margin-bottom: 12px; flex-wrap: wrap; }
.calc-tab { padding: 6px 14px; border: 1px solid var(--border, #c8c8cc); background: var(--card, white); color: var(--fg, #222); border-radius: 18px; cursor: pointer; font-size: 13px; }
.calc-tab.active { background: var(--primary, #007aff); color: white; border-color: var(--primary, #007aff); }
.calc-pane { display: flex; flex-direction: column; gap: 8px; padding: 8px 0; }
.calc-pane.hidden { display: none; }
.calc-meta { font-size: 12px; color: var(--muted, #777); padding: 6px 8px; background: var(--result-bg, #fafafa); border-radius: 6px; }
.calc-pane label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted, #555); }
.calc-pane label input, .calc-pane label select { padding: 8px 10px; font-size: 14px; border: 1px solid var(--border, #e5e5ea); border-radius: 6px; background: var(--result-bg, #fafafa); color: var(--fg, #222); }
.calc-run { padding: 10px 16px; background: var(--primary, #007aff); color: white; border: 0; border-radius: 6px; cursor: pointer; font-size: 14px; align-self: flex-start; }
.calc-out { padding: 10px 12px; background: var(--cite-bg, #f0f4ff); border: 1px solid var(--cite-border, #c8d4ff); border-radius: 6px; font-size: 14px; color: var(--fg, #222); min-height: 24px; }
.calc-out:empty { display: none; }
.chemo-block { border: 1px solid var(--border, #e5e5ea); border-radius: 6px; padding: 8px 12px; margin-top: 8px; background: var(--result-bg, #fafafa); }
.chemo-block legend { font-size: 12px; font-weight: 600; color: var(--primary, #007aff); padding: 0 6px; }
.popq-grid { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 8px; }
.popq-grid label { font-size: 12px; }
.popq-table { width: 100%; margin-top: 8px; border-collapse: collapse; font-size: 13px; }
.popq-table td { padding: 4px 8px; border: 1px solid var(--cite-border, #c8d4ff); }
.popq-table td:nth-child(odd) { color: var(--muted, #557); font-weight: 600; }

/* 외부 도구 모달 카드 리스트 */
#tools-list { display: flex; flex-direction: column; gap: 8px; }
.tool-card { display: block; padding: 12px 14px; background: var(--result-bg, #fafafa); border: 1px solid var(--border, #e5e5ea); border-radius: 8px; text-decoration: none; color: var(--fg, #222); transition: border-color .15s, transform .05s; }
.tool-card:hover { border-color: var(--primary, #007aff); }
.tool-card:active { transform: scale(.99); }
.tool-card .tc-title { font-weight: 600; font-size: 14px; margin-bottom: 4px; color: var(--primary, #007aff); }
.tool-card .tc-desc { font-size: 12px; color: var(--muted, #666); }
.tool-card .tc-url { font-size: 11px; color: var(--muted, #888); margin-top: 4px; word-break: break-all; }

/* citations toggle (📚 — dxtx 모드만) */
#citations { margin-top: 12px; }
#citations.hidden { display: none; }
#citations details { background: var(--cite-bg, #f0f4ff); border: 1px solid var(--cite-border, #c8d4ff); border-radius: 6px; padding: 6px 10px; }
#citations summary { cursor: pointer; font-size: 13px; color: var(--cite-summary, #335); padding: 4px 0; user-select: none; }
#citations summary:hover { color: var(--primary, #007aff); }
#citations .cite { margin-top: 8px; padding: 6px 8px; background: var(--card, white); border: 1px solid var(--cite-border, #e0e8ff); border-radius: 4px; }
#citations .cite-head { font-size: 12px; color: var(--cite-summary, #557); font-weight: 600; margin-bottom: 4px; }
#citations .cite-body { white-space: pre-wrap; word-break: break-word; font-family: ui-monospace, "SF Mono", Menlo, monospace; font-size: 12px; color: var(--fg, #333); max-height: 200px; overflow-y: auto; margin: 0; padding: 4px; background: var(--result-bg, #fafafa); border-radius: 4px; }

/* ── 결과 영역 시각 강조 (■ 헤더 / ★ 강조 / 인용) ── */
.r-header { display: inline-block; font-weight: 700; color: var(--primary, #007aff); font-size: 15px; padding-top: 4px; }
.r-star { color: var(--star, #d11); font-weight: 700; }

/* ── 다크 모드 ── */
:root {
  --bg: #f5f5f7;
  --fg: #222;
  --muted: #666;
  --card: #ffffff;
  --border: #e5e5ea;
  --result-bg: #fafafa;
  --primary: #007aff;
  --star: #d11;
  --cite-bg: #f0f4ff;
  --cite-border: #c8d4ff;
  --cite-summary: #335;
}
[data-theme="dark"] {
  --bg: #1c1c1e;
  --fg: #e5e5e7;
  --muted: #98989d;
  --card: #2c2c2e;
  --border: #3a3a3c;
  --result-bg: #1c1c1e;
  --primary: #0a84ff;
  --star: #ff6961;
  --cite-bg: #1f2937;
  --cite-border: #3b475a;
  --cite-summary: #a8b3c4;
}
html, body { background: var(--bg); color: var(--fg); }
header { background: var(--card); border-bottom-color: var(--border); }
#input-area, #output { background: var(--card); border-color: var(--border); }
#result { background: var(--result-bg); color: var(--fg); }
#prompt { background: var(--result-bg); color: var(--fg); border-color: var(--border); }
.mode-btn { background: var(--card); color: var(--fg); border-color: var(--border); }
.mode-btn.active { background: var(--primary); color: white; border-color: var(--primary); }
.input-controls button { background: var(--card); color: var(--fg); border-color: var(--border); }
.input-controls button.primary { background: var(--primary); color: white; border-color: var(--primary); }
.out-controls button { background: var(--card); color: var(--fg); border-color: var(--border); }
.out-header { border-bottom-color: var(--border); }
#active-mode { color: var(--muted); }
.img-attach { border-color: var(--border); color: var(--fg); }
#image-name { color: var(--muted); }
.modal-card { background: var(--card); color: var(--fg); }
.modal-card textarea { background: var(--result-bg); color: var(--fg); border-color: var(--border); }
.modal-actions button { background: var(--card); color: var(--fg); border-color: var(--border); }
.modal-actions button.primary { background: var(--primary); color: white; border-color: var(--primary); }
header .actions button { color: var(--fg); }

/* modal */
.modal { position: fixed; inset: 0; background: rgba(0,0,0,.4); display: grid; place-items: center; z-index: 100; }
.modal.hidden { display: none; }
.modal-card { background: white; width: 90%; max-width: 720px; max-height: 85vh; overflow: auto; padding: 20px; border-radius: 10px; }
.modal-card h2 { margin: 0 0 12px; }
.modal-card textarea { width: 100%; font-family: ui-monospace, "SF Mono", Menlo, monospace; font-size: 13px; padding: 8px; border: 1px solid #e5e5ea; border-radius: 6px; }
.modal-actions { margin-top: 12px; display: flex; justify-content: flex-end; gap: 8px; }
.modal-actions button { padding: 8px 14px; font-size: 14px; border: 1px solid #c8c8cc; background: white; border-radius: 6px; cursor: pointer; }
.modal-actions button.primary { background: #007aff; color: white; border-color: #007aff; }
