:root {
  --bg: #0e1320;
  --bg-2: #151c2e;
  --bg-3: #1d263c;
  --line: #2a3550;
  --text: #e8ecf6;
  --muted: #9aa6c2;
  --accent: #4f8cff;
  --accent-2: #7a5cff;
  --danger: #ff5d6c;
  --ok: #3ddc97;
  --warn: #ffb454;
  --radius: 10px;
  --mono: ui-monospace, "Cascadia Mono", "SF Mono", Menlo, Consolas, monospace;
  color-scheme: dark;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: radial-gradient(1200px 700px at 20% -10%, #1a2342 0%, var(--bg) 60%);
  color: var(--text);
  font-size: 15px;
  line-height: 1.45;
  overscroll-behavior: none;
}
[hidden] { display: none !important; }
h1, h2 { margin: 0; font-weight: 650; }
h1 { font-size: 1.5rem; }
h2 { font-size: 1.05rem; }
p { margin: 0 0 .6rem; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.mono { font-family: var(--mono); }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* ── Controls ── */
button, select, input[type="text"], input:not([type]), input[type="password"], input[type="url"] {
  font: inherit;
  color: var(--text);
}
button {
  background: var(--bg-3);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: .45rem .8rem;
  cursor: pointer;
  touch-action: manipulation;
}
button:hover:not(:disabled) { border-color: #3d4c75; background: #232e4a; }
button:disabled { opacity: .45; cursor: default; }
button:focus-visible, select:focus-visible, input:focus-visible, summary:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
button.primary {
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  border-color: transparent;
  color: #fff;
  font-weight: 600;
}
button.primary:hover:not(:disabled) { filter: brightness(1.1); background: linear-gradient(135deg, var(--accent), var(--accent-2)); }
button.danger { color: #ffd5d9; border-color: #5a2a33; background: #2c1820; }
button.danger:hover:not(:disabled) { background: #3b1d27; border-color: var(--danger); }
button[aria-pressed="true"] { background: #243a6b; border-color: var(--accent); }
.icon-btn { padding: .1rem .55rem; font-size: 1.2rem; line-height: 1.2; }

input, select {
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: .55rem .7rem;
  width: 100%;
  font-size: 1rem;
}
select { width: auto; padding: .4rem .6rem; }
input:focus { border-color: var(--accent); }
label { display: block; margin: .9rem 0 .3rem; font-size: .88rem; color: var(--muted); }
label.check { display: flex; align-items: center; gap: .55rem; color: var(--text); font-size: .95rem; margin: .4rem 0; }
label.check input { width: auto; }
fieldset { border: 0; margin: 0; padding: 0; min-width: 0; }
fieldset:disabled { opacity: .6; }
.hint { color: var(--muted); font-size: .82rem; margin-top: .35rem; }

/* ── Screens & cards ── */
.screen { min-height: 100%; }
#connect-view, #unsupported { display: grid; place-items: center; padding: 1.5rem 1rem; }
.card {
  width: min(460px, 100%);
  background: var(--bg-2);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 1.5rem;
  box-shadow: 0 20px 60px rgb(0 0 0 / .45);
}
.brand { display: flex; align-items: center; gap: .9rem; margin-bottom: .6rem; }
.brand img { border-radius: 11px; }
.brand p { margin: 0; }
.actions { display: flex; gap: .6rem; margin-top: 1.1rem; flex-wrap: wrap; }
.actions .primary { flex: 1 1 auto; padding: .7rem 1rem; }
.error { color: #ffc2c8; background: #2a1820; border: 1px solid #5a2a33; border-radius: 8px; padding: .6rem .75rem; margin-top: 1rem; }
.notice { color: #ffe2b8; background: #2b2314; border: 1px solid #5c4a22; border-radius: 8px; padding: .6rem .75rem; font-size: .88rem; }
.foot { color: var(--muted); font-size: .8rem; margin: 1.2rem 0 0; }
.progress { display: flex; align-items: center; gap: .7rem; margin-top: 1rem; color: var(--muted); }
.spinner {
  width: 18px; height: 18px; border-radius: 50%;
  border: 2px solid var(--line); border-top-color: var(--accent);
  animation: spin .8s linear infinite; flex: none;
}
@keyframes spin { to { transform: rotate(360deg); } }
details { margin-top: 1rem; }
summary { cursor: pointer; color: var(--muted); font-size: .9rem; user-select: none; }
.options-body { padding-top: .4rem; }
.more { margin-top: 1.3rem; border-top: 1px solid var(--line); padding-top: .9rem; }
#recent { margin-top: 1.4rem; }
#recent h2 { font-size: .9rem; color: var(--muted); font-weight: 600; margin-bottom: .5rem; }
.chips { list-style: none; margin: 0; padding: 0; display: flex; flex-wrap: wrap; gap: .5rem; }
.chips button { display: flex; flex-direction: column; align-items: flex-start; gap: 0; padding: .4rem .7rem; }
.chips small { color: var(--muted); font-family: var(--mono); }
.trusted { list-style: none; margin: .4rem 0 0; padding: 0; }
.trusted li { display: flex; align-items: center; justify-content: space-between; gap: .6rem; padding: .35rem 0; border-top: 1px solid var(--line); font-size: .88rem; }
.trusted code { font-family: var(--mono); font-size: .78rem; color: var(--muted); display: block; }
.trusted button { padding: .25rem .55rem; font-size: .82rem; }

/* ── Viewer layout ── */
.viewer { display: flex; flex-direction: column; height: 100vh; height: 100dvh; background: #05070d; }
#toolbar {
  display: flex; flex-wrap: wrap; gap: .4rem .9rem; align-items: center;
  padding: .4rem .6rem; padding-top: max(.4rem, env(safe-area-inset-top));
  background: var(--bg-2); border-bottom: 1px solid var(--line);
  z-index: 5;
}
.tool-group { display: flex; flex-wrap: wrap; gap: .35rem; align-items: center; }
.tool-group.end { margin-left: auto; }
#toolbar button, #toolbar select { padding: .3rem .6rem; font-size: .88rem; }
.chip { display: inline-flex; align-items: center; gap: .45rem; padding: .25rem .6rem; background: var(--bg); border: 1px solid var(--line); border-radius: 999px; font-size: .88rem; max-width: 16rem; }
.chip #host-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dot { width: 9px; height: 9px; border-radius: 50%; background: var(--ok); flex: none; }
.dot.slow { background: var(--warn); }
.dot.bad { background: var(--danger); }
.badge { display: inline-block; min-width: 1.2em; padding: 0 .35em; border-radius: 999px; background: var(--danger); color: #fff; font-size: .75rem; text-align: center; }
.menu-wrap { position: relative; }
.menu {
  position: absolute; top: calc(100% + 6px); left: 0; z-index: 20; min-width: 15rem;
  display: flex; flex-direction: column; gap: 2px; padding: .35rem;
  background: var(--bg-2); border: 1px solid var(--line); border-radius: 10px;
  box-shadow: 0 14px 40px rgb(0 0 0 / .5);
}
.menu button { text-align: left; border-color: transparent; background: transparent; }
.menu button:hover:not(:disabled) { background: var(--bg-3); }
.menu hr { border: 0; border-top: 1px solid var(--line); margin: .25rem 0; width: 100%; }

#workspace { flex: 1 1 auto; display: flex; min-height: 0; position: relative; }
#stage {
  position: relative; flex: 1 1 auto; min-width: 0;
  display: flex; align-items: center; justify-content: center;
  overflow: hidden; background: #000;
}
#stage.actual { overflow: auto; align-items: flex-start; justify-content: flex-start; }
#stage.actual canvas { margin: auto; }
#screen { display: block; background: #000; touch-action: none; user-select: none; -webkit-user-select: none; outline: none; image-rendering: auto; }
#key-sink {
  position: absolute; left: 0; top: 0; width: 1px; height: 1px; opacity: 0; padding: 0; border: 0;
  resize: none; font-size: 16px; /* 16px stops iOS zooming when it takes focus */
  pointer-events: none; caret-color: transparent; color: transparent; background: transparent;
}
.banner {
  position: absolute; top: .6rem; left: 50%; transform: translateX(-50%); z-index: 4;
  max-width: min(90%, 36rem); padding: .45rem .85rem; border-radius: 999px;
  background: rgb(21 28 46 / .92); border: 1px solid var(--line); font-size: .88rem; text-align: center;
  backdrop-filter: blur(6px);
}
.overlay { position: absolute; inset: 0; z-index: 6; display: grid; place-items: center; background: rgb(5 7 13 / .72); backdrop-filter: blur(3px); padding: 1rem; }
.overlay-card { background: var(--bg-2); border: 1px solid var(--line); border-radius: 14px; padding: 1.3rem 1.4rem; width: min(420px, 100%); }
.overlay-card h2 { margin-bottom: .4rem; }

#statusbar {
  display: flex; flex-wrap: wrap; gap: .25rem 1.1rem; padding: .25rem .7rem; padding-bottom: max(.25rem, env(safe-area-inset-bottom));
  background: var(--bg-2); border-top: 1px solid var(--line); color: var(--muted); font-size: .78rem;
}
#statusbar span:empty { display: none; }

/* ── Side panels ── */
.panel {
  flex: 0 0 min(380px, 42vw); display: flex; flex-direction: column; min-height: 0;
  background: var(--bg-2); border-left: 1px solid var(--line); z-index: 3;
}
.panel-head { display: flex; align-items: center; justify-content: space-between; padding: .55rem .7rem; border-bottom: 1px solid var(--line); }
.path-row { display: flex; gap: .35rem; padding: .5rem .6rem; }
.path-row button { padding: .3rem .6rem; }
.path-row input { padding: .3rem .5rem; font-size: .88rem; font-family: var(--mono); }
#files-message { padding: 0 .7rem; }
.files-list-wrap { position: relative; flex: 1 1 auto; overflow: auto; min-height: 6rem; }
.files-list-wrap.dragging { outline: 2px dashed var(--accent); outline-offset: -6px; }
.drop-hint { position: absolute; inset: 0; display: grid; place-items: center; background: rgb(21 28 46 / .85); font-weight: 600; pointer-events: none; }
table.files { width: 100%; border-collapse: collapse; font-size: .84rem; }
table.files th { position: sticky; top: 0; background: var(--bg-2); text-align: left; font-weight: 600; color: var(--muted); padding: .3rem .5rem; border-bottom: 1px solid var(--line); }
table.files td { padding: .25rem .5rem; border-bottom: 1px solid #1d2640; vertical-align: middle; }
table.files .num { text-align: right; white-space: nowrap; }
table.files td.name { max-width: 11rem; }
table.files td.name button { all: unset; cursor: pointer; display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 100%; }
table.files td.name button:hover { color: var(--accent); text-decoration: underline; }
table.files td.name button:focus-visible { outline: 2px solid var(--accent); }
table.files td.name button.dir::before { content: "▸ "; color: var(--muted); }
table.files td.when { white-space: nowrap; color: var(--muted); font-size: .78rem; }
table.files td.ops { white-space: nowrap; text-align: right; }
table.files td.ops button { padding: .12rem .4rem; font-size: .78rem; }
.panel-foot { padding: .5rem .6rem; border-top: 1px solid var(--line); }
.panel-foot button { width: 100%; padding: .55rem; }
.transfers { list-style: none; margin: 0; padding: 0 .6rem .6rem; max-height: 11rem; overflow: auto; }
.transfers li { padding: .35rem 0; border-top: 1px solid #1d2640; font-size: .8rem; }
.transfers .t-row { display: flex; justify-content: space-between; gap: .5rem; align-items: center; }
.transfers .t-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.transfers progress { width: 100%; height: 6px; margin-top: .25rem; accent-color: var(--accent); }
.transfers .t-state { color: var(--muted); }
.transfers .t-state.fail { color: #ffb4bc; }
.transfers button { padding: .08rem .4rem; font-size: .75rem; }

.chat-log { list-style: none; margin: 0; padding: .6rem; overflow: auto; flex: 1 1 auto; display: flex; flex-direction: column; gap: .45rem; }
.chat-log li { max-width: 90%; padding: .4rem .65rem; border-radius: 12px; background: var(--bg-3); align-self: flex-start; word-break: break-word; white-space: pre-wrap; }
.chat-log li.me { align-self: flex-end; background: #243a6b; }
.chat-log small { display: block; color: var(--muted); font-size: .72rem; }
.chat-form { display: flex; gap: .4rem; padding: .5rem .6rem; border-top: 1px solid var(--line); padding-bottom: max(.5rem, env(safe-area-inset-bottom)); }
.chat-form input { padding: .45rem .6rem; }

/* ── Dialog & toasts ── */
dialog {
  background: var(--bg-2); color: var(--text); border: 1px solid var(--line); border-radius: 14px;
  padding: 1.2rem 1.3rem; width: min(480px, calc(100% - 2rem));
  box-shadow: 0 24px 70px rgb(0 0 0 / .6);
}
dialog::backdrop { background: rgb(5 7 13 / .7); backdrop-filter: blur(3px); }
dialog h2 { margin-bottom: .5rem; }
dialog p { color: #cfd7ea; white-space: pre-line; }
dialog.warn { border-color: #7a4a1c; }
dialog.warn h2 { color: var(--warn); }
.detail { margin: .6rem 0; padding: .6rem .7rem; background: var(--bg); border: 1px solid var(--line); border-radius: 8px; font-size: .85rem; white-space: pre-wrap; word-break: break-all; }
dialog .actions { justify-content: flex-end; }
dialog .actions .primary { flex: 0 0 auto; padding: .5rem 1rem; }

#toasts { position: fixed; right: .8rem; bottom: .8rem; z-index: 50; display: flex; flex-direction: column; gap: .5rem; align-items: flex-end; max-width: min(26rem, calc(100% - 1.6rem)); pointer-events: none; }
.toast {
  pointer-events: auto; display: flex; gap: .7rem; align-items: center;
  background: var(--bg-3); border: 1px solid var(--line); border-radius: 10px; padding: .55rem .8rem; font-size: .88rem;
  box-shadow: 0 10px 30px rgb(0 0 0 / .45); animation: toast-in .18s ease-out;
}
.toast.error { border-color: #5a2a33; background: #2a1820; }
.toast button { padding: .15rem .55rem; font-size: .8rem; }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } }

.noscript { padding: 1rem; text-align: center; }

@media (max-width: 760px) {
  .panel { position: absolute; inset: 0; flex-basis: auto; z-index: 8; }
  /* One swipeable row, so the picture keeps the screen. */
  #toolbar { flex-wrap: nowrap; overflow-x: auto; gap: .5rem; scrollbar-width: none; -webkit-overflow-scrolling: touch; }
  #toolbar::-webkit-scrollbar { display: none; }
  .tool-group { flex-wrap: nowrap; flex: none; }
  .tool-group.end { margin-left: 0; }
  #toolbar button, #toolbar select, .chip { white-space: nowrap; flex: none; }
  .chip { max-width: 9rem; }
  /* The menu would be clipped inside the scrolling row, so it floats over the page instead. */
  .menu-wrap { position: static; }
  .menu { position: fixed; top: 3.2rem; left: .5rem; right: .5rem; min-width: 0; max-height: 70vh; overflow-y: auto; }
  #statusbar { gap: .1rem .8rem; font-size: .72rem; }
}
@media (prefers-reduced-motion: reduce) {
  .spinner { animation-duration: 2s; }
  .toast { animation: none; }
}
