:root {
  --bg: #14111c;
  --panel: #1e1a29;
  --panel-2: #2a2438;
  --text: #f1edf7;
  --muted: #a49cb5;
  --accent: #b48cff;
  --accent-text: #1a1325;
  --user: #6d4bd1;
  --user-text: #ffffff;
  --border: #3a3349;
  --danger: #ff6b81;
  --radius: 18px;
  color-scheme: dark;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f6f4fa;
    --panel: #ffffff;
    --panel-2: #ece8f4;
    --text: #1b1724;
    --muted: #6c6480;
    --accent: #6d4bd1;
    --accent-text: #ffffff;
    --user: #6d4bd1;
    --border: #ddd6ea;
    --danger: #d6334f;
    color-scheme: light;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 17px/1.45 -apple-system, BlinkMacSystemFont, "SF Pro Text", system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

button, input, textarea, select { font: inherit; color: inherit; }
/* 16px+ inputs stop iOS from zooming in on focus */
input, textarea, select { font-size: 17px; }

svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

.muted { color: var(--muted); }
.error { color: var(--danger); }
small { display: block; color: var(--muted); font-size: 14px; margin-top: 4px; }
code { background: var(--panel-2); padding: 1px 5px; border-radius: 6px; font-size: 0.9em; }

.primary, .secondary, .danger {
  border: 0;
  border-radius: 12px;
  padding: 12px 18px;
  font-weight: 600;
  cursor: pointer;
}
.primary { background: var(--accent); color: var(--accent-text); }
.secondary { background: var(--panel-2); }
.danger { background: transparent; color: var(--danger); border: 1px solid var(--danger); }
.link { background: none; border: 0; color: var(--accent); padding: 8px 0; cursor: pointer; font-weight: 600; }
.icon-btn { background: none; border: 0; padding: 8px; border-radius: 10px; color: var(--text); cursor: pointer; display: grid; place-items: center; }
.icon-btn:active { background: var(--panel-2); }

/* ---------- Full-screen panels ---------- */

.screen {
  position: fixed; inset: 0;
  display: grid; place-items: center;
  padding: max(24px, env(safe-area-inset-top)) 24px max(24px, env(safe-area-inset-bottom));
}
.lock-box { width: 100%; max-width: 360px; text-align: center; }
.lock-box form { display: grid; gap: 12px; margin-top: 16px; }
.lock-box input { padding: 14px; border-radius: 12px; border: 1px solid var(--border); background: var(--panel); text-align: center; }
.lock-icon { width: 72px; height: 72px; border-radius: 18px; }
.steps { text-align: left; padding-left: 20px; }
.steps li { margin-bottom: 8px; }

/* ---------- App layout ---------- */

#app {
  position: fixed; inset: 0;
  display: flex; flex-direction: column;
}

.topbar {
  display: flex; align-items: center; gap: 4px;
  padding: calc(env(safe-area-inset-top) + 6px) 8px 6px;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
}
.title { flex: 1; display: flex; flex-direction: column; align-items: center; min-width: 0; }
#ai-name { font-weight: 700; }
.status { font-size: 12px; color: var(--muted); }

.loader { padding: 10px 16px; background: var(--panel); border-bottom: 1px solid var(--border); }
.loader-text { font-size: 14px; color: var(--muted); margin-bottom: 6px; }
.bar { height: 6px; border-radius: 3px; background: var(--panel-2); overflow: hidden; }
.bar > div { height: 100%; width: 0; background: var(--accent); transition: width 0.3s; }

.messages {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 16px 12px;
  display: flex; flex-direction: column; gap: 4px;
}

.welcome { margin: auto 0; text-align: center; padding: 0 12px; }
.welcome h2 { margin: 0 0 8px; }

.msg { display: flex; flex-direction: column; max-width: 86%; }
.msg.user { align-self: flex-end; align-items: flex-end; }
.msg.assistant { align-self: flex-start; align-items: flex-start; }
.bubble {
  padding: 10px 14px;
  border-radius: var(--radius);
  overflow-wrap: anywhere;
  user-select: text;
  -webkit-user-select: text;
}
.user .bubble { background: var(--user); color: var(--user-text); border-bottom-right-radius: 6px; }
.assistant .bubble { background: var(--panel); border-bottom-left-radius: 6px; }
.bubble pre { white-space: pre-wrap; background: var(--panel-2); padding: 8px; border-radius: 8px; margin: 6px 0; font-size: 14px; }
.bubble.typing:empty::after { content: "…"; animation: blink 1s infinite; }
@keyframes blink { 50% { opacity: 0.3; } }

.actions { display: flex; gap: 12px; padding: 2px 6px 8px; }
.actions button { background: none; border: 0; color: var(--muted); font-size: 13px; padding: 4px 0; cursor: pointer; }

.composer {
  display: flex; align-items: flex-end; gap: 8px;
  padding: 8px 10px calc(env(safe-area-inset-bottom) + 8px);
  background: var(--bg);
  border-top: 1px solid var(--border);
}
.composer textarea {
  flex: 1;
  resize: none;
  max-height: 160px;
  padding: 10px 14px;
  border-radius: 20px;
  border: 1px solid var(--border);
  background: var(--panel);
  outline: none;
}
.send {
  flex: none;
  width: 40px; height: 40px;
  border-radius: 50%; border: 0;
  background: var(--accent); color: var(--accent-text);
  display: grid; place-items: center; cursor: pointer;
}
.send.stop { background: var(--panel-2); color: var(--text); }
.send svg { width: 20px; height: 20px; }
.stop svg rect { fill: currentColor; }

/* Hide chats in the iOS app switcher */
body.privacy-blur #app, body.privacy-blur dialog { filter: blur(24px); }

/* ---------- Chat drawer ---------- */

.drawer { position: fixed; inset: 0; z-index: 10; display: flex; }
.drawer-panel {
  width: min(320px, 85vw);
  background: var(--panel);
  padding: calc(env(safe-area-inset-top) + 8px) 12px env(safe-area-inset-bottom);
  overflow-y: auto;
}
.drawer-scrim { flex: 1; background: rgba(0, 0, 0, 0.45); }
.drawer-head { display: flex; justify-content: space-between; align-items: center; }
.drawer-head h2 { margin: 0; font-size: 20px; }
.chat-list { list-style: none; margin: 12px 0; padding: 0; }
.chat-list li { display: flex; align-items: center; border-radius: 10px; }
.chat-list li.active { background: var(--panel-2); }
.chat-open {
  flex: 1; text-align: left; background: none; border: 0;
  padding: 12px 10px; cursor: pointer;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.chat-del { background: none; border: 0; color: var(--muted); padding: 10px; cursor: pointer; }

/* ---------- Settings sheet ---------- */

dialog.sheet {
  width: 100%; max-width: 560px;
  height: 100%; max-height: 100%;
  margin: 0 auto;
  padding: 0; border: 0;
  background: var(--bg); color: var(--text);
}
dialog.sheet::backdrop { background: rgba(0, 0, 0, 0.5); }
.sheet-inner {
  padding: calc(env(safe-area-inset-top) + 8px) 18px calc(env(safe-area-inset-bottom) + 32px);
  display: flex; flex-direction: column; gap: 10px;
}
.sheet-head {
  display: flex; justify-content: space-between; align-items: center;
  position: sticky; top: 0; background: var(--bg); padding: 6px 0; z-index: 1;
}
.sheet-head h2 { margin: 0; }
.sheet h3 { margin: 20px 0 0; font-size: 14px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); }
.sheet label { display: block; font-weight: 600; }
.sheet input[type="text"], .sheet input[type="password"], .sheet input[type="url"], .sheet textarea, .sheet select {
  display: block; width: 100%; margin-top: 6px;
  padding: 11px 12px;
  border-radius: 12px; border: 1px solid var(--border);
  background: var(--panel); font-weight: 400;
}
.sheet textarea { resize: vertical; line-height: 1.4; }
.sheet input[type="range"] { width: 100%; margin-top: 8px; accent-color: var(--accent); }
.sheet .row { display: flex; justify-content: space-between; align-items: center; }
.btn-row { display: flex; flex-wrap: wrap; gap: 10px; }

.switch { appearance: none; width: 50px; height: 30px; border-radius: 15px; background: var(--panel-2); position: relative; cursor: pointer; transition: background 0.2s; }
.switch::after { content: ""; position: absolute; top: 3px; left: 3px; width: 24px; height: 24px; border-radius: 50%; background: #fff; transition: transform 0.2s; }
.switch:checked { background: var(--accent); }
.switch:checked::after { transform: translateX(20px); }

.model-list { display: grid; gap: 8px; }
.model {
  display: flex !important; gap: 12px; align-items: flex-start;
  padding: 12px; border-radius: 12px; background: var(--panel);
  border: 1px solid var(--border); font-weight: 400 !important; cursor: pointer;
}
.model:has(input:checked) { border-color: var(--accent); }
.model input { margin-top: 4px; accent-color: var(--accent); }
.model div { display: flex; flex-direction: column; }
.model .muted { font-size: 14px; }
.advanced summary { cursor: pointer; color: var(--accent); font-weight: 600; padding: 4px 0; }

.memory-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.memory-list li { display: flex; align-items: center; gap: 8px; padding: 8px 4px 8px 12px; border-radius: 10px; background: var(--panel); }
.memory-list li span { flex: 1; }
.memory-list li.muted { background: none; padding-left: 0; }
.memory-add { display: flex; gap: 8px; }
.memory-add input { flex: 1; margin-top: 0 !important; }

/* ---------- Writing studio ---------- */

#toggle-writing.on { color: var(--accent); background: var(--panel-2); }

.writing { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 0 14px calc(env(safe-area-inset-bottom) + 24px); }
.writing h3 { margin: 22px 0 4px; font-size: 14px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); }
.writing label { display: block; font-weight: 600; margin-top: 12px; }
.writing input[type="text"], .writing textarea, .writing select {
  display: block; width: 100%; margin-top: 6px;
  padding: 11px 12px; border-radius: 12px; border: 1px solid var(--border);
  background: var(--panel); font-weight: 400;
}
.writing textarea { resize: vertical; line-height: 1.5; }
.writing .btn-row { margin-top: 12px; }
.writing button:disabled { opacity: 0.45; }
.mono { font: 14px/1.45 ui-monospace, "SF Mono", Menlo, monospace !important; }

.tabs {
  position: sticky; top: 0; z-index: 1;
  display: flex; gap: 4px; padding: 10px 0;
  background: var(--bg);
}
.tabs button {
  flex: 1; border: 0; border-radius: 10px; padding: 8px 4px;
  background: var(--panel); color: var(--muted); font-size: 15px; font-weight: 600; cursor: pointer;
}
.tabs button[aria-selected="true"] { background: var(--accent); color: var(--accent-text); }

.card { background: var(--panel); border: 1px solid var(--border); border-radius: 14px; padding: 12px; margin-top: 12px; }
.total { font-weight: 600; }
.file-btn { display: inline-block; cursor: pointer; }

.item-list { list-style: none; margin: 14px 0 0; padding: 0; display: grid; gap: 8px; }
.item-list li { display: flex; align-items: center; background: var(--panel); border-radius: 12px; }
.item-open { flex: 1; text-align: left; background: none; border: 0; padding: 12px; display: flex; flex-direction: column; cursor: pointer; min-width: 0; }
.item-open strong { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item-open .muted { font-size: 14px; }

.stat-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(100px, 1fr)); gap: 8px; margin-top: 8px; }
.stat { background: var(--panel); border-radius: 12px; padding: 10px; display: flex; flex-direction: column; }
.stat strong { font-size: 20px; }
.stat span { font-size: 13px; color: var(--muted); }
.stat-grid.small .stat strong { font-size: 17px; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.chip {
  display: inline-flex; align-items: center; gap: 2px;
  background: var(--panel); border: 1px solid var(--border);
  border-radius: 999px; padding: 4px 6px 4px 12px; font-size: 15px;
}
.chip:not(:has(button)) { padding-right: 12px; }
.chip button { background: none; border: 0; color: var(--muted); padding: 2px 5px; cursor: pointer; font-size: 14px; }
.chip.star { border-color: var(--accent); }
.chip.star button:first-of-type { color: var(--accent); }
.chip.off { opacity: 0.4; text-decoration: line-through; }
.muted-chips .chip { color: var(--muted); font-size: 14px; }

.chapter-bar { display: flex; gap: 8px; align-items: center; }
.chapter-bar select { flex: 1; margin-top: 0 !important; }
.danger-link { color: var(--danger); }

.score { display: flex; align-items: baseline; gap: 10px; margin-top: 16px; }
.score span { font-size: 40px; font-weight: 800; }
.score small { margin: 0; }
.check-notes p { margin: 4px 0; }
.highlight { white-space: pre-wrap; line-height: 1.6; }
.flag-ai { color: var(--danger); font-weight: 700; }
.flag-new { text-decoration: underline wavy var(--accent); text-underline-offset: 3px; }
.flag-cliche { text-decoration: underline 2px #f0a040; text-underline-offset: 3px; }
.update-banner {
  border: 0; padding: 10px 16px; font-weight: 600; cursor: pointer;
  background: var(--accent); color: var(--accent-text);
}
.version { font-size: 13px; text-align: center; margin-top: 24px; }
.btn-row.center { justify-content: center; }
#computer-status.ok { color: #4caf7a; }
.card.computer { display: grid; gap: 8px; margin-top: 0; }
.card.computer label { margin-top: 0; }
.card.computer code { word-break: break-all; }
.tabs { overflow-x: auto; }
.tabs button { flex: 1 0 auto; padding-left: 10px; padding-right: 10px; }
.gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 8px; margin-top: 12px; }
.gallery figure { margin: 0; }
.gallery img { width: 100%; border-radius: 10px; display: block; cursor: pointer; background: var(--panel); }
dialog.viewer { max-width: min(720px, 96vw); max-height: 96vh; padding: 0; border: 0; border-radius: 16px; background: var(--bg); color: var(--text); }
dialog.viewer::backdrop { background: rgba(0, 0, 0, 0.7); }
.viewer-inner { padding: 12px; }
.viewer-inner img { width: 100%; max-height: 70vh; object-fit: contain; border-radius: 10px; }
#img-status.ok { color: #4caf7a; }
.char-list { display: grid; gap: 8px; margin: 8px 0; }
.char-card { display: flex; align-items: center; gap: 10px; background: var(--panel); border-radius: 12px; padding: 8px 10px; }
.char-card input { width: 22px; height: 22px; accent-color: var(--accent); flex: none; }
.char-card label { flex: 1; display: flex; align-items: center; gap: 10px; margin: 0 !important; cursor: pointer; }
.char-card img { width: 48px; height: 48px; object-fit: cover; border-radius: 8px; }
.char-preview { max-width: 160px; border-radius: 10px; display: block; }
.flag-tail { text-decoration: underline dotted 2px var(--accent); text-underline-offset: 3px; }

.msg .via { display: block; margin: 2px 4px 0; font-size: 0.75rem; color: var(--muted, #888); }
.char-card img.no-ref { background: var(--muted); opacity: 0.3; }

.ref-group { margin: 6px 0; }
.ref-group summary { cursor: pointer; font-weight: 600; padding: 4px 0; }
.ref-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); gap: 8px; margin: 8px 0; }
.ref-card { display: flex; flex-direction: column; gap: 4px; }
.ref-card img { width: 100%; aspect-ratio: 3 / 4; object-fit: cover; border-radius: 8px; }
.ref-card input { font-size: 0.8rem; padding: 4px 6px; min-width: 0; }
.ref-pickers { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 8px; margin: 8px 0; }

.edit-stage { position: relative; width: 100%; touch-action: none; user-select: none; }
.edit-stage img { width: 100%; display: block; border-radius: 10px; }
.edit-stage canvas { position: absolute; inset: 0; width: 100%; height: 100%; opacity: 0.5; cursor: crosshair; border-radius: 10px; }
.edit-tools { align-items: center; }
.edit-brush { flex: 1; min-width: 140px; }
#edit-erase[aria-pressed="true"] { outline: 2px solid var(--accent, #c9a0ff); }
#img-editor label { display: block; margin: 8px 0; }
#img-editor textarea, #img-editor select { display: block; width: 100%; margin-top: 4px; box-sizing: border-box; }
#img-editor .edit-brush input { display: block; width: 100%; }
