:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #eceef2;
  --text: #16181d;
  --muted: #646b78;
  --border: #dfe2e7;
  --accent: #2459d6;
  --accent-text: #ffffff;
  --danger: #c62828;
  --stage: #e6e8ec;
  --shadow: 0 8px 28px rgba(15, 20, 30, 0.16);
  --radius: 12px;
  --bar-h: 52px;
  font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #14171c;
    --surface: #1c2027;
    --surface-2: #252a33;
    --text: #e7e9ee;
    --muted: #9aa2b0;
    --border: #2d323c;
    --accent: #5b8cff;
    --accent-text: #0b1020;
    --danger: #ff6b6b;
    --stage: #0d0f13;
    --shadow: 0 8px 28px rgba(0, 0, 0, 0.5);
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #14171c;
  --surface: #1c2027;
  --surface-2: #252a33;
  --text: #e7e9ee;
  --muted: #9aa2b0;
  --border: #2d323c;
  --accent: #5b8cff;
  --accent-text: #0b1020;
  --danger: #ff6b6b;
  --stage: #0d0f13;
  --shadow: 0 8px 28px rgba(0, 0, 0, 0.5);
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  overscroll-behavior: none;
}

[hidden] { display: none !important; }

button, input, select, textarea { font: inherit; color: inherit; }

button { cursor: pointer; }

/* Приложение занимает видимую область: на iOS клавиатура уменьшает visualViewport,
   и мы подстраиваемся под него (переменные выставляет app.js). */
#app {
  position: fixed;
  left: 0;
  right: 0;
  top: var(--app-top, 0px);
  height: var(--app-h, 100dvh);
  display: flex;
  flex-direction: column;
}
/* Старые браузеры (Safari 15.0–15.3, Chrome до 108) не знают dvh: без запасного варианта
   высота окна схлопнулась бы в ноль и страница была бы пустой. */
@supports not (height: 100dvh) {
  #app { height: var(--app-h, 100vh); }
}

/* ---- панель адреса ---- */

#bar {
  display: none !important;
}

.icon-button {
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  flex: none;
  border: 0;
  border-radius: 10px;
  background: transparent;
  color: var(--text);
}

.icon-button:hover:not(:disabled) { background: var(--surface-2); }
.icon-button:disabled { color: var(--muted); opacity: 0.45; cursor: default; }
.icon-button.small { width: 32px; height: 32px; border-radius: 8px; }

.icon-button svg, #lock {
  width: 22px;
  height: 22px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

#menu-button svg circle { fill: currentColor; stroke: none; }

#address-form {
  position: relative;
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  height: 40px;
  padding: 0 4px 0 12px;
  background: var(--surface-2);
  border-radius: 20px;
  overflow: hidden;
}

#address-form:focus-within { box-shadow: 0 0 0 2px var(--accent); }

#lock { width: 15px; height: 15px; margin-right: 6px; color: var(--muted); flex: none; }
#address-form:not(.secure) #lock { display: none; }

#address {
  flex: 1;
  min-width: 0;
  height: 100%;
  border: 0;
  outline: 0;
  background: transparent;
  font-size: 16px; /* меньше 16px — iOS зумит страницу при фокусе */
}

#reload .when-loading { display: none; }
#address-form.loading #reload .when-loading { display: block; }
#address-form.loading #reload .when-idle { display: none; }

#progress {
  position: absolute;
  left: 0;
  bottom: 0;
  height: 2px;
  width: 0;
  background: var(--accent);
  opacity: 0;
  transition: width 0.4s ease, opacity 0.3s ease;
}

#address-form.loading #progress { width: 70%; opacity: 1; transition: width 8s cubic-bezier(0.1, 0.7, 0.2, 1), opacity 0.2s; }

#tab-count {
  display: grid;
  place-items: center;
  min-width: 22px;
  height: 22px;
  padding: 0 4px;
  border: 2px solid currentColor;
  border-radius: 6px;
  font-size: 12px;
  font-weight: 700;
  line-height: 1;
}

@media (max-width: 480px) {
  .wide-only { display: none; }
  .icon-button { width: 36px; }
}

/* ---- полноэкранный режим: страница на весь экран, панель выезжает поверх ----
   Вызывается без кнопок: мышью к верхнему краю, Ctrl+L или свайпом вниз от верхнего края. */

/* Сцена — только в безопасной зоне: в альбомной ориентации iPhone слева и справа вырез и
   скруглённые углы, снизу — полоска «домой». Поля красятся в фон сайта (правило ниже). */
#app.immersive, #app.idle {
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}
/* Панель закреплена (настройка «Прятать панель» выключена): сверху отступ даёт она сама, а по
   бокам и снизу — сцене. */
#app:not(.immersive):not(.idle) #stage {
  margin: 0 env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}
#app.idle #bar { display: none; }

/* Во время сессии всё, что не закрыто картинкой (края, место под уезжающей клавиатурой),
   красится в фон открытого сайта. */
html:has(#app.immersive), html:has(#app.immersive) body, #app.immersive #stage {
  background: var(--page-bg, var(--stage));
}

#app.immersive #bar {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  z-index: 12;
  transform: translateY(-100%);
  /* Спрятанная панель невидима целиком: иначе её тень ложилась бы серой полосой на верх
     страницы, а кнопки оставались бы в порядке Tab. */
  visibility: hidden;
  transition: transform 0.18s ease, visibility 0s 0.18s;
}

#app.immersive.bar-open #bar {
  transform: none;
  visibility: visible;
  box-shadow: var(--shadow);
  transition: transform 0.18s ease;
}

/* Индикатор загрузки, пока панель спрятана. */
#stage-progress {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 5;
  height: 2px;
  width: 0;
  background: var(--accent);
  opacity: 0;
  pointer-events: none;
  transition: width 0.4s ease, opacity 0.3s ease;
}

#app.immersive.loading:not(.bar-open) #stage-progress {
  width: 70%;
  opacity: 1;
  transition: width 8s cubic-bezier(0.1, 0.7, 0.2, 1), opacity 0.2s;
}

/* ---- сцена с удалённой страницей ---- */

#stage {
  position: relative;
  flex: 1;
  min-height: 0;
  overflow: hidden;
  background: var(--stage);
}

#screen {
  position: absolute;
  display: block;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  outline: none;
  background: #fff;
}

#field-layer {
  position: absolute;
  inset: 0;
  pointer-events: none;
  overflow: hidden;
}

/* Нативное поле поверх удалённого: его стиль копирует оригинал (задаётся из ime.js). */
.native-field {
  position: absolute;
  margin: 0;
  padding: 0;
  border: 0;
  border-radius: 0;
  outline: none;
  appearance: none;
  -webkit-appearance: none;
  box-shadow: none;
  resize: none;
  pointer-events: auto;
  text-overflow: clip;
}

/* Пока сервер не прислал стиль поля — поле невидимо, но уже держит фокус и клавиатуру. */
.native-field.pending { color: transparent; background: transparent; caret-color: transparent; }
.native-field.ghost { opacity: 0.011; }

#keys {
  position: absolute;
  left: 0;
  bottom: 0;
  width: 1px;
  height: 1px;
  padding: 0;
  border: 0;
  opacity: 0;
  resize: none;
  font-size: 16px;
  pointer-events: none;
}

#legacy {
  position: fixed;
  left: 16px;
  right: 16px;
  top: 40%;
  padding: 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  text-align: center;
  box-shadow: var(--shadow);
}

#spinner {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  pointer-events: none;
}

#spinner span {
  width: 34px;
  height: 34px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

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

/* Лоадер поверх окна: прячет процесс переключения на ПК-версию и открытия Messenger. */
#overlay {
  position: absolute;
  inset: 0;
  z-index: 40;
  display: grid;
  place-items: center;
  background: var(--bg);
}
#overlay[hidden] { display: none; }
.overlay-box {
  display: grid;
  justify-items: center;
  gap: 16px;
  padding: 24px;
  text-align: center;
}
.overlay-spin {
  width: 40px;
  height: 40px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
#overlay-text {
  margin: 0;
  font-size: 15px;
  color: var(--muted);
}

/* ---- стартовый и финальный экраны ---- */

.card-screen {
  position: absolute;
  inset: 0;
  display: flex;
  padding: 16px;
  overflow-y: auto;
  background: var(--bg);
}

/* margin: auto центрирует, но, в отличие от place-items, не обрезает верх карточки
   на низких экранах — её можно прокрутить. */
.card {
  margin: auto;
  width: 100%;
  max-width: 460px;
  padding: 24px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
}

h1, h2 { margin: 0; color: var(--text); font-size: 22px; line-height: 1.25; }
.lead { color: var(--muted); margin: 10px 0 18px; }

.field { display: grid; gap: 6px; margin: 0 0 14px; }
.field > span { font-size: 13px; color: var(--muted); }
.field select, .field input {
  width: 100%;
  height: 42px;
  padding: 0 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  font-size: 16px;
}

button.primary, button.danger, #new-tab {
  width: 100%;
  height: 44px;
  border: 0;
  border-radius: 10px;
  font-weight: 600;
}
button.primary { background: var(--accent); color: var(--accent-text); }
button.danger { background: transparent; color: var(--danger); border: 1px solid var(--border); }

/* ---- меню и вкладки ---- */

.panel {
  position: absolute;
  top: calc(var(--bar-h) + env(safe-area-inset-top) + 6px);
  right: max(8px, env(safe-area-inset-right));
  z-index: 10;
  width: min(330px, calc(100vw - 16px));
  max-height: calc(100% - var(--bar-h) - 24px);
  overflow-y: auto;
  padding: 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.check { display: flex; align-items: center; gap: 10px; margin: 0 0 14px; }
.check input { width: 18px; height: 18px; accent-color: var(--accent); }

.stats { margin: 0 0 14px; color: var(--muted); font-size: 13px; font-variant-numeric: tabular-nums; }

#tab-list { list-style: none; margin: 0 0 12px; padding: 0; display: grid; gap: 6px; }
#tab-list li { display: flex; align-items: center; gap: 6px; }
#tab-list .tab-open {
  flex: 1;
  min-width: 0;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  text-align: left;
}
#tab-list li.active .tab-open { border-color: var(--accent); }
#tab-list .tab-title, #tab-list .tab-url { display: block; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
#tab-list .tab-url { color: var(--muted); font-size: 12px; }
#tab-list .tab-close { width: 36px; height: 36px; border: 0; border-radius: 8px; background: transparent; color: var(--muted); font-size: 20px; }
#new-tab { background: var(--surface-2); }

/* ---- диалоги страницы и уведомления ---- */

dialog {
  width: min(400px, calc(100vw - 32px));
  padding: 20px;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
}
dialog::backdrop { background: rgba(0, 0, 0, 0.35); }
.dialog-origin { margin: 0 0 6px; color: var(--muted); font-size: 13px; }
.dialog-text { margin: 0 0 14px; white-space: pre-wrap; overflow-wrap: anywhere; max-height: 50vh; overflow-y: auto; }
#dialog-input { width: 100%; height: 40px; margin-bottom: 14px; padding: 0 10px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface); font-size: 16px; }
.dialog-actions { display: flex; gap: 8px; justify-content: flex-end; }
.dialog-actions button { min-width: 90px; height: 40px; padding: 0 16px; border: 1px solid var(--border); border-radius: 10px; background: transparent; }
.dialog-actions button.primary { width: auto; border: 0; background: var(--accent); color: var(--accent-text); }

#toast {
  position: fixed;
  left: 50%;
  bottom: max(20px, env(safe-area-inset-bottom));
  z-index: 20;
  transform: translateX(-50%);
  max-width: calc(100vw - 32px);
  padding: 10px 16px;
  border-radius: 10px;
  background: var(--text);
  color: var(--bg);
  box-shadow: var(--shadow);
}
#toast.error { background: var(--danger); color: #fff; }
#toast button {
  margin-left: 12px;
  padding: 4px 10px;
  border: 0;
  border-radius: 8px;
  background: var(--accent);
  color: var(--accent-text);
  font: inherit;
}
