/* Apple-inspired theme — light by default, dark via prefers-color-scheme */
:root {
  --bg: #f5f5f7;
  --surface: #ffffff;
  --surface-2: #fafafa;
  --text: #1d1d1f;
  --text-muted: #6e6e73;
  --border: #d2d2d7;
  --border-light: #e5e5ea;
  --accent: #0071e3;
  --accent-hover: #0077ed;
  --danger: #ff3b30;
  --success: #34c759;
  --success-bg: rgba(52,199,89,0.10);
  --success-text: #1d6e35;
  --shadow-sm: 0 1px 3px rgba(0,0,0,0.04), 0 1px 2px rgba(0,0,0,0.06);
  --shadow-md: 0 4px 16px rgba(0,0,0,0.06), 0 2px 6px rgba(0,0,0,0.04);
  --shadow-lg: 0 10px 30px rgba(0,0,0,0.08);
  --radius: 12px;
  --radius-sm: 8px;
  --radius-lg: 18px;
  --topbar-bg: rgba(255,255,255,0.85);
  --highlight: #fff59d;
  --highlight-active: #ffd54f;
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "SF Pro Display",
    "Helvetica Neue", Helvetica, Arial, "Inter", sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0a0a0c;
    --surface: #161618;
    --surface-2: #1c1c1e;
    --text: #f2f2f7;
    --text-muted: #98989d;
    --border: #38383a;
    --border-light: #2c2c2e;
    --accent: #0a84ff;
    --accent-hover: #2997ff;
    --danger: #ff453a;
    --success: #30d158;
    --success-bg: rgba(48,209,88,0.15);
    --success-text: #74e898;
    --shadow-sm: 0 1px 3px rgba(0,0,0,0.4);
    --shadow-md: 0 4px 16px rgba(0,0,0,0.45);
    --shadow-lg: 0 10px 30px rgba(0,0,0,0.55);
    --topbar-bg: rgba(22,22,24,0.85);
    --highlight: #5d4a00;
    --highlight-active: #8a6d00;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; padding: 0; }
body {
  font-family: var(--font);
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  font-size: 15px;
  line-height: 1.5;
  display: flex; flex-direction: column;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
  padding-top: env(safe-area-inset-top);
  padding-bottom: env(safe-area-inset-bottom);
}

h1, h2, h3 { margin: 0 0 8px 0; font-weight: 600; letter-spacing: -0.01em; }
h1 { font-size: 26px; }
h2 { font-size: 19px; }
h3 { font-size: 17px; }
.muted { color: var(--text-muted); font-size: 13px; }

/* ----- Login ----- */
.login-body {
  display: flex; align-items: center; justify-content: center; min-height: 100vh;
  background: linear-gradient(180deg, var(--bg) 0%, var(--surface-2) 100%);
}
.login-card {
  background: var(--surface);
  padding: 40px 36px;
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-md);
  width: min(420px, 92vw);
  border: 1px solid var(--border-light);
}
.brand { text-align: center; margin-bottom: 28px; }
.brand-mark { font-size: 36px; line-height: 1; margin-bottom: 8px; color: var(--accent); }
.brand h1 { margin-bottom: 4px; }

.form { display: flex; flex-direction: column; gap: 12px; }

.shake { animation: shake .35s ease; }
@keyframes shake {
  10%, 90% { transform: translateX(-1px); }
  20%, 80% { transform: translateX(2px); }
  30%, 50%, 70% { transform: translateX(-4px); }
  40%, 60% { transform: translateX(4px); }
}
.form-error {
  background: rgba(255,59,48,0.10); color: var(--danger);
  padding: 10px 14px; border-radius: var(--radius-sm);
  font-size: 14px; font-weight: 500; text-align: center;
}

/* ----- Inputs ----- */
input[type=text], input[type=password], input[type=url], input[type=search],
select, textarea {
  width: 100%;
  padding: 12px 14px;
  font-family: inherit;
  font-size: 15px;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  outline: none;
  transition: border-color .15s, box-shadow .15s;
}
input:focus, select:focus, textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 4px rgba(10,132,255,0.18);
}

/* ----- Top bar ----- */
.topbar {
  flex: 0 0 auto;
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 28px;
  background: var(--topbar-bg);
  backdrop-filter: saturate(180%) blur(20px);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  border-bottom: 1px solid var(--border-light);
  z-index: 10;
}
.brand-mini { font-weight: 600; letter-spacing: -0.01em; }
.topbar-nav { display: flex; gap: 6px; align-items: center; }
.nav-btn {
  background: transparent;
  border: none;
  padding: 7px 14px;
  border-radius: 999px;
  font-size: 14px;
  color: var(--text-muted);
  cursor: pointer;
  font-family: inherit;
  transition: background .15s, color .15s;
}
.nav-btn:hover { background: var(--border-light); color: var(--text); }
.nav-btn.active { background: var(--text); color: var(--bg); }
.nav-btn.ghost { color: var(--text-muted); }

.topbar-overflow { position: relative; }
.overflow-menu {
  position: absolute; right: 0; top: calc(100% + 6px);
  background: var(--surface); border: 1px solid var(--border-light);
  border-radius: var(--radius); box-shadow: var(--shadow-md);
  min-width: 220px; padding: 6px; display: flex; flex-direction: column; gap: 2px;
  z-index: 50;
}
.overflow-item {
  background: transparent; border: none; text-align: left;
  padding: 10px 14px; border-radius: 8px;
  color: var(--text); font-family: inherit; font-size: 14px; cursor: pointer;
}
.overflow-item:hover { background: var(--border-light); }

/* ----- Layout ----- */
.container {
  flex: 1 1 auto;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  width: 100%;
  max-width: 880px;
  margin: 0 auto;
  padding: 28px 20px 60px;
}
.tab { display: none; }
.tab.active { display: block; }

.card {
  background: var(--surface);
  border: 1px solid var(--border-light);
  border-radius: var(--radius-lg);
  padding: 24px;
  margin-bottom: 20px;
  box-shadow: var(--shadow-sm);
}
.card-head {
  display: flex; align-items: flex-start; justify-content: space-between;
  gap: 12px; margin-bottom: 14px; flex-wrap: wrap;
}
.card-actions { display: flex; gap: 8px; flex-wrap: wrap; }

.view-title-block { min-width: 0; flex: 1; }
.view-name { cursor: text; outline: none; padding: 2px 6px; margin-left: -6px; border-radius: 6px; transition: background .15s; }
.view-name:hover { background: var(--border-light); }
.view-name:focus { background: var(--border-light); }

/* ----- Buttons ----- */
.btn {
  display: inline-flex; align-items: center; justify-content: center;
  padding: 9px 18px;
  font-size: 14px; font-weight: 500;
  font-family: inherit;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 999px;
  cursor: pointer;
  transition: background .15s, border-color .15s, transform .05s;
}
.btn:hover { background: var(--surface-2); }
.btn:active { transform: scale(0.98); }
.btn-primary { background: var(--accent); color: #fff; border-color: var(--accent); }
.btn-primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn-danger { color: var(--danger); border-color: var(--border); }
.btn-danger:hover { background: rgba(255,59,48,0.08); border-color: var(--danger); }
.btn.ghost { background: transparent; border-color: transparent; color: var(--text-muted); }
.btn.ghost:hover { background: var(--border-light); color: var(--text); }
.btn-lg { padding: 12px 24px; font-size: 15px; }
.btn-sm { padding: 5px 12px; font-size: 12px; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

/* ----- Dropzone ----- */
.dropzone {
  display: flex; align-items: center; justify-content: center;
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  padding: 36px 20px;
  cursor: pointer;
  text-align: center;
  background: var(--surface-2);
  transition: border-color .15s, background .15s;
}
.dropzone:hover { border-color: var(--accent); background: rgba(10,132,255,0.04); }
.dropzone.drag-over { border-color: var(--accent); background: rgba(10,132,255,0.08); }
.dz-title { font-size: 15px; font-weight: 500; margin-bottom: 4px; }
.dz-sub { font-size: 13px; }
.file-name { margin-top: 12px; font-size: 13px; color: var(--text); font-weight: 500; }

/* ----- Source link / advanced details ----- */
.src-link { margin: 14px 0 0; }
.src-link summary {
  cursor: pointer; padding: 10px 14px;
  background: var(--surface-2); border: 1px solid var(--border-light);
  border-radius: var(--radius-sm); font-size: 13px; color: var(--text-muted);
  list-style: none; transition: background .15s;
}
.src-link summary::-webkit-details-marker { display: none; }
.src-link summary:hover { background: var(--border-light); }
.src-link[open] summary { color: var(--text); border-bottom-left-radius: 0; border-bottom-right-radius: 0; }
.src-link-body {
  padding: 14px;
  background: var(--surface-2);
  border: 1px solid var(--border-light); border-top: none;
  border-bottom-left-radius: var(--radius-sm); border-bottom-right-radius: var(--radius-sm);
}

/* ----- Form rows ----- */
.row {
  display: grid; grid-template-columns: 1fr 1fr; gap: 16px;
}
@media (max-width: 600px) {
  .row { grid-template-columns: 1fr; }
}
.field label {
  display: block; font-size: 13px; color: var(--text-muted);
  margin-bottom: 6px; font-weight: 500;
}
.hint { margin: 6px 0 0; font-size: 12px; }

/* ----- Status / errors / progress ----- */
#status-block { margin-top: 14px; }
.status, .error {
  padding: 12px 16px;
  border-radius: var(--radius-sm);
  font-size: 14px;
}
.status { background: rgba(10,132,255,0.10); color: var(--accent); }
.status.is-loading::after {
  content: "";
  display: inline-block;
  width: 12px; height: 12px;
  border: 2px solid currentColor;
  border-top-color: transparent;
  border-radius: 50%;
  margin-left: 10px;
  animation: spin 0.7s linear infinite;
  vertical-align: middle;
}
@keyframes spin { to { transform: rotate(360deg); } }
.error { background: rgba(255,59,48,0.10); color: var(--danger); margin-top: 14px; }
.status-meta {
  display: flex; align-items: center; justify-content: space-between;
  margin-top: 10px; gap: 10px;
}
.status-elapsed {
  font-variant-numeric: tabular-nums;
  font-size: 13px; color: var(--text-muted);
}

/* ----- Result text ----- */
.result-text {
  background: var(--surface-2);
  border: 1px solid var(--border-light);
  border-radius: var(--radius);
  padding: 16px 18px;
  font-family: var(--font);
  font-size: 14px;
  line-height: 1.6;
  white-space: pre-wrap;
  word-wrap: break-word;
  margin: 0;
  max-height: 480px;
  overflow-y: auto;
}
.view-text { font-size: 14px; max-height: none; }

/* ----- Saved badge ----- */
.saved-badge {
  display: inline-flex; align-items: center; gap: 8px;
  background: var(--success-bg); color: var(--success-text);
  padding: 8px 14px; border-radius: 999px;
  font-size: 13px; font-weight: 500;
  margin-bottom: 12px;
  border: 1px solid rgba(48,209,88,0.25);
}
.saved-badge::before { content: "✓"; font-weight: 700; }

.cost-line {
  display: inline-flex; align-items: center; gap: 6px;
  background: var(--success-bg); color: var(--success-text);
  padding: 6px 12px; border-radius: 999px;
  font-size: 13px; font-weight: 500;
  margin-bottom: 12px; margin-left: 8px;
}

/* ----- Оценка полноты транскрипта (quality.py) ----- */
.quality-line {
  display: block;
  padding: 8px 12px; border-radius: 10px;
  font-size: 13px; line-height: 1.4;
  margin: 4px 0 12px;
  border: 1px solid transparent;
}
.quality-ok      { background: #e8f5e9; color: #1b5e20; border-color: #c8e6c9; }
.quality-suspect { background: #fff8e1; color: #8d6e00; border-color: #ffe082; }
.quality-low     { background: #fdecea; color: #b71c1c; border-color: #f5c6cb; }
.quality-partial { background: #fdecea; color: #b71c1c; border-color: #f5c6cb; }
@media (prefers-color-scheme: dark) {
  .quality-ok      { background: #17301a; color: #a5d6a7; border-color: #2e5b33; }
  .quality-suspect { background: #332a10; color: #ffd54f; border-color: #5c4a12; }
  .quality-low,
  .quality-partial { background: #331a1a; color: #ef9a9a; border-color: #5c2626; }
}

/* ----- Text search bar ----- */
.text-search-bar {
  display: flex; gap: 8px; align-items: center;
  margin-bottom: 12px;
}
.text-search-bar input { flex: 1; padding: 8px 12px; font-size: 13px; }
.text-search-bar #text-search-count { font-size: 12px; min-width: 60px; text-align: right; }
mark.text-search-hit { background: var(--highlight); color: inherit; border-radius: 2px; padding: 0 2px; }
mark.text-search-hit.is-active { background: var(--highlight-active); }

/* ----- View layout with side chat ----- */
.view-layout { display: grid; grid-template-columns: 1fr; gap: 16px; }
.view-layout.with-chat { grid-template-columns: 1fr; }
.view-layout.with-chat .view-text { display: none; }

/* ----- Chat panel ----- */
.chat-panel {
  display: flex; flex-direction: column;
  background: var(--surface-2);
  border: 1px solid var(--border-light);
  border-radius: var(--radius);
  overflow: hidden;
  min-height: 400px;
  max-height: calc(100dvh - 220px);
  height: auto;
  /* Анимация открытия */
  opacity: 1; transform: translateY(0);
  transition: opacity .2s ease, transform .2s ease;
}
.chat-panel.is-closing { opacity: 0; transform: translateY(8px); }

.chat-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 14px;
  border-bottom: 1px solid var(--border-light);
  background: var(--surface);
  gap: 10px;
}
.chat-title { font-weight: 600; font-size: 14px; }
.chat-sub { font-size: 12px; }
.chat-head-actions { display: flex; align-items: center; gap: 6px; }
.chat-model {
  font-size: 12px; padding: 5px 8px; border-radius: 6px; max-width: 140px;
}

.chat-messages {
  flex: 1; overflow-y: auto;
  padding: 14px;
  display: flex; flex-direction: column; gap: 10px;
  min-height: 240px;
}
.chat-empty { color: var(--text-muted); font-size: 13px; text-align: center; padding: 20px 8px; }
.chat-msg {
  max-width: 90%;
  padding: 10px 14px;
  border-radius: 14px;
  font-size: 14px;
  line-height: 1.5;
  white-space: pre-wrap;
  word-wrap: break-word;
}
.chat-msg.user {
  align-self: flex-end;
  background: var(--accent); color: #fff;
  border-bottom-right-radius: 4px;
}
.chat-msg.assistant {
  align-self: flex-start;
  background: var(--surface); border: 1px solid var(--border-light);
  border-bottom-left-radius: 4px;
  max-width: 96%;
}
.chat-msg-meta {
  margin-top: 6px;
  font-size: 11px;
  color: var(--text-muted);
}
.chat-msg.user .chat-msg-meta { color: rgba(255,255,255,0.75); }

.chat-msg-body { white-space: normal; }
.chat-msg.user .chat-msg-body { white-space: pre-wrap; }
.chat-md-line { margin: 0 0 4px; }
.chat-md-line:last-child { margin-bottom: 0; }
.chat-md-list { margin: 4px 0 6px; padding-left: 22px; }
.chat-md-list li { margin: 2px 0; }
.chat-msg-body code {
  background: var(--border-light); padding: 1px 5px;
  border-radius: 4px; font-size: 12.5px;
}

.chat-msg-actions { margin-top: 8px; display: flex; gap: 6px; align-items: center; }
.chat-msg-copy {
  background: transparent; border: 1px solid var(--border-light);
  color: var(--text-muted); padding: 3px 9px; border-radius: 6px;
  font-size: 11px; cursor: pointer; font-family: inherit;
  transition: background .15s, color .15s;
}
.chat-msg-copy:hover { background: var(--border-light); color: var(--text); }

/* Model badges (цветные по семейству) */
.model-badge {
  display: inline-block;
  padding: 1px 7px; border-radius: 999px;
  font-size: 10.5px; font-weight: 600; letter-spacing: 0.02em;
  text-transform: lowercase;
  color: #fff; margin-left: 4px;
}
.model-badge.gemini { background: #1a73e8; }
.model-badge.claude { background: #c96442; }
.model-badge.gpt { background: #10a37f; }
.model-badge.other { background: var(--text-muted); }

.chat-presets {
  display: flex; gap: 6px; padding: 8px 12px 0; flex-wrap: wrap;
  border-top: 1px solid var(--border-light);
  background: var(--surface);
}
.chip {
  background: var(--border-light);
  border: none;
  padding: 6px 12px;
  border-radius: 999px;
  font-size: 12px;
  cursor: pointer;
  font-family: inherit;
  color: var(--text);
  transition: background .15s;
}
.chip:hover { background: var(--border); }

.chat-form {
  display: flex; gap: 8px;
  padding: 10px 12px 12px;
  background: var(--surface);
  align-items: flex-end;
}
.chat-form textarea {
  flex: 1;
  resize: none;
  padding: 9px 12px;
  font-size: 13px;
  border-radius: 10px;
  font-family: inherit;
  background: var(--surface);
  border: 1px solid var(--border);
}
.chat-form button { padding: 9px 16px; }

.chat-thinking {
  align-self: flex-start;
  color: var(--text-muted);
  font-size: 13px;
  font-style: italic;
}

/* ----- Projects list ----- */
.projects-list { display: flex; flex-direction: column; gap: 8px; }
.project-item {
  display: flex; align-items: flex-start; justify-content: space-between;
  padding: 14px 16px;
  background: var(--surface-2);
  border: 1px solid var(--border-light);
  border-radius: var(--radius);
  cursor: pointer;
  transition: background .15s, border-color .15s;
  gap: 12px;
}
.project-item:hover { background: var(--surface); border-color: var(--border); box-shadow: var(--shadow-sm); }
.project-meta { display: flex; flex-direction: column; gap: 4px; flex: 1; min-width: 0; }
.project-title { font-weight: 500; font-size: 15px; }
.project-sub { font-size: 12px; color: var(--text-muted); }
.project-preview {
  font-size: 13px; color: var(--text-muted);
  margin-top: 4px;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  line-height: 1.45;
}
.project-actions { display: flex; gap: 6px; }

/* ----- Modal ----- */
.modal {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.45);
  display: flex; align-items: center; justify-content: center;
  z-index: 100;
  padding: 16px;
  animation: modal-in .15s ease-out;
}
@keyframes modal-in { from { opacity: 0; } }
.modal-card {
  background: var(--surface);
  border-radius: var(--radius-lg);
  padding: 28px;
  width: min(440px, 100%);
  box-shadow: var(--shadow-lg);
  border: 1px solid var(--border-light);
}
.modal-card h3 { margin-bottom: 6px; }
.modal-card input { margin: 14px 0 18px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; }

/* ----- Toast ----- */
.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
  background: rgba(29,29,31,0.94);
  color: #fff; padding: 10px 18px;
  border-radius: 999px; font-size: 14px;
  z-index: 200;
  animation: toast-in 0.2s ease-out;
  cursor: pointer;
  max-width: 90vw;
}
@keyframes toast-in { from { opacity: 0; transform: translate(-50%, 8px); } }
@media (prefers-color-scheme: dark) {
  .toast { background: rgba(242,242,247,0.94); color: #1d1d1f; }
}

/* ----- Responsive ----- */
@media (max-width: 720px) {
  .topbar { padding: 12px 16px; }
  .container { padding: 18px 14px 60px; }
  .card { padding: 18px 16px; }
  .nav-btn { padding: 7px 10px; font-size: 13px; }
  .card-actions .btn { padding: 7px 12px; font-size: 13px; }
  .text-search-bar { flex-wrap: wrap; }
}
