:root {
  --bg: #ffffff;
  --panel: #f7f7f8;
  --panel-strong: #ececf1;
  --text: #171717;
  --muted: #6b7280;
  --muted-2: #9ca3af;
  --border: #e5e7eb;
  --border-strong: #d1d5db;
  --accent: #10a37f;
  --accent-dark: #0e8f70;
  --accent-soft: #e8f7f2;
  --pro: #7c3aed;
  --pro-soft: #f3e8ff;
  --danger: #dc2626;
  --success: #059669;
  --code-bg: #0f172a;
  --code-text: #e5e7eb;
  --shadow: 0 24px 90px rgba(0, 0, 0, 0.18);
  --sidebar-width: 286px;
  --composer-width: 900px;
  --font-scale: 1;
}

html[data-theme="dark"] {
  --bg: #0f1115;
  --panel: #171a21;
  --panel-strong: #222631;
  --text: #f5f7fb;
  --muted: #a1a8b8;
  --muted-2: #7f8797;
  --border: #2a2f3a;
  --border-strong: #3a4150;
  --accent-soft: rgba(16, 163, 127, .15);
  --code-bg: #05070b;
  --shadow: 0 24px 90px rgba(0, 0, 0, 0.55);
}
html[data-font="small"] { --font-scale: .94; }
html[data-font="large"] { --font-scale: 1.08; }

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans KR", sans-serif;
  font-size: calc(16px * var(--font-scale));
  -webkit-tap-highlight-color: transparent;
}
button, input, select, textarea { font: inherit; }
button { border: 0; }
button:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible { outline: 3px solid rgba(16, 163, 127, .25); outline-offset: 2px; }

.toast { position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom)); z-index: 120; transform: translateX(-50%) translateY(16px); opacity: 0; pointer-events: none; padding: 11px 14px; border-radius: 999px; color: white; background: #171717; font-size: 13px; box-shadow: 0 16px 40px rgba(0,0,0,.22); transition: .2s ease; max-width: min(92vw, 560px); text-align: center; }
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

.app { height: 100dvh; display: grid; grid-template-columns: var(--sidebar-width) 1fr; overflow: hidden; background: var(--bg); }
.app[data-sidebar="closed"] { grid-template-columns: 0 1fr; }
.app[data-sidebar="closed"] .sidebar { transform: translateX(calc(-1 * var(--sidebar-width))); }
.sidebar { width: var(--sidebar-width); background: var(--panel); border-right: 1px solid var(--border); display: flex; flex-direction: column; transition: transform .18s ease; overflow: hidden; z-index: 40; }
.drawer-backdrop { position: fixed; inset: 0; z-index: 35; background: rgba(0,0,0,.35); backdrop-filter: blur(2px); }
.brand-row { display: flex; align-items: center; gap: 10px; padding: 16px 14px 10px; }
.brand-mark, .empty-logo { width: 38px; height: 38px; border-radius: 12px; display: grid; place-items: center; background: #171717; color: white; font-weight: 850; letter-spacing: -.02em; }
.brand-copy span { display: block; color: var(--muted); font-size: 12px; margin-top: 2px; }
.new-chat-button { margin: 8px 12px 14px; height: 42px; display: flex; align-items: center; gap: 8px; border-radius: 12px; background: var(--bg); border: 1px solid var(--border); color: var(--text); padding: 0 12px; cursor: pointer; font-weight: 700; }
.new-chat-button:hover, .sidebar-item:hover { background: var(--bg); }
.sidebar-section { padding: 6px 12px; }
.sidebar-section.grow { flex: 1; min-height: 0; overflow: hidden; }
.sidebar-label { color: var(--muted-2); font-size: 12px; font-weight: 800; padding: 8px 8px; text-transform: uppercase; letter-spacing: .04em; }
.sidebar-item, .history-title { width: 100%; min-height: 38px; display: flex; align-items: center; text-align: left; border-radius: 10px; padding: 0 10px; background: transparent; color: var(--text); cursor: pointer; font-size: 14px; }
.history-list { height: calc(100% - 34px); overflow-y: auto; padding-right: 2px; }
.history-item { position: relative; margin-bottom: 4px; }
.history-item.active .history-title { background: var(--panel-strong); }
.history-title { padding-right: 34px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.history-delete { position: absolute; right: 4px; top: 5px; width: 28px; height: 28px; border-radius: 8px; background: transparent; color: var(--muted); cursor: pointer; }
.history-delete:hover { background: var(--panel-strong); color: var(--danger); }
.sidebar-footer { border-top: 1px solid var(--border); padding: 10px 12px 14px; }
.footer-action { font-weight: 700; }
.sidebar-footnote { padding: 6px 8px 0; color: var(--muted-2); font-size: 12px; line-height: 1.35; }

.main { min-width: 0; height: 100dvh; display: grid; grid-template-rows: auto 1fr auto; }
.topbar { height: 60px; padding: 10px 16px; display: flex; align-items: center; justify-content: space-between; border-bottom: 1px solid transparent; }
.topbar-left, .topbar-actions { display: flex; align-items: center; gap: 8px; min-width: 0; }
.icon-button { width: 38px; height: 38px; border-radius: 12px; background: transparent; color: var(--text); cursor: pointer; font-size: 18px; display: grid; place-items: center; }
.icon-button:hover { background: var(--panel); }
.model-pill, .ghost-pill, .plan-badge { min-height: 38px; display: inline-flex; align-items: center; border-radius: 999px; padding: 0 12px; background: transparent; color: var(--text); font-weight: 750; border: 1px solid transparent; white-space: nowrap; }
.model-pill { cursor: pointer; overflow: hidden; text-overflow: ellipsis; max-width: 220px; }
.model-pill:hover { background: var(--panel); }
.ghost-pill { border-color: var(--border); color: var(--muted); font-size: 13px; cursor: pointer; background: var(--bg); }
.plan-badge { min-height: 30px; font-size: 12px; }
.plan-badge.free { color: var(--text); background: var(--panel); border-color: var(--border); }
.plan-badge.pro { color: var(--pro); background: var(--pro-soft); border-color: #ddd6fe; }

.chat { overflow-y: auto; padding: 0 16px 28px; scroll-behavior: smooth; }
.empty-state { min-height: 100%; display: flex; flex-direction: column; align-items: center; justify-content: center; max-width: 780px; margin: 0 auto; text-align: center; padding: 40px 0; }
.empty-logo { margin-bottom: 18px; width: 52px; height: 52px; border-radius: 16px; }
.empty-state h1 { margin: 0 0 10px; font-size: clamp(28px, 5vw, 46px); letter-spacing: -.04em; }
.empty-state p { margin: 0 0 26px; color: var(--muted); line-height: 1.6; }
.prompt-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; width: 100%; }
.prompt-card { text-align: left; padding: 16px; min-height: 96px; border-radius: 16px; background: var(--bg); border: 1px solid var(--border); cursor: pointer; color: var(--text); }
.prompt-card:hover { border-color: var(--border-strong); box-shadow: 0 10px 30px rgba(0,0,0,.06); }
.prompt-card strong { display: block; margin-bottom: 6px; }
.prompt-card span { color: var(--muted); font-size: 14px; }

.message { max-width: 900px; margin: 0 auto; padding: 22px 4px; display: grid; grid-template-columns: 38px 1fr; gap: 14px; border-bottom: 1px solid color-mix(in srgb, var(--border) 55%, transparent); }
html[data-compact="true"] .message { padding: 14px 4px; }
.avatar { width: 32px; height: 32px; border-radius: 10px; display: grid; place-items: center; font-size: 13px; font-weight: 800; flex: none; }
.message.user .avatar { background: #111827; color: white; }
.message.assistant .avatar { background: var(--accent-soft); color: var(--accent-dark); }
.message-body { min-width: 0; }
.message-header { display: flex; justify-content: space-between; gap: 8px; align-items: flex-start; }
.message-content { min-width: 0; line-height: 1.65; white-space: pre-wrap; overflow-wrap: anywhere; }
.message-actions { display: flex; gap: 5px; opacity: .15; transition: .15s ease; }
.message:hover .message-actions { opacity: 1; }
.tiny-button { min-height: 28px; border-radius: 8px; padding: 0 8px; background: var(--panel); color: var(--muted); cursor: pointer; font-size: 12px; border: 1px solid var(--border); }
.tiny-button:hover { color: var(--text); }
.message-meta { margin-top: 10px; color: var(--muted-2); font-size: 12px; }
.message-content pre { position: relative; background: var(--code-bg); color: var(--code-text); padding: 42px 16px 16px; border-radius: 14px; overflow-x: auto; white-space: pre; line-height: 1.45; }
.message-content code { font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace; }
.copy-code { position: absolute; right: 10px; top: 10px; min-height: 26px; border-radius: 8px; padding: 0 8px; background: rgba(255,255,255,.09); color: #e5e7eb; border: 1px solid rgba(255,255,255,.12); cursor: pointer; font-size: 12px; }
.typing { display: inline-flex; gap: 5px; align-items: center; }
.typing span { width: 7px; height: 7px; border-radius: 50%; background: var(--muted-2); animation: blink 1s infinite ease-in-out; }
.typing span:nth-child(2) { animation-delay: .15s; }
.typing span:nth-child(3) { animation-delay: .3s; }
@keyframes blink { 0%, 80%, 100% { opacity: .25; transform: translateY(0); } 40% { opacity: 1; transform: translateY(-2px); } }

.composer-shell { padding: 12px 16px calc(18px + env(safe-area-inset-bottom)); background: linear-gradient(180deg, transparent, var(--bg) 25%); }
.composer-tools { max-width: var(--composer-width); margin: 0 auto 8px; display: flex; justify-content: space-between; align-items: center; gap: 8px; color: var(--muted); font-size: 13px; }
.composer-tools select, .composer-actions-inline button { height: 32px; border-radius: 999px; border: 1px solid var(--border); padding: 0 10px; background: var(--bg); color: var(--text); }
.composer-actions-inline { display: flex; gap: 6px; }
.composer { max-width: var(--composer-width); margin: 0 auto; display: grid; grid-template-columns: 1fr 42px; gap: 8px; padding: 10px; border: 1px solid var(--border-strong); border-radius: 24px; background: var(--bg); box-shadow: 0 12px 50px rgba(0,0,0,.06); }
.composer textarea { border: 0; outline: none; resize: none; min-height: 42px; max-height: 220px; padding: 10px 8px; line-height: 1.5; background: transparent; color: var(--text); }
.send-button { width: 42px; height: 42px; border-radius: 14px; background: #171717; color: white; cursor: pointer; font-size: 20px; }
.send-button:disabled { background: #d1d5db; cursor: not-allowed; }
.composer-hint { max-width: var(--composer-width); margin: 8px auto 0; color: var(--muted-2); font-size: 12px; text-align: center; }

.mobile-nav { display: none; position: fixed; left: 0; right: 0; bottom: 0; z-index: 45; height: calc(62px + env(safe-area-inset-bottom)); padding: 6px 8px env(safe-area-inset-bottom); background: color-mix(in srgb, var(--bg) 88%, transparent); backdrop-filter: blur(16px); border-top: 1px solid var(--border); }
.mobile-nav button { flex: 1; border-radius: 14px; background: transparent; color: var(--muted); display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; }
.mobile-nav span { font-size: 20px; line-height: 1; }
.mobile-nav small { font-size: 11px; }
.float-button { position: fixed; right: 18px; bottom: 118px; z-index: 44; width: 42px; height: 42px; border-radius: 50%; background: #171717; color: white; box-shadow: 0 12px 30px rgba(0,0,0,.18); cursor: pointer; }

.modal.hidden, .sheet.hidden { display: none; }
.modal, .sheet { position: fixed; inset: 0; z-index: 90; display: grid; place-items: center; padding: 22px; }
.modal-backdrop, .sheet-backdrop { position: absolute; inset: 0; background: rgba(17, 24, 39, .44); backdrop-filter: blur(8px); }
.modal-card, .sheet-card, .command-card { position: relative; width: min(980px, 100%); max-height: min(860px, 92dvh); display: flex; flex-direction: column; overflow: hidden; border-radius: 24px; background: var(--bg); box-shadow: var(--shadow); border: 1px solid var(--border); }
.modal-header, .sheet-header { padding: 20px 22px; display: flex; justify-content: space-between; gap: 16px; border-bottom: 1px solid var(--border); }
.modal-header h2, .sheet-header h2 { margin: 0; letter-spacing: -.02em; }
.modal-header p, .sheet-header p { margin: 4px 0 0; color: var(--muted); }
.settings-tabs { display: flex; gap: 6px; padding: 12px 14px 0; overflow-x: auto; }
.tab { height: 38px; border-radius: 999px; padding: 0 14px; background: transparent; color: var(--muted); cursor: pointer; font-weight: 750; white-space: nowrap; }
.tab.active { background: #171717; color: white; }
.settings-body { overflow: auto; padding: 18px 22px 22px; }
.tab-panel { display: none; }
.tab-panel.active { display: block; }
.setting-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 14px; }
.setting-grid.nested { margin-top: 14px; }
.wide, .setting-field.wide { grid-column: 1 / -1; }
.setting-field { display: flex; flex-direction: column; gap: 7px; }
.setting-field span { color: var(--muted); font-size: 13px; font-weight: 750; }
.setting-field input, .setting-field select, .setting-field textarea, .memory-editor, .command-search input { width: 100%; border: 1px solid var(--border); border-radius: 14px; padding: 12px; outline: none; background: var(--bg); color: var(--text); }
.setting-field input:focus, .setting-field select:focus, .setting-field textarea:focus, .memory-editor:focus, .command-search input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(16,163,127,.12); }
.switch-card { display: flex; gap: 12px; align-items: center; padding: 14px; border: 1px solid var(--border); border-radius: 16px; background: var(--bg); }
.switch-card input, .switch-row input { width: 22px; height: 22px; accent-color: var(--accent); flex: none; }
.switch-card span { display: flex; flex-direction: column; gap: 3px; }
.switch-card small { color: var(--muted); }
.settings-note { margin-top: 14px; padding: 14px; border-radius: 16px; background: var(--panel); color: var(--muted); line-height: 1.55; }
.button-row, .bio-toolbar { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 14px; }
.button-row button, .bio-toolbar button, #saveSettingsButton, #memoryButton, #healthButton { min-height: 42px; border-radius: 12px; padding: 0 14px; background: #171717; color: white; cursor: pointer; font-weight: 750; }
button.secondary, #resetSettingsButton, #saveMemoryButton, #copyStatusButton, #resetUsageButton, #copyProfileKeyButton, #downloadProfileButton, #importProfileButton, #exportChatButton { background: var(--panel); color: var(--text); border: 1px solid var(--border); }
.danger-lite { color: var(--danger) !important; }
.memory-editor { min-height: 300px; font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace; font-size: 13px; }
.status-cards { display: grid; grid-template-columns: repeat(2, minmax(0,1fr)); gap: 12px; }
.status-card { display: flex; gap: 12px; padding: 16px; border: 1px solid var(--border); border-radius: 16px; background: var(--bg); }
.status-card p { margin: 4px 0 0; color: var(--muted); font-size: 13px; }
.status-dot { width: 11px; height: 11px; border-radius: 50%; margin-top: 4px; background: var(--muted-2); }
.status-dot.ok { background: var(--success); }
.status-dot.bad { background: var(--danger); }
.status-box { min-height: 220px; overflow: auto; padding: 14px; border-radius: 16px; background: var(--code-bg); color: var(--code-text); }
.billing-panel { display: flex; justify-content: space-between; gap: 16px; padding: 18px; border: 1px solid var(--border); border-radius: 18px; background: linear-gradient(135deg, var(--bg), var(--panel)); }
.billing-panel h3 { margin: 4px 0; }
.billing-panel p { color: var(--muted); line-height: 1.55; }
.switch-row { display: flex; align-items: center; gap: 10px; font-weight: 800; white-space: nowrap; }
.plan-grid { display: grid; grid-template-columns: repeat(2, minmax(0,1fr)); gap: 14px; margin-top: 16px; }
.plan-card { padding: 18px; border: 1px solid var(--border); border-radius: 18px; background: var(--bg); }
.plan-card h3 { margin: 8px 0; }
.plan-card p, .plan-card li { color: var(--muted); line-height: 1.55; }
.plan-name { display: inline-flex; height: 28px; align-items: center; padding: 0 10px; border-radius: 999px; background: var(--panel); font-size: 12px; font-weight: 850; }
.pro-glow { border-color: #ddd6fe; background: linear-gradient(135deg, var(--bg), var(--pro-soft)); }
.pro-glow .plan-name { color: var(--pro); background: #ede9fe; }
.eyebrow { display: inline-flex; color: var(--accent-dark); background: var(--accent-soft); border-radius: 999px; padding: 5px 9px; font-size: 12px; font-weight: 850; }
.advanced-panel { border: 1px solid var(--border); border-radius: 16px; padding: 14px; }
.advanced-panel summary { cursor: pointer; font-weight: 800; color: var(--muted); }

.sheet { align-items: end; padding: 0; }
.sheet-card { width: min(980px, 100%); border-radius: 26px 26px 0 0; max-height: 86dvh; }
.template-grid { padding: 18px 22px 22px; overflow: auto; display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
.template-card { text-align: left; padding: 16px; min-height: 112px; border-radius: 18px; background: var(--bg); border: 1px solid var(--border); color: var(--text); cursor: pointer; }
.template-card:hover { border-color: var(--accent); }
.template-card small { display: inline-flex; padding: 3px 8px; border-radius: 999px; background: var(--panel); color: var(--muted); margin-bottom: 10px; }
.template-card strong { display: block; margin-bottom: 6px; }
.template-card span { color: var(--muted); line-height: 1.45; font-size: 14px; }

.command-card { width: min(720px, 100%); }
.command-search { padding: 16px; display: grid; gap: 8px; border-bottom: 1px solid var(--border); }
.command-search span { font-size: 13px; color: var(--muted); font-weight: 800; }
.command-search input { font-size: 18px; }
.command-list { max-height: 460px; overflow: auto; padding: 10px; }
.command-item { width: 100%; min-height: 48px; border-radius: 12px; background: transparent; color: var(--text); display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 0 12px; cursor: pointer; text-align: left; }
.command-item:hover, .command-item.active { background: var(--panel); }
.command-item small { color: var(--muted); }

@media (max-width: 920px) {
  .app { grid-template-columns: 0 1fr; }
  .app[data-sidebar="open"] .sidebar { transform: translateX(0); box-shadow: 20px 0 60px rgba(0,0,0,.18); }
  .sidebar { position: fixed; inset: 0 auto 0 0; transform: translateX(calc(-1 * var(--sidebar-width))); }
  .drawer-backdrop:not([hidden]) { display: block; }
  .prompt-grid, .setting-grid, .status-cards, .plan-grid, .template-grid { grid-template-columns: 1fr; }
  .topbar { padding: 8px 10px; height: 56px; }
  .model-pill { max-width: 145px; padding: 0 8px; }
  .ghost-pill#usagePill, .install-button, .desktop-only { display: none; }
  .topbar-actions { gap: 2px; }
  .chat { padding: 0 10px 16px; }
  .message { grid-template-columns: 32px 1fr; gap: 10px; padding: 18px 0; }
  .avatar { width: 28px; height: 28px; border-radius: 9px; font-size: 12px; }
  .message-actions { opacity: 1; }
  .composer-shell { padding: 8px 10px calc(78px + env(safe-area-inset-bottom)); }
  .composer-tools { font-size: 12px; }
  .composer-actions-inline { display: none; }
  #connectionLabel { display: none; }
  .composer { border-radius: 20px; grid-template-columns: 1fr 40px; padding: 8px; }
  .composer textarea { min-height: 40px; font-size: 16px; }
  .send-button { width: 40px; height: 40px; border-radius: 13px; }
  .composer-hint { display: none; }
  .mobile-nav { display: flex; }
  .modal { padding: 8px; align-items: end; }
  .modal-card, .command-card { max-height: 94dvh; border-radius: 22px 22px 0 0; }
  .modal-header, .sheet-header { padding: 16px; }
  .settings-body { padding: 14px 16px 18px; }
  .settings-tabs { padding: 10px 10px 0; }
  .billing-panel { flex-direction: column; }
  .compact-only-hide { display: none; }
}

@media (max-width: 520px) {
  :root { --sidebar-width: min(86vw, 310px); }
  .brand-row { padding-top: calc(14px + env(safe-area-inset-top)); }
  .topbar-left { gap: 4px; }
  .plan-badge { display: none; }
  #healthPill { display: none; }
  .empty-state { justify-content: flex-start; padding-top: 42px; }
  .empty-logo { display: none; }
  .empty-state h1 { font-size: 30px; }
  .prompt-card { min-height: 86px; padding: 14px; }
  .message-content pre { margin-left: -40px; border-radius: 12px; }
  .button-row button, .bio-toolbar button { width: 100%; }
  .template-card { min-height: 96px; }
}
