/*
 * Оформление мини-аппа DSH Relay.
 *
 * Дизайн повторяет интерфейс DeepSeek Harness, снятый с живого GUI
 * (scripts/dsh-shot.mjs): те же поверхности, акцент, радиусы и типографика,
 * но разложенные под вертикальный экран телефона.
 *
 * Палитра из снимков DSH:
 *   светлая: список #f9fafb · переписка #ffffff · рамка #e9eaec · акцент #4d6bfe
 *   тёмная:  список #1b1b1c · переписка #151517 · рамка #2c2c2e · акцент #4d6bfe
 *
 * ⚠️ Порядок приоритетов цвета: переменная темы мессенджера → палитра DSH.
 * Мост мессенджера раскладывает тему в CSS-переменные на <html>, в двух
 * семействах сразу (--ms-theme-* и --tg-theme-*), но их может не быть вовсе
 * (открыто вне мессенджера), поэтому запасное значение в var() обязательно.
 * ⚠️ В именах переменных моста ДЕФИСЫ, а не подчёркивания.
 */

/* ── Палитра ─────────────────────────────────────────────────────────────── */

:root {
  /* Поверхности */
  --bg: var(--ms-theme-bg-color, #ffffff);
  --bg-rail: var(--ms-theme-secondary-bg-color, #f9fafb);
  --bg-soft: var(--ms-theme-secondary-bg-color, #f4f4f5);
  --bg-hover: #f2f3f5;
  --border: var(--ms-theme-hint-color, #e9eaec);
  --border-strong: #d9dade;

  /* Текст */
  --text: var(--ms-theme-text-color, #17181a);
  --text-muted: var(--ms-theme-hint-color, #8a8f98);
  --text-faint: #a8adb6;

  /* Акцент и состояния */
  --accent: var(--ms-theme-button-color, #4d6bfe);
  --accent-text: var(--ms-theme-button-text-color, #ffffff);
  --accent-soft: #eef1fe;
  --ok: #17a34a;
  --ok-soft: #e8f7ee;
  --warn: #b06a00;
  --warn-soft: #fdf3e2;
  --danger: #dc2626;
  --danger-soft: #fdeceb;

  /* Геометрия: радиусы и ритм DSH */
  --r-sm: 6px;
  --r-md: 10px;
  --r-lg: 14px;
  --r-xl: 18px;
  --r-pill: 999px;
  --gap: 8px;

  --shadow: 0 1px 2px rgba(16, 18, 22, .04), 0 6px 20px rgba(16, 18, 22, .06);

  --font: var(--ms-theme-font-family, -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, "Helvetica Neue", sans-serif);
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
}

/* Тёмная палитра DSH. Мост мессенджера перекрывает её своими переменными,
   поэтому она работает и как системная, и как запасная. */
@media (prefers-color-scheme: dark) {
  :root {
    --bg: var(--ms-theme-bg-color, #151517);
    --bg-rail: var(--ms-theme-secondary-bg-color, #1b1b1c);
    --bg-soft: var(--ms-theme-secondary-bg-color, #232326);
    --bg-hover: #26262a;
    --border: var(--ms-theme-hint-color, #2c2c2e);
    --border-strong: #3a3a3f;

    --text: var(--ms-theme-text-color, #ececee);
    --text-muted: var(--ms-theme-hint-color, #8b8f98);
    --text-faint: #6a6e77;

    --accent: var(--ms-theme-button-color, #4d6bfe);
    --accent-text: var(--ms-theme-button-text-color, #ffffff);
    --accent-soft: #1e2540;
    --ok: #3ecf74;
    --ok-soft: #16281d;
    --warn: #e0a35a;
    --warn-soft: #2b2314;
    --danger: #f87171;
    --danger-soft: #2c1a1a;

    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 8px 24px rgba(0, 0, 0, .35);
  }
}

/* ── Основа ─────────────────────────────────────────────────────────────── */

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 var(--font);
  -webkit-text-size-adjust: 100%;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: contain;
}

body {
  display: flex;
  flex-direction: column;
  min-height: 100%;
}

svg { fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }

.banner {
  padding: 10px 14px;
  background: var(--warn-soft);
  color: var(--warn);
  font-size: 13px;
  border-bottom: 1px solid var(--border);
}

.screen {
  display: flex;
  flex-direction: column;
  flex: 1 1 auto;
  min-height: 0;
  height: 100%;
}

.screen[hidden] { display: none; }

.hint { color: var(--text-muted); font-size: 12.5px; }

code {
  font: 12.5px var(--mono);
  background: var(--bg-soft);
  border-radius: var(--r-sm);
  padding: 1px 5px;
}

/* ── Верхняя панель ─────────────────────────────────────────────────────── */

.app-bar {
  display: flex;
  align-items: center;
  gap: 2px;
  padding: 6px 6px;
  min-height: 48px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
  position: sticky;
  top: 0;
  z-index: 6;
}

.icon-btn {
  flex: 0 0 auto;
  display: grid;
  place-items: center;
  width: 38px;
  height: 38px;
  border: none;
  border-radius: var(--r-md);
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  transition: background .14s ease, color .14s ease;
}

.icon-btn:active { background: var(--bg-soft); color: var(--text); }
.icon-btn.ghost { pointer-events: none; }

.app-title {
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  padding: 7px 10px;
  border: none;
  border-radius: var(--r-md);
  background: transparent;
  color: var(--text);
  font: 600 15px var(--font);
  cursor: pointer;
}

.app-title:not(.static):active { background: var(--bg-soft); }
.app-title.static { cursor: default; }

.app-title-text {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chev { color: var(--text-faint); flex: 0 0 auto; }

.status-dot {
  flex: 0 0 auto;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--text-faint);
}

.status-dot.online { background: var(--ok); box-shadow: 0 0 0 3px var(--ok-soft); }
.status-dot.running { background: var(--accent); animation: pulse 1.6s ease-in-out infinite; }

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: .35; }
}

.sub-bar {
  padding: 5px 14px 7px;
  color: var(--text-muted);
  font-size: 12px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ── Поиск ──────────────────────────────────────────────────────────────── */

.search-row {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 10px 6px;
  background: var(--bg-rail);
}

.search {
  flex: 1 1 auto;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 10px;
  height: 38px;
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  background: var(--bg);
  color: var(--text-muted);
}

.search input {
  flex: 1 1 auto;
  min-width: 0;
  border: none;
  background: transparent;
  color: var(--text);
  font: 14.5px var(--font);
  outline: none;
}

.search input::placeholder { color: var(--text-faint); }

/* ── Списки и карточки ──────────────────────────────────────────────────── */

.list {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 6px 8px 24px;
  background: var(--bg-rail);
}

.section-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  padding: 12px 14px 6px;
  color: var(--text-muted);
  font-size: 12px;
  font-weight: 600;
  letter-spacing: .02em;
  text-transform: uppercase;
}

.counter { font-weight: 500; letter-spacing: 0; text-transform: none; }

.row {
  display: flex;
  gap: 10px;
  align-items: center;
  padding: 11px 12px;
  margin: 2px 2px;
  border-radius: var(--r-lg);
  background: transparent;
  cursor: pointer;
  transition: background .14s ease;
}

.row:active { background: var(--bg-hover); }

.row.current {
  background: var(--bg);
  box-shadow: var(--shadow);
}

.row-main { flex: 1 1 auto; min-width: 0; }

.row-title {
  display: flex;
  align-items: center;
  gap: 7px;
  font-size: 14.5px;
  font-weight: 600;
}

.row-title .name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.row-sub {
  margin-top: 3px;
  color: var(--text-muted);
  font-size: 12.5px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.row-badges {
  display: flex;
  gap: 5px;
  margin-top: 6px;
  flex-wrap: wrap;
}

.row.child { padding-left: 34px; }
.row.child .row-title { font-weight: 500; color: var(--text-muted); }

.badge {
  display: inline-flex;
  align-items: center;
  gap: 3px;
  padding: 1px 7px;
  border-radius: var(--r-pill);
  background: var(--bg-soft);
  color: var(--text-muted);
  font-size: 11px;
  line-height: 1.7;
  white-space: nowrap;
}

.badge.ok { background: var(--ok-soft); color: var(--ok); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.err { background: var(--danger-soft); color: var(--danger); }
.badge.accent { background: var(--accent-soft); color: var(--accent); }

.card {
  margin: 8px 6px;
  padding: 14px;
  border: 1px solid var(--border);
  border-radius: var(--r-xl);
  background: var(--bg);
}

.card-title { margin: 0 0 6px; font-size: 15px; font-weight: 600; }
.card p { margin: 0 0 12px; }

.btn {
  width: 100%;
  padding: 11px 14px;
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  background: var(--bg);
  color: var(--text);
  font: 600 14.5px var(--font);
  cursor: pointer;
  transition: background .14s ease;
}

.btn:active { background: var(--bg-soft); }
.btn[disabled] { opacity: .55; }

.btn.primary {
  border-color: transparent;
  background: var(--accent);
  color: var(--accent-text);
}

.pair { margin-top: 12px; text-align: center; }

.pair-value {
  font: 600 32px/1.2 var(--mono);
  letter-spacing: 4px;
  color: var(--text);
}

.empty {
  padding: 44px 26px;
  text-align: center;
  color: var(--text-muted);
  font-size: 14px;
}

.empty h2 { margin: 0 0 6px; font-size: 16px; color: var(--text); }
.empty p { margin: 4px 0 12px; }

/* ── Лента переписки ────────────────────────────────────────────────────── */

.feed {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 14px 12px 10px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  background: var(--bg);
}

.bubble {
  max-width: 88%;
  padding: 10px 13px;
  border-radius: var(--r-lg);
  font-size: 14.5px;
  line-height: 1.55;
  word-wrap: break-word;
  overflow-wrap: anywhere;
  white-space: pre-wrap;
}

.bubble.user {
  align-self: flex-end;
  background: var(--accent-soft);
  color: var(--text);
  border-bottom-right-radius: var(--r-sm);
}

.bubble.assistant {
  align-self: flex-start;
  background: var(--bg-soft);
  border-bottom-left-radius: var(--r-sm);
}

.bubble.attempt { opacity: .75; }

.bubble-meta {
  margin-top: 6px;
  font-size: 11px;
  color: var(--text-muted);
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.bubble pre {
  margin: 8px 0;
  padding: 10px 11px;
  border-radius: var(--r-md);
  background: rgba(127, 127, 127, .13);
  overflow-x: auto;
  font: 12.5px/1.5 var(--mono);
  white-space: pre;
}

.bubble code { font: 12.5px var(--mono); background: rgba(127, 127, 127, .13); }

.bubble img {
  display: block;
  max-width: 100%;
  margin-top: 7px;
  border-radius: var(--r-md);
}

/* Карточки инструментов: как в DSH — сдержанная рамка, детали под катом. */
.card-line {
  align-self: flex-start;
  width: 100%;
  max-width: 94%;
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  background: var(--bg);
  font-size: 13px;
}

.card-head {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--text-muted);
}

.card-name { color: var(--text); font-weight: 600; font-family: var(--mono); font-size: 12.5px; }

.card-line details { margin-top: 7px; }

.card-line summary {
  cursor: pointer;
  color: var(--text-muted);
  font-size: 12px;
  list-style: none;
}

.card-line summary::-webkit-details-marker { display: none; }
.card-line summary::before { content: "▸ "; }

.card-line pre {
  max-height: 280px;
  overflow: auto;
  margin: 7px 0 0;
  padding: 9px;
  border-radius: var(--r-sm);
  background: var(--bg-soft);
  font: 12px/1.5 var(--mono);
}

.separator {
  align-self: center;
  padding: 2px 10px;
  border-radius: var(--r-pill);
  background: var(--bg-soft);
  color: var(--text-muted);
  font-size: 11px;
}

.notice {
  align-self: stretch;
  padding: 9px 12px;
  border-radius: var(--r-md);
  background: var(--warn-soft);
  color: var(--warn);
  font-size: 13px;
  white-space: pre-wrap;
}

.notice.ok { background: var(--ok-soft); color: var(--ok); }
.notice.info { background: var(--bg-soft); color: var(--text-muted); }

.scroll-bottom {
  position: absolute;
  right: 16px;
  bottom: 168px;
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 7px 12px;
  border: 1px solid var(--border);
  border-radius: var(--r-pill);
  background: var(--bg);
  color: var(--accent);
  font-size: 12px;
  font-weight: 600;
  box-shadow: var(--shadow);
  z-index: 7;
  cursor: pointer;
}

/* ── Очередь ────────────────────────────────────────────────────────────── */

.queue {
  max-height: 32vh;
  overflow-y: auto;
  padding: 9px 12px;
  border-top: 1px solid var(--border);
  background: var(--bg-rail);
}

.queue-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: var(--text-muted);
  font-size: 12px;
  font-weight: 600;
}

.queue-items { display: flex; flex-direction: column; gap: 6px; margin-top: 7px; }

.queue-item {
  display: flex;
  gap: 8px;
  align-items: flex-start;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  background: var(--bg);
  font-size: 13px;
}

.qi-main { flex: 1 1 auto; min-width: 0; }

.qi-text {
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.link-btn {
  border: none;
  background: none;
  color: var(--accent);
  font: 500 12.5px var(--font);
  padding: 2px 0;
  cursor: pointer;
}

/* ── Композер (повторяет поле ввода DSH) ────────────────────────────────── */

.composer-wrap {
  padding: 8px 10px calc(8px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
  background: var(--bg);
}

.composer {
  padding: 10px 12px 8px;
  border: 1px solid var(--border);
  border-radius: var(--r-xl);
  background: var(--bg);
  transition: border-color .14s ease, box-shadow .14s ease;
}

.composer:focus-within {
  border-color: var(--border-strong);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.composer textarea {
  width: 100%;
  max-height: 132px;
  border: none;
  background: transparent;
  color: var(--text);
  font: 15px/1.5 var(--font);
  resize: none;
  outline: none;
  padding: 0;
}

.composer textarea::placeholder { color: var(--text-faint); }

.composer-foot {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-top: 8px;
}

.foot-left, .foot-right { display: flex; align-items: center; gap: 6px; min-width: 0; }

.tool-btn {
  display: grid;
  place-items: center;
  width: 32px;
  height: 32px;
  border: none;
  border-radius: var(--r-md);
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
}

.tool-btn:active { background: var(--bg-soft); color: var(--text); }

.chip {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  max-width: 46vw;
  padding: 5px 9px;
  border: none;
  border-radius: var(--r-md);
  background: transparent;
  color: var(--text-muted);
  font: 500 12.5px var(--font);
  cursor: pointer;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.chip:active { background: var(--bg-soft); }
.chip.ghost { pointer-events: none; cursor: default; }
.chip.active { background: var(--warn-soft); color: var(--warn); }

.send-btn {
  display: grid;
  place-items: center;
  width: 34px;
  height: 34px;
  border: none;
  border-radius: 50%;
  background: var(--accent);
  color: var(--accent-text);
  cursor: pointer;
  transition: opacity .14s ease, transform .1s ease;
}

.send-btn[disabled] { opacity: .4; }
.send-btn:active { transform: scale(.94); }

.composer-hint {
  padding: 6px 4px 0;
  min-height: 0;
  font-size: 12px;
}

.attachments {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding-bottom: 8px;
}

.attachment {
  position: relative;
  flex: 0 0 auto;
  width: 64px;
  height: 64px;
  border-radius: var(--r-md);
  overflow: hidden;
  background: var(--bg-soft);
}

.attachment img { width: 100%; height: 100%; object-fit: cover; display: block; }

.attachment button {
  position: absolute;
  top: 3px;
  right: 3px;
  width: 21px;
  height: 21px;
  border: none;
  border-radius: 50%;
  background: rgba(0, 0, 0, .6);
  color: #fff;
  font-size: 12px;
  line-height: 1;
  cursor: pointer;
}

/* ── Шторки ─────────────────────────────────────────────────────────────── */

.sheet {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: flex;
  align-items: flex-end;
  background: rgba(16, 18, 22, .38);
  backdrop-filter: blur(2px);
}

.sheet[hidden] { display: none; }

.sheet-body {
  width: 100%;
  max-height: 74vh;
  overflow-y: auto;
  padding: 10px 12px calc(14px + env(safe-area-inset-bottom));
  border-radius: var(--r-xl) var(--r-xl) 0 0;
  background: var(--bg);
  animation: rise .18s ease-out;
}

@keyframes rise {
  from { transform: translateY(12px); opacity: .6; }
  to { transform: translateY(0); opacity: 1; }
}

.sheet-grip {
  width: 36px;
  height: 4px;
  margin: 2px auto 12px;
  border-radius: var(--r-pill);
  background: var(--border-strong);
}

.sheet-item {
  display: flex;
  align-items: center;
  gap: 11px;
  width: 100%;
  padding: 12px 12px;
  margin-bottom: 6px;
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  background: var(--bg);
  color: var(--text);
  font: 14.5px var(--font);
  text-align: left;
  cursor: pointer;
}

.sheet-item:active { background: var(--bg-soft); }
.sheet-item.danger { color: var(--danger); }
.sheet-item.current { border-color: var(--accent); background: var(--accent-soft); }
.sheet-item .sheet-sub { display: block; margin-top: 2px; color: var(--text-muted); font-size: 12px; }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(26px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  max-width: 88%;
  padding: 10px 15px;
  border-radius: var(--r-pill);
  background: rgba(23, 24, 26, .94);
  color: #fff;
  font-size: 13px;
  z-index: 30;
  box-shadow: var(--shadow);
}

.toast[hidden] { display: none; }

@media (prefers-color-scheme: dark) {
  .toast { background: rgba(240, 240, 242, .95); color: #17181a; }
}

.spinner {
  display: inline-block;
  width: 12px;
  height: 12px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .8s linear infinite;
  vertical-align: -2px;
}

@keyframes spin { to { transform: rotate(360deg); } }

@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}
