:root {
  --bg: #0f1117;
  --panel: #171a23;
  --card: #1d2230;
  --card2: #232838;
  --ink: #e7e9f0;
  --muted: #9aa0ad;
  --accent: #2f8bff;
  --accent2: #1c74bc;
  --ok: #4cc676;
  --err: #e0796f;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.5 -apple-system, "Segoe UI", Roboto, system-ui, sans-serif;
  display: flex; flex-direction: column;
  height: 100vh; height: 100dvh;     /* fixed shell — only content/chat scroll inside */
  overflow: hidden;
}
header {
  flex: 0 0 auto;
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(12px + env(safe-area-inset-top)) 16px 12px;
  border-bottom: 1px solid rgba(127,127,127,0.18); background: var(--bg); z-index: 10;
}
.brandwrap { display: flex; align-items: center; gap: 9px; min-width: 0; }
.brandmark { display: block; border-radius: 7px; flex: 0 0 auto; }
.brand { font-weight: 700; letter-spacing: .3px; font-size: 17px; }
.headright { display: flex; align-items: center; gap: 10px; min-width: 0; }
.status { display: flex; align-items: center; gap: 7px; color: var(--muted); font-size: 11.5px;
  max-width: 42vw; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.status #statustext { overflow: hidden; text-overflow: ellipsis; }
.avatarbtn {
  width: 34px; height: 34px; border-radius: 50%; padding: 0; flex: 0 0 auto;
  background-color: var(--card2); background-size: cover; background-position: center;
  color: var(--ink); font-size: 13px; font-weight: 700;
  border: 1px solid rgba(127,127,127,0.3);
  display: flex; align-items: center; justify-content: center;
}
.avatarbtn.haspic { color: transparent; }
.dot { width: 9px; height: 9px; border-radius: 50%; background: #555; }
.dot.online { background: var(--ok); }
.dot.connecting { background: #d2a24c; }
.dot.offline { background: #6b7180; }
.dot.error { background: var(--err); }

section { padding: 16px; }
#setup, #offline, #moved { flex: 1; overflow-y: auto; }
#offline, #moved { flex-direction: column; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.ok { color: var(--ok); margin-top: 10px; }
.center { text-align: center; }
.big { font-size: 44px; }

.card-lg {
  max-width: 460px; margin: 6vh auto 0; background: var(--panel);
  border: 1px solid rgba(127,127,127,0.18); border-radius: 16px; padding: 24px;
}
.card-lg h1 { margin: 0 0 8px; font-size: 22px; }
.card-lg h2 { margin: 8px 0; }
#gbtn { margin: 16px 0; }
.pairwrap { margin-top: 16px; border-top: 1px solid rgba(127,127,127,0.18); padding-top: 16px; }
.scanner {
  position: fixed; inset: 0; z-index: 100; background: #000;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
}
.scanner video { width: 100%; height: 100%; object-fit: cover; position: absolute; inset: 0; }
.scanframe {
  position: relative; width: 70vw; max-width: 280px; aspect-ratio: 1;
  border: 3px solid rgba(255,255,255,0.9); border-radius: 18px;
  box-shadow: 0 0 0 100vmax rgba(0,0,0,0.45);
}
.scanhint {
  position: absolute; bottom: 18%; left: 0; right: 0; text-align: center;
  color: #fff; padding: 0 24px; font-size: 14px; text-shadow: 0 1px 4px #000;
}
.scanclose {
  position: absolute; bottom: calc(7% + env(safe-area-inset-bottom)); z-index: 101;
  background: rgba(255,255,255,0.16); color: #fff; border: 1px solid rgba(255,255,255,0.4);
}
.installrow { margin-top: 16px; border-top: 1px solid rgba(127,127,127,0.18); padding-top: 16px; }
.installrow #installbtn { background: var(--card2); border: 1px solid rgba(127,127,127,0.25); }
.installrow #installhint { margin-top: 4px; line-height: 1.5; }
.pairrow { display: flex; gap: 8px; margin-top: 10px; }
input, textarea {
  background: var(--card); border: 1px solid rgba(127,127,127,0.25); color: var(--ink);
  border-radius: 10px; padding: 10px 12px; font: inherit; width: 100%;
}
button.primary, button.active { background: var(--accent); }
button {
  background: var(--card2); color: var(--ink); border: none; border-radius: 10px;
  padding: 10px 16px; font: inherit; font-weight: 600; cursor: pointer;
}
button.primary { color: #fff; }
button:hover { filter: brightness(1.08); }

/* App */
#app { display: flex; flex-direction: column; flex: 1; min-height: 0; padding: 0; }
/* Bottom tab bar — native-feeling on mobile */
#nav {
  flex: 0 0 auto; display: flex; gap: 2px;
  padding: 6px 4px calc(6px + env(safe-area-inset-bottom));
  border-top: 1px solid rgba(127,127,127,0.16);
  background: color-mix(in srgb, var(--panel) 92%, transparent);
  backdrop-filter: blur(8px);
}
#nav button {
  flex: 1; min-width: 0; display: flex; flex-direction: column; align-items: center; gap: 3px;
  background: transparent; border: none; padding: 7px 2px 5px; border-radius: 14px;
  color: var(--muted); font-weight: 600; transition: color .15s, background .15s;
}
#nav button .ni { font-size: 19px; line-height: 1; filter: grayscale(0.35) opacity(0.75); transition: filter .15s; }
#nav button .nl { font-size: 10.5px; letter-spacing: .2px; }
#nav button.active { color: var(--accent); background: rgba(47,139,255,0.12); }
#nav button.active .ni { filter: none; }
#nav button.active .nl { color: var(--accent); }
#devbar {
  flex: 0 0 auto; display: flex; align-items: center; gap: 8px;
  padding: 8px 12px; border-bottom: 1px solid rgba(127,127,127,0.18); background: var(--bg);
}
#devbar .devlbl { color: var(--muted); font-size: 12px; }
#devbar select { flex: 1; padding: 7px 10px; font-size: 13px; }
.content { flex: 1; min-height: 0; overflow-y: auto; padding: 16px 18px; }
.card {
  background: var(--card); border: 1px solid rgba(127,127,127,0.14);
  border-radius: 12px; padding: 12px 14px; margin-bottom: 10px;
}
.card.me { border-left: 3px solid var(--accent2); }
.card .who { font-weight: 600; font-size: 13px; margin-bottom: 3px; }
.card .body { white-space: pre-wrap; word-break: break-word; }
.card .meta { color: var(--muted); font-size: 12px; margin-top: 6px; }

/* Chat */
.chat { display: flex; flex-direction: column; flex: 1; min-height: 0; }
.chatlog {
  flex: 1; min-height: 0; overflow-y: auto;
  display: flex; flex-direction: column; gap: 12px;
  padding: 18px 18px;                       /* full width — bubbles hug the edges */
}
.msg {
  max-width: min(72%, 640px);               /* bubble capped, but anchored to the side */
  padding: 10px 14px; border-radius: 16px;
  line-height: 1.45; white-space: pre-wrap; word-break: break-word;
}
.msg.me { align-self: flex-end; background: var(--accent2); color: #fff; border-bottom-right-radius: 5px; }
.msg.rev { align-self: flex-start; background: var(--card2); border-bottom-left-radius: 5px; }
.chatbar {
  flex: 0 0 auto; display: flex; gap: 8px; align-items: flex-end;
  padding: 10px 14px;
  border-top: 1px solid rgba(127,127,127,0.18);
}
.chatbar textarea { resize: none; max-height: 120px; border-radius: 22px; padding: 11px 16px; }
.chatbar #chatsend {
  flex: 0 0 auto; width: 42px; height: 42px; border-radius: 50%; padding: 0;
  font-size: 17px; display: flex; align-items: center; justify-content: center;
}

/* Control tab */
.ctl { display: flex; flex-direction: column; gap: 14px; }
.ctlsec { background: var(--card); border: 1px solid rgba(127,127,127,0.14); border-radius: 14px; padding: 14px 16px; }
.ctlh { font-weight: 700; font-size: 13px; margin-bottom: 11px; color: var(--ink); display: flex; align-items: center; gap: 8px; }
.ctlh .i { font-size: 16px; }
.ctlrow { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.ctlrow input { width: auto; flex: 1 1 150px; min-width: 130px; }
.ctlbtns { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
.ctlrow button, .ctlbtns button {
  padding: 10px 14px; background: var(--card2); display: inline-flex; align-items: center; gap: 6px;
  border: 1px solid rgba(127,127,127,0.16);
}
.ctlrow button:hover, .ctlbtns button:hover { background: #2b3142; }
.ctlout {
  white-space: pre-wrap; margin: 12px 0 0; font-size: 12.5px; line-height: 1.6;
  background: var(--bg); border-radius: 10px; padding: 12px; max-height: 220px; overflow: auto;
}
.ctltimer { margin-top: 14px; text-align: center; font-size: 34px; font-weight: 700; font-variant-numeric: tabular-nums; letter-spacing: 1px; }
.ctltimer.done { color: var(--ok); }
.ctlstatus { font-size: 12px; min-height: 16px; color: var(--muted); }
select { background: var(--card2); color: var(--ink); border: 1px solid rgba(127,127,127,0.25); border-radius: 10px; padding: 10px; font: inherit; }

/* Study hub */
.subnav { display: flex; gap: 8px; margin-bottom: 14px; overflow-x: auto; }
.subnav button {
  background: var(--card2); color: var(--muted); padding: 8px 14px; border-radius: 999px;
  border: 1px solid rgba(127,127,127,0.16); white-space: nowrap; font-size: 13px;
}
.subnav button.active { background: var(--accent); color: #fff; border-color: transparent; }

/* Study review */
.review { display: flex; flex-direction: column; gap: 14px; }
.rmeta { color: var(--muted); font-size: 12px; }
.rcard { background: var(--card); border: 1px solid rgba(127,127,127,0.16); border-radius: 14px; padding: 26px 18px; min-height: 140px; cursor: pointer; display: flex; flex-direction: column; gap: 14px; justify-content: center; }
.rfront { font-size: 18px; font-weight: 600; }
.rback { font-size: 16px; border-top: 1px solid rgba(127,127,127,0.18); padding-top: 14px; white-space: pre-wrap; }
.rhint { font-size: 12px; }
.rbtns { display: flex; gap: 10px; }
.rbtns button { flex: 1; padding: 13px; }
.rdone { text-align: center; color: var(--ok); padding: 30px 0; font-size: 16px; }

/* Files */
#files { display: flex; flex-direction: column; }
.ftop { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 10px; }
.ftop .fpath { margin-bottom: 0; }
.fupload { background: var(--accent2); color: #fff; border-radius: 10px; padding: 8px 13px; cursor: pointer; font-size: 13px; font-weight: 600; white-space: nowrap; flex: 0 0 auto; }
.fupload:hover { filter: brightness(1.08); }
.fpath { font-size: 12px; color: var(--muted); margin-bottom: 10px; word-break: break-all; }
.flist { display: flex; flex-direction: column; }
.frow { display: flex; align-items: center; gap: 10px; padding: 11px 10px; border-bottom: 1px solid rgba(127,127,127,0.10); cursor: pointer; border-radius: 8px; word-break: break-all; }
.frow:hover { background: var(--card2); }
.fic { width: 20px; text-align: center; flex: 0 0 auto; }
.fsize { margin-left: auto; color: var(--muted); font-size: 12px; flex: 0 0 auto; padding-left: 10px; }
.uprow { display: flex; align-items: center; gap: 10px; margin-top: 4px; }
.ubar { flex: 1; height: 8px; background: var(--card2); border-radius: 6px; overflow: hidden; }
.ufill { height: 100%; width: 0; background: var(--accent); transition: width .2s; }
.ucancel { background: var(--card2); border: 1px solid rgba(127,127,127,0.2); padding: 6px 12px; font-size: 12px; flex: 0 0 auto; }
.usmall { font-size: 12px; margin-top: 6px; }

/* Profile / account bottom sheet */
.sheet { position: fixed; inset: 0; z-index: 90; background: rgba(0,0,0,0.55); display: flex; align-items: flex-end; }
.sheetcard {
  width: 100%; background: var(--panel); border-radius: 22px 22px 0 0;
  padding: 10px 20px calc(20px + env(safe-area-inset-bottom));
  border-top: 1px solid rgba(127,127,127,0.2); box-shadow: 0 -10px 40px rgba(0,0,0,0.5);
  animation: sheetup .22s ease;
}
@keyframes sheetup { from { transform: translateY(40px); opacity: .3; } to { transform: none; opacity: 1; } }
.sheetgrab { width: 40px; height: 4px; border-radius: 2px; background: rgba(127,127,127,0.4); margin: 4px auto 18px; }
.profhead { display: flex; align-items: center; gap: 14px; margin-bottom: 6px; }
.profavatar {
  width: 56px; height: 56px; border-radius: 50%; flex: 0 0 auto;
  background-color: var(--card2); background-size: cover; background-position: center;
  display: flex; align-items: center; justify-content: center; font-size: 21px; font-weight: 700;
}
.profavatar.haspic { color: transparent; }
.profwho { min-width: 0; }
.profname { font-size: 18px; font-weight: 700; word-break: break-word; }
.profemail { font-size: 13px; word-break: break-all; }
.profrow { font-size: 13px; padding: 14px 2px; margin-top: 14px; border-top: 1px solid rgba(127,127,127,0.14); }
.sheetbtns { display: flex; flex-direction: column; gap: 10px; margin-top: 10px; }
.sheetbtns button { width: 100%; padding: 14px; }
.ghostbtn { background: var(--card2); border: 1px solid rgba(127,127,127,0.2); }
.dangerbtn { background: rgba(224,121,111,0.15); color: var(--err); border: 1px solid rgba(224,121,111,0.35); }
.sheetclose { width: 100%; margin-top: 8px; background: transparent; color: var(--muted); }

/* Touch + polish */
@media (hover: none) { button:hover { filter: none; } }
.card { border-radius: 14px; }
input, textarea, select { font-size: 16px; }   /* ≥16px stops iOS zoom-on-focus */
