:root {
  --bg: #eef0f4;
  --bar: #ffffff;
  --text: #1b2130;
  --muted: #667085;
  --border: #dfe3ea;
  --field: #f0f2f6;
  --field-focus: #ffffff;
  --accent: #2f6fed;
  --accent-text: #ffffff;
  --tab: #e6e9ef;
  --tab-hover: #dde1e8;
  --tab-active: #ffffff;
  --danger: #c93a3a;
  --overlay: rgba(238, 240, 244, 0.92);
  --shadow: 0 10px 34px rgba(15, 23, 42, 0.18), 0 2px 6px rgba(15, 23, 42, 0.08);
  --radius: 10px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1014;
    --bar: #171a20;
    --text: #e6e8ed;
    --muted: #98a1b0;
    --border: #2a2f39;
    --field: #22262e;
    --field-focus: #1b1e25;
    --accent: #5b8cff;
    --accent-text: #0b1020;
    --tab: #1d2128;
    --tab-hover: #252a33;
    --tab-active: #2b303a;
    --danger: #ff6b6b;
    --overlay: rgba(14, 16, 20, 0.9);
    --shadow: 0 10px 34px rgba(0, 0, 0, 0.5), 0 2px 6px rgba(0, 0, 0, 0.3);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font: 14px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
}
button { font: inherit; color: inherit; }
[hidden] { display: none !important; }

/* ---------- login ---------- */
.login-page {
  display: grid;
  place-items: center;
  min-height: 100%;
  padding: 24px 16px;
}
.login-card {
  width: 100%;
  max-width: 360px;
  background: var(--bar);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 32px 24px 28px;
  box-shadow: var(--shadow);
  text-align: center;
}
.login-logo { display: block; margin: 0 auto 12px; border-radius: 12px; }
.login-card h1 { margin: 0; font-size: 22px; letter-spacing: -0.01em; }
.login-sub { margin: 4px 0 20px; color: var(--muted); }
.login-msg {
  margin: 0 0 16px;
  padding: 10px 12px;
  border-radius: 8px;
  background: color-mix(in srgb, var(--danger) 12%, transparent);
  color: var(--danger);
}
.login-form { display: grid; gap: 8px; text-align: left; }
.login-form label { font-weight: 600; font-size: 13px; color: var(--muted); }
.login-form input {
  height: 44px;
  padding: 0 12px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--field);
  color: var(--text);
  font-size: 16px;
}
.login-form input:focus { outline: 2px solid var(--accent); outline-offset: 1px; background: var(--field-focus); }
.btn-primary {
  margin-top: 8px;
  height: 44px;
  border: 0;
  border-radius: 8px;
  background: var(--accent);
  color: var(--accent-text);
  font-weight: 600;
  cursor: pointer;
}
.btn-primary:hover { filter: brightness(1.06); }

/* ---------- app layout ---------- */
.app {
  display: flex;
  flex-direction: column;
  position: fixed;
  inset: 0;
  height: var(--app-h, 100dvh);
  overflow: hidden;
}
.bar {
  display: flex;
  align-items: center;
  gap: 2px;
  height: 48px;
  padding: 0 6px;
  padding-left: max(6px, env(safe-area-inset-left));
  padding-right: max(6px, env(safe-area-inset-right));
  background: var(--bar);
  border-bottom: 1px solid var(--border);
  flex: none;
}
.icon-btn {
  flex: none;
  width: 36px;
  height: 36px;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 8px;
  background: transparent;
  cursor: pointer;
}
.icon-btn:hover:not(:disabled) { background: var(--tab-hover); }
.icon-btn:disabled { opacity: 0.35; cursor: default; }
.icon-btn svg, .omni-icon {
  width: 20px;
  height: 20px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}
#menu-btn svg circle { fill: currentColor; stroke: none; }
.icon-btn .i-stop { display: none; }
.loading .icon-btn .i-stop { display: block; }
.loading .icon-btn .i-reload { display: none; }

.omnibox {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 8px;
  height: 36px;
  margin: 0 4px;
  padding: 0 12px;
  border-radius: 18px;
  background: var(--field);
  border: 1px solid transparent;
}
.omnibox:focus-within { background: var(--field-focus); border-color: var(--accent); }
.omni-icon { width: 15px; height: 15px; color: var(--muted); flex: none; }
.omnibox input {
  flex: 1;
  min-width: 0;
  height: 100%;
  border: 0;
  outline: 0;
  background: transparent;
  color: var(--text);
  font-size: 15px;
}

.progress {
  height: 2px;
  background: linear-gradient(90deg, transparent, var(--accent), transparent);
  background-size: 40% 100%;
  background-repeat: no-repeat;
  animation: slide 1s linear infinite;
  flex: none;
  margin-top: -2px;
  position: relative;
  z-index: 1;
}
@keyframes slide { from { background-position: -40% 0; } to { background-position: 140% 0; } }

.tabs {
  display: flex;
  gap: 4px;
  height: 36px;
  padding: 4px 6px 0;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  overflow-x: auto;
  overflow-y: hidden;
  scrollbar-width: none;
  flex: none;
}
.tabs::-webkit-scrollbar { display: none; }
.tab {
  flex: 0 1 200px;
  min-width: 96px;
  display: flex;
  align-items: center;
  gap: 4px;
  height: 31px;
  padding: 0 4px 0 12px;
  border: 1px solid transparent;
  border-bottom: 0;
  border-radius: 8px 8px 0 0;
  background: var(--tab);
  color: var(--muted);
  cursor: pointer;
  font-size: 13px;
}
.tab:hover { background: var(--tab-hover); }
.tab.active { background: var(--tab-active); color: var(--text); border-color: var(--border); }
.tab-title { flex: 1; min-width: 0; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; text-align: left; }
.tab-close {
  flex: none;
  width: 22px;
  height: 22px;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: inherit;
  cursor: pointer;
  font-size: 16px;
  line-height: 1;
}
.tab-close:hover { background: var(--tab-hover); }

.keys {
  display: flex;
  gap: 6px;
  padding: 6px;
  background: var(--bar);
  border-bottom: 1px solid var(--border);
  overflow-x: auto;
  flex: none;
}
.keys button {
  flex: none;
  min-width: 44px;
  height: 34px;
  padding: 0 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--field);
  cursor: pointer;
}
.keys .keys-done { margin-left: auto; background: var(--accent); color: var(--accent-text); border-color: transparent; font-weight: 600; }

.viewer {
  position: relative;
  flex: 1;
  min-height: 0;
  overflow: hidden;
  background: var(--bg);
}
#screen {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
  outline: none;
  cursor: default;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}
.kbd {
  position: absolute;
  left: 0;
  bottom: 0;
  width: 2px;
  height: 2px;
  padding: 0;
  border: 0;
  opacity: 0;
  resize: none;
  font-size: 16px; /* stops iOS zooming in on focus */
  pointer-events: none;
  caret-color: transparent;
}
.overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  background: var(--overlay);
  color: var(--muted);
  font-size: 15px;
}
.spinner {
  width: 20px;
  height: 20px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- menu ---------- */
.menu {
  position: fixed;
  top: 52px;
  right: max(8px, env(safe-area-inset-right));
  width: 260px;
  max-width: calc(100vw - 16px);
  padding: 8px;
  background: var(--bar);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow);
  z-index: 20;
}
.menu form { margin: 0; }
.menu-label { padding: 4px 8px 6px; font-size: 12px; color: var(--muted); font-weight: 600; }
.segmented {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 2px;
  margin: 0 4px 8px;
  padding: 2px;
  border-radius: 8px;
  background: var(--field);
}
.segmented button {
  height: 30px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  font-size: 12.5px;
  cursor: pointer;
}
.segmented button.on { background: var(--bar); box-shadow: 0 1px 3px rgba(0, 0, 0, 0.15); font-weight: 600; }
.menu-item {
  display: block;
  width: 100%;
  padding: 9px 10px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  text-align: left;
  cursor: pointer;
}
.menu-item:hover { background: var(--field); }
.menu-item.danger { color: var(--danger); }

/* ---------- modal ---------- */
.modal-root {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 16px;
  background: rgba(0, 0, 0, 0.4);
  z-index: 30;
}
.modal {
  width: 100%;
  max-width: 420px;
  padding: 20px;
  background: var(--bar);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: var(--shadow);
}
.modal h2 { margin: 0 0 8px; font-size: 17px; }
.modal p { margin: 0 0 14px; color: var(--muted); white-space: pre-wrap; word-break: break-word; max-height: 40vh; overflow: auto; }
.modal input, .modal textarea {
  width: 100%;
  margin-bottom: 14px;
  padding: 10px 12px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--field);
  color: var(--text);
  font: inherit;
  font-size: 16px;
}
.modal-buttons { display: flex; justify-content: flex-end; gap: 8px; flex-wrap: wrap; }
.modal-buttons button {
  min-width: 84px;
  height: 38px;
  padding: 0 14px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--field);
  cursor: pointer;
}
.modal-buttons button.primary { background: var(--accent); color: var(--accent-text); border-color: transparent; font-weight: 600; }

/* ---------- toasts ---------- */
.toasts {
  position: fixed;
  right: 12px;
  bottom: max(12px, env(safe-area-inset-bottom));
  display: grid;
  gap: 8px;
  z-index: 40;
  max-width: calc(100vw - 24px);
}
.toast {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 220px;
  max-width: 380px;
  padding: 10px 12px;
  background: var(--bar);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: var(--shadow);
  font-size: 13.5px;
}
.toast.error { border-color: var(--danger); }
.toast-text { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.toast a {
  flex: none;
  padding: 6px 10px;
  border-radius: 6px;
  background: var(--accent);
  color: var(--accent-text);
  text-decoration: none;
  font-weight: 600;
}

@media (max-width: 560px) {
  .hide-narrow { display: none; }
  .icon-btn { width: 34px; }
  .omnibox { margin: 0 2px; padding: 0 10px; }
  .omni-icon { display: none; }
  .tab { min-width: 84px; }
}
