/* D1-1: dark default (Codex-style baseline). Light: data-theme="light" on <html> (toggle D1-5). */
:root,
[data-theme="dark"] {
  --bg: #0d1117;
  --panel: #161b22;
  --line: #30363d;
  --text: #e6edf3;
  --muted: #8b949e;
  --accent: #58a6ff;
  --accent-soft: #21262d;
  --danger: #f85149;
  --input-bg: #0d1117;
  --radius: 6px;
  --chat-user-bg: #1f6feb;
  --chat-user-fg: #ffffff;
  --chat-assist-bg: var(--panel);
  --chat-busy-line: var(--accent);
}

[data-theme="light"] {
  --bg: #f8fafc;
  --panel: #ffffff;
  --line: #e5e7eb;
  --text: #0f172a;
  --muted: #64748b;
  --accent: #4f46e5;
  --accent-soft: #eef2ff;
  --danger: #dc2626;
  --input-bg: #ffffff;
  --chat-user-bg: #4f46e5;
  --chat-user-fg: #ffffff;
  --chat-assist-bg: var(--panel);
  --chat-busy-line: var(--accent);
}

:root {
  font-family: ui-monospace, Menlo, Consolas, "Cascadia Code", "Segoe UI Mono", monospace;
  color: var(--text);
  background: var(--bg);
}
* { box-sizing: border-box; }
html { overflow-x: clip; }
body { margin: 0; overflow-x: hidden; max-width: 100%; }
button, input, textarea { font: inherit; }
input, textarea {
  width: 100%; padding: 10px 12px; border: 1px solid var(--line); border-radius: var(--radius);
  background: var(--input-bg); color: var(--text); outline: none;
}
input:focus, textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
textarea { min-height: 320px; resize: vertical; font-family: ui-monospace, Menlo, monospace; font-size: 14px; }
button {
  cursor: pointer; border: 1px solid var(--line); background: var(--panel); color: var(--text);
  padding: 9px 14px; border-radius: var(--radius);
}
button:disabled { opacity: .5; cursor: default; }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--bg); }
button.danger { color: var(--danger); }
button.ghost { border: none; background: transparent; color: var(--muted); }
.muted { color: var(--muted); }
.error { color: var(--danger); margin: 8px 0; }
.pad { padding: 24px; }
.right { margin-left: auto; }

.login { display: grid; place-items: center; height: 100vh; padding: 16px; }
.card { display: grid; gap: 12px; width: 320px; padding: 24px; background: var(--panel); border: 1px solid var(--line); border-radius: 12px; }
.login-card {
  width: min(360px, calc(100vw - 32px));
  gap: 14px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.35);
}
[data-theme="light"] .login-card { box-shadow: 0 8px 32px rgba(15, 23, 42, 0.08); }
.login-card__hint { margin: 0; text-align: center; font-size: 13px; }
.login-card__submit { width: 100%; }
/* Codex minimal BRAND — wordmark «Катюша» only; no logos */
.brand { display: flex; max-width: 100%; min-width: 0; }
.brand--login { flex-direction: column; align-items: center; margin-bottom: 4px; }
.brand--header {
  flex-direction: row;
  align-items: center;
  flex-shrink: 1;
  min-width: 0;
}
.brand__wordmark {
  font-size: 14px;
  font-weight: 600;
  letter-spacing: -0.02em;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  line-height: 1.2;
}
.brand__wordmark--login {
  font-size: 22px;
  font-weight: 700;
  letter-spacing: 0.14em;
  text-transform: uppercase;
}

.app { display: grid; grid-template-rows: 44px 1fr; height: 100vh; overflow-x: hidden; }
header {
  display: flex; align-items: center; gap: 12px; height: 44px;
  padding: 0 12px; background: var(--panel); border-bottom: 1px solid var(--line); min-width: 0;
}
nav { display: flex; gap: 2px; flex-shrink: 0; }
.tab {
  border: none; background: transparent; color: var(--muted);
  padding: 6px 12px; border-radius: 6px; font-size: 13px;
}
.tab:hover:not(.active) { background: var(--bg); color: var(--text); }
.tab.active { background: var(--accent-soft); color: var(--accent); }
.header-actions {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 2px;
  flex-shrink: 0;
}
header .ghost {
  flex-shrink: 0;
  padding: 6px 10px;
  font-size: 13px;
  border-radius: 6px;
}
header .ghost:hover { color: var(--text); background: var(--bg); }
.theme-toggle {
  font-size: 12px;
  padding: 6px 10px;
  border-radius: 6px;
  color: var(--muted);
  border: 1px solid transparent;
}
.theme-toggle:hover { color: var(--text); background: var(--bg); border-color: var(--line); }
.theme-toggle[aria-pressed="true"] { color: var(--accent); }
main { overflow: hidden; min-height: 0; }

.split { display: grid; grid-template-columns: 260px 1fr; height: 100%; min-height: 0; }
.sidebar {
  display: flex; flex-direction: column; gap: 6px; padding: 10px 12px;
  border-right: 1px solid var(--line); background: var(--bg); overflow: auto; min-height: 0;
}
.sidebar > .primary { width: 100%; padding: 8px 12px; font-size: 13px; }
.sidebar form { margin: 0; }
.sidebar form input { padding: 8px 10px; font-size: 13px; }
.list { list-style: none; margin: 0; padding: 0; display: grid; gap: 1px; }
.list li {
  display: flex; align-items: center; gap: 6px; min-height: 32px;
  padding: 5px 8px; border-radius: var(--radius); cursor: pointer; font-size: 13px; line-height: 1.35;
}
.list li span { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.list li:hover { background: var(--panel); }
.list li.active { background: var(--accent-soft); color: var(--accent); }
.list li .ghost {
  opacity: 0; flex-shrink: 0; padding: 2px 6px; line-height: 1; font-size: 16px;
}
.list li:hover .ghost,
.list li:focus-within .ghost { opacity: 1; }
.list li .ghost:hover { color: var(--danger); }
.snippet { display: block; font-size: 12px; white-space: normal; overflow: hidden; text-overflow: ellipsis; }

@media (min-width: 1366px) {
  .split { grid-template-columns: 280px 1fr; }
  .sidebar { padding: 12px 14px; }
}

/* D1-6: desktop visual acceptance at 1440 / 1920 */
@media (min-width: 1440px) {
  .messages { padding: 18px 28px 24px; }
  .split:has(> .editor) > .editor .stack { padding: 18px 28px 24px; }
}
@media (min-width: 1920px) {
  .split:has(> .editor) > .editor .stack { max-width: 80rem; }
  .settings-panel { max-width: 720px; }
}

/* D1-3: chat view — messages, composer, mic / busy / error (both themes) */
.chat {
  display: grid;
  grid-template-rows: 1fr auto auto;
  height: 100%;
  min-height: 0;
  background: var(--bg);
}
.messages {
  overflow: auto;
  padding: 12px 16px 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  scroll-padding-bottom: 10px;
}
.messages > .muted {
  margin: auto;
  max-width: 36ch;
  text-align: center;
  font-size: 13px;
  line-height: 1.45;
}
.msg {
  max-width: min(72ch, 88%);
  padding: 10px 12px;
  border-radius: 8px;
  font-size: 14px;
  white-space: pre-wrap;
  line-height: 1.55;
  overflow-wrap: anywhere;
}
.msg.user {
  align-self: flex-end;
  background: var(--chat-user-bg);
  color: var(--chat-user-fg);
  border: 1px solid transparent;
}
.msg.assistant {
  align-self: flex-start;
  background: var(--chat-assist-bg);
  color: var(--text);
  border: 1px solid var(--line);
}
.chat:has(.composer input:disabled) .msg.assistant:last-child {
  border-color: var(--chat-busy-line);
  box-shadow: 0 0 0 1px var(--accent-soft);
}
.chat > .error {
  margin: 0;
  padding: 10px 16px;
  font-size: 13px;
  line-height: 1.4;
  border-top: 1px solid var(--line);
  background: var(--panel);
}
.composer {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 16px;
  border-top: 1px solid var(--line);
  background: var(--panel);
}
.chat:has(.mic.on) .composer {
  border-top-color: var(--danger);
}
.composer input {
  flex: 1;
  min-width: 0;
  width: auto;
  padding: 8px 12px;
  font-size: 14px;
  border-radius: 8px;
}
.composer .primary {
  flex-shrink: 0;
  padding: 8px 14px;
  font-size: 13px;
  border-radius: 8px;
}
.composer:has(input:disabled) {
  opacity: 0.94;
}
.composer:has(input:disabled):not(:has(.mic.on)) .mic:not(.on) {
  opacity: 0.45;
}
.mic {
  width: 40px;
  height: 40px;
  padding: 0;
  flex-shrink: 0;
  display: grid;
  place-items: center;
  border-radius: 8px;
  font-size: 1.15rem;
  line-height: 1;
}
.mic.on {
  background: var(--danger);
  color: #fff;
  border-color: var(--danger);
  animation: mic-rec-pulse 1.25s ease-in-out infinite;
}
.mic:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
@keyframes mic-rec-pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(248, 81, 73, 0.35); }
  50% { box-shadow: 0 0 0 5px rgba(248, 81, 73, 0.12); }
}
[data-theme="light"] .mic.on {
  animation-name: mic-rec-pulse-light;
}
@keyframes mic-rec-pulse-light {
  0%, 100% { box-shadow: 0 0 0 0 rgba(220, 38, 38, 0.3); }
  50% { box-shadow: 0 0 0 5px rgba(220, 38, 38, 0.1); }
}

/* PMV0: voice mic / recording / busy / error (lane 20 — not D1 units) */
:root,
[data-theme="dark"] {
  --pmv0-error-surface: color-mix(in srgb, var(--danger) 16%, var(--panel));
  --pmv0-recording-surface: color-mix(in srgb, var(--danger) 8%, var(--panel));
}
[data-theme="light"] {
  --pmv0-error-surface: color-mix(in srgb, var(--danger) 10%, var(--panel));
  --pmv0-recording-surface: color-mix(in srgb, var(--danger) 6%, var(--panel));
}
.chat.chat--recording .composer {
  border-top-color: var(--danger);
  background: var(--pmv0-recording-surface);
}
.chat.chat--recording .composer input {
  border-color: color-mix(in srgb, var(--danger) 45%, var(--line));
}
.chat.chat--recording .composer input::placeholder {
  color: var(--danger);
  opacity: 0.88;
}
.chat.chat--busy:not(.chat--recording) .composer {
  border-top-color: var(--chat-busy-line);
}
.chat.chat--busy .composer input:disabled {
  cursor: wait;
}
.chat.chat--busy .mic:not(.on):disabled {
  opacity: 0.42;
}
.chat.chat--busy .messages .msg.assistant:last-child {
  border-color: var(--chat-busy-line);
  box-shadow: 0 0 0 1px var(--accent-soft);
}
.chat > .error.chat__voice-error {
  color: var(--danger);
  margin: 0;
  padding: 10px 16px 10px 13px;
  font-size: 13px;
  line-height: 1.45;
  font-weight: 500;
  border-top: 1px solid color-mix(in srgb, var(--danger) 35%, var(--line));
  border-left: 3px solid var(--danger);
  background: var(--pmv0-error-surface);
}
.chat .mic:not(.on):not(:disabled):hover {
  border-color: var(--accent);
  background: var(--accent-soft);
}
.chat .mic.on {
  font-size: 1rem;
  font-weight: 700;
  letter-spacing: -0.02em;
}

/* D1-4: documents tab — sidebar search/list/snippets + editor pane (visual only) */
.split:has(> .editor) > .sidebar {
  gap: 10px;
}
.split:has(> .editor) > .sidebar > form {
  flex-shrink: 0;
  padding-bottom: 10px;
  border-bottom: 1px solid var(--line);
}
.split:has(> .editor) > .sidebar .list {
  flex: 1;
  min-height: 0;
  overflow: auto;
  align-content: start;
}
.split:has(> .editor) .list li {
  align-items: flex-start;
  min-height: auto;
  padding: 8px 10px;
}
.split:has(> .editor) .list li > span {
  display: block;
  white-space: normal;
  line-height: 1.35;
}
.split:has(> .editor) .list li > span:not(:has(.snippet)) {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.split:has(> .editor) .snippet {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  line-clamp: 2;
  margin-top: 3px;
  font-size: 11px;
  line-height: 1.4;
  color: var(--muted);
}
.split:has(> .editor) .list li.muted {
  cursor: default;
  justify-content: center;
  font-size: 12px;
  padding: 14px 10px;
}
.split:has(> .editor) .list li.muted:hover {
  background: transparent;
}
.split:has(> .editor) > .editor {
  display: flex;
  flex-direction: column;
  height: 100%;
  min-height: 0;
  padding: 0;
  overflow: hidden;
  background: var(--bg);
}
.split:has(> .editor) > .editor .stack {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
  gap: 12px;
  width: 100%;
  max-width: 72rem;
  margin: 0 auto;
  padding: 16px 20px 20px;
  overflow: auto;
}
.split:has(> .editor) > .editor .stack > input:first-of-type {
  flex-shrink: 0;
  font-size: 15px;
  font-weight: 600;
  padding: 10px 4px;
  border: none;
  border-bottom: 1px solid var(--line);
  border-radius: 0;
  background: transparent;
  box-shadow: none;
}
.split:has(> .editor) > .editor .stack > input:first-of-type:focus {
  border-color: var(--accent);
  box-shadow: none;
}
.split:has(> .editor) > .editor .stack > input:nth-of-type(2) {
  flex-shrink: 0;
  padding: 8px 10px;
  font-size: 13px;
  border-radius: 8px;
}
.split:has(> .editor) > .editor .stack > textarea {
  flex: 1;
  min-height: 200px;
  padding: 14px;
  font-size: 14px;
  line-height: 1.55;
  border-radius: 8px;
}
.split:has(> .editor) > .editor .row {
  flex-shrink: 0;
  flex-wrap: wrap;
  gap: 10px;
  padding-top: 12px;
  margin-top: auto;
  border-top: 1px solid var(--line);
}
.split:has(> .editor) > .editor .row button {
  font-size: 13px;
  padding: 8px 14px;
  border-radius: 8px;
}
.split:has(> .editor) > .editor .row .muted {
  font-size: 12px;
}

/* D1-CX-2: documents + settings — Codex IDE pane polish (visual only) */
.main--docs { overflow: hidden; }
.docs-sidebar {
  background: var(--panel);
  border-right-color: var(--line);
}
.docs-sidebar > .primary {
  font-size: 12px;
  font-weight: 600;
  padding: 7px 10px;
  border-radius: var(--radius);
}
.docs-sidebar form input {
  background: var(--input-bg);
  border-color: var(--line);
  font-size: 12px;
}
.docs-editor {
  background: var(--bg);
}
.docs-editor .stack > textarea {
  background: var(--input-bg);
  border-color: var(--line);
  font-size: 13px;
  line-height: 1.5;
  border-radius: var(--radius);
}
.docs-editor .stack > input:nth-of-type(2) {
  background: var(--input-bg);
  font-size: 12px;
  color: var(--muted);
}

.main--settings {
  overflow: auto;
  background: var(--bg);
  display: flex;
  justify-content: center;
  padding: 16px 14px 24px;
}
.editor, .settings { padding: 20px; overflow: auto; }
.settings { max-width: none; padding: 0; width: 100%; }
.settings-panel {
  margin: 0;
  padding: 18px 20px 22px;
  max-width: 560px;
  width: 100%;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: 0 1px 0 color-mix(in srgb, var(--line) 40%, transparent);
}
[data-theme="light"] .settings-panel {
  box-shadow: 0 8px 24px rgba(15, 23, 42, 0.06);
}
.settings-panel__head {
  display: grid;
  gap: 4px;
  margin-bottom: 14px;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--line);
}
.settings-panel__title {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
  color: var(--text);
  letter-spacing: -0.02em;
  line-height: 1.3;
}
.settings-panel__lead { margin: 0; font-size: 12px; line-height: 1.45; }
.settings-panel label {
  gap: 6px;
  font-size: 12px;
  font-weight: 500;
  color: var(--muted);
  text-transform: none;
}
.settings-panel input,
.settings-panel textarea {
  font-size: 13px;
  padding: 8px 10px;
}
.settings-panel textarea {
  min-height: 112px;
  resize: vertical;
  font-family: ui-monospace, Menlo, monospace;
  line-height: 1.45;
}
.settings-panel > .muted {
  font-size: 12px;
  line-height: 1.45;
  margin: 2px 0 0;
}
.settings-panel .row {
  padding-top: 12px;
  border-top: 1px solid var(--line);
  margin-top: 8px;
}
.settings-panel .row button.primary {
  font-size: 13px;
  padding: 8px 14px;
}
.stack { display: grid; gap: 12px; }
.stack label { display: grid; gap: 6px; font-size: 14px; color: var(--muted); }
.row { display: flex; align-items: center; gap: 10px; }

/* D1-6: mobile ≤760px — stacked layout, no horizontal overflow */
@media (max-width: 760px) {
  .app { grid-template-rows: auto 1fr; }
  .split { grid-template-columns: 1fr; min-width: 0; width: 100%; }
  .sidebar {
    max-height: min(38vh, 280px);
    border-right: none;
    border-bottom: 1px solid var(--line);
    min-width: 0;
  }
  header {
    gap: 8px;
    padding: 8px 10px;
    flex-wrap: wrap;
    height: auto;
    min-height: 44px;
    row-gap: 6px;
  }
  nav {
    flex: 1 1 auto;
    min-width: 0;
    overflow-x: auto;
    scrollbar-width: thin;
    -webkit-overflow-scrolling: touch;
  }
  .brand__wordmark { font-size: 13px; max-width: min(36vw, 140px); }
  .brand__wordmark--login { font-size: 17px; letter-spacing: 0.1em; max-width: 100%; }
  .theme-toggle { padding: 6px 8px; font-size: 11px; }
  header .ghost { padding: 6px 8px; font-size: 12px; }
  .login .card,
  .login-card { width: min(320px, calc(100vw - 24px)); }
  .messages, .chat, main, .editor, .settings { min-width: 0; }
  .messages { padding: 12px 14px 16px; }
  .msg { max-width: min(92%, 100%); }
  .composer { flex-wrap: wrap; padding: 10px 12px; gap: 8px; }
  .composer input { flex: 1 1 120px; min-width: 0; }
  .split:has(> .editor) > .editor .stack {
    padding: 12px 14px 16px;
    min-width: 0;
  }
  .split:has(> .editor) > .editor .stack > textarea { min-height: 160px; }
  .main--settings { padding: 12px 10px 20px; }
  .settings-panel { padding: 16px 14px 20px; max-width: 100%; border-radius: 8px; }
  .row { flex-wrap: wrap; }
  textarea { min-height: 200px; }
}
