:root {
  --fs: 15px;
  --bg: #0e0e12; --panel: #17171d; --panel2: #1f1f27; --line: #2b2b36;
  --fg: #ececf1; --muted: #9a9aaa; --accent: #8f6bff; --ok: #3ccf7a; --warn: #f3b33d; --err: #ff5a5f;
  --tw: #9146ff; --yt: #ff0033; --tt: #fe2c55; --bl: #00a1d6; --x: #8b98a5; --se: #e8a33d;
  --safe-t: env(safe-area-inset-top, 0px); --safe-b: env(safe-area-inset-bottom, 0px);
  color-scheme: dark;
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; overscroll-behavior: none; }
body {
  background: var(--bg); color: var(--fg);
  font: 400 var(--fs)/1.4 system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans JP", "Noto Sans SC", sans-serif;
  display: grid; grid-template-rows: auto auto minmax(0, 1fr) auto; grid-template-columns: minmax(0, 1fr); overflow: hidden;
  -webkit-tap-highlight-color: transparent;
  /* pinned to the visual viewport so the composer stays above the on-screen keyboard (iOS + Android) */
  position: fixed; left: 0; right: 0; top: var(--app-top, 0px); height: var(--app-h, 100dvh);
  padding-left: env(safe-area-inset-left, 0px); padding-right: env(safe-area-inset-right, 0px);
}
button, input, select, textarea { font: inherit; color: inherit; }
a { color: #b9a6ff; }
[hidden] { display: none !important; }

/* ---------- header ---------- */
#top {
  display: flex; align-items: center; gap: 8px; padding: calc(6px + var(--safe-t)) 8px 6px;
  background: var(--panel); border-bottom: 1px solid var(--line);
}
.brand { font-weight: 800; letter-spacing: .5px; background: var(--accent); color: #fff; border-radius: 8px; padding: 2px 7px; font-size: 13px; }
.pills { display: flex; gap: 6px; overflow-x: auto; flex: 1; min-width: 0; scrollbar-width: none; }
.pills::-webkit-scrollbar { display: none; }
.pill {
  display: inline-flex; align-items: center; gap: 4px; border: 1px solid var(--line); background: var(--panel2);
  border-radius: 999px; padding: 2px 8px 2px 3px; white-space: nowrap; cursor: pointer; font-size: 12px;
}
.pill.muted { opacity: .35; }
.pill .dot { width: 8px; height: 8px; border-radius: 50%; background: #555; }
.pill.st-live .dot { background: var(--ok); box-shadow: 0 0 6px var(--ok); }
.pill.st-connecting .dot { background: var(--warn); animation: pulse 1s infinite; }
.pill.st-error .dot { background: var(--err); }
.pill.st-offline .dot { background: #777; }
.pill .vc { color: var(--muted); font-variant-numeric: tabular-nums; }
@keyframes pulse { 50% { opacity: .3; } }
.top-actions { display: flex; gap: 4px; }
.iconbtn { background: transparent; border: 1px solid var(--line); border-radius: 8px; width: 34px; height: 30px; cursor: pointer; }
.iconbtn.on { background: var(--warn); color: #000; border-color: var(--warn); }

.pchip {
  display: inline-grid; place-items: center; min-width: 22px; height: 18px; padding: 0 4px; border-radius: 5px;
  font-size: 10px; font-weight: 800; color: #fff; letter-spacing: .3px; flex: none;
}

.adbar { display: flex; gap: 10px; align-items: center; padding: 4px 10px; background: #231a33; font-size: 13px; border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.ad-soon { color: var(--warn); font-weight: 700; }
.ad-live { color: #000; background: var(--warn); padding: 1px 6px; border-radius: 4px; font-weight: 800; }
.hype { color: #ffb3f0; font-weight: 600; }

/* ---------- main / views ---------- */
#main { min-height: 0; min-width: 0; position: relative; display: grid; grid-template-columns: minmax(0, 1fr); }
.view { min-height: 0; overflow-y: auto; }
.pad { padding: 10px; }
/* explicit rows: a hidden #adbar must not shift the tab bar into the flexible row */
#top { grid-row: 1; } #adbar { grid-row: 2; } #main { grid-row: 3; } #tabs { grid-row: 4; }

/* layout is set by main.js on <body data-layout>: 1 = phone portrait, 2 = split (desktop / phone landscape), 3 = wide desktop */
body[data-layout="2"] #main { grid-template-columns: minmax(0, 1.25fr) minmax(0, 1fr); }
body[data-layout="3"] #main { grid-template-columns: minmax(380px, 1.25fr) minmax(360px, 1fr) minmax(300px, .8fr); }
body:not([data-layout="1"]) .view { min-width: 0; }
body:not([data-layout="1"]) #view-chat { grid-column: 1; grid-row: 1; border-right: 1px solid var(--line); }
body:not([data-layout="1"]) .view:not(#view-chat) { grid-column: 2; grid-row: 1; }
body[data-layout="3"] #view-events { grid-column: 3; border-left: 1px solid var(--line); }
body:not([data-layout="1"]) .tabs .tab-chat, body[data-layout="3"] .tabs [data-tab="events"] { display: none; }

/* phone in landscape (handlebar / chest mount): tabs become a left rail, header slims down */
@media (orientation: landscape) and (max-height: 540px) {
  body { grid-template-columns: auto minmax(0, 1fr); grid-template-rows: auto auto minmax(0, 1fr); }
  #top { grid-column: 1 / -1; grid-row: 1; padding-top: 4px; padding-bottom: 4px; }
  #adbar { grid-column: 1 / -1; grid-row: 2; }
  #main { grid-column: 2; grid-row: 3; }
  #tabs {
    grid-column: 1; grid-row: 3; grid-auto-flow: row; grid-auto-rows: 1fr; grid-auto-columns: auto;
    width: 62px; border-top: 0; border-right: 1px solid var(--line); padding-bottom: var(--safe-b);
  }
  .tabs button { box-shadow: none; }
  .tabs button.on { box-shadow: inset 3px 0 var(--accent); }
  .composer { padding-top: 5px; padding-bottom: 5px; }
}

/* ---------- chat ---------- */
.chat-view { display: grid; grid-template-rows: auto auto minmax(0, 1fr) auto auto; grid-template-columns: minmax(0, 1fr); position: relative; min-height: 0; }
#monitorBar { grid-row: 1; } #player { grid-row: 2; } #chatList { grid-row: 3; } #replyBar { grid-row: 4; } #composer { grid-row: 5; }
.chat-list { overflow-y: auto; padding: 6px 8px 10px; overscroll-behavior: contain; min-height: 0; }
.msg { padding: 3px 4px; border-radius: 6px; word-wrap: break-word; overflow-wrap: anywhere; cursor: pointer; }
.msg:hover { background: #ffffff08; }
.msg > * { vertical-align: middle; }
.msg .ts { color: var(--muted); font-size: .78em; margin-right: 4px; font-variant-numeric: tabular-nums; }
.msg .pchip { margin-right: 4px; transform: translateY(-1px); }
.msg .avatar { width: 1.4em; height: 1.4em; border-radius: 50%; margin-right: 4px; object-fit: cover; }
.badges .badge { height: 1.1em; margin-right: 2px; vertical-align: -2px; }
.tbadge { font-size: .7em; font-weight: 800; padding: 0 4px; border-radius: 4px; margin-right: 3px; background: #444; color: #fff; vertical-align: 1px; }
.tbadge.b-owner { background: #e91916; } .tbadge.b-mod { background: #00ad03; } .tbadge.b-vip { background: #e005b9; }
.tbadge.b-sub { background: #6441a5; } .tbadge.b-medal { background: #3a7bd5; }
.tbadge.b-collab { background: #00a3c4; text-transform: none; }
.msg .name { font-weight: 700; }
.msg .amount { background: var(--warn); color: #000; font-weight: 800; border-radius: 4px; padding: 0 5px; margin-left: 5px; font-size: .85em; }
.msg.action .text { font-style: italic; }
.msg .emote { height: 1.8em; vertical-align: middle; margin: -4px 1px; }
.msg.history { opacity: .55; }
.msg.deleted .text { text-decoration: line-through; opacity: .4; }
.msg.hl-first { background: #3ccf7a1a; box-shadow: inset 3px 0 var(--ok); }
.msg.hl-mention { background: #ff5a5f22; box-shadow: inset 3px 0 var(--err); }
.msg.hl-super, .msg.hl-bits { background: #f3b33d1f; box-shadow: inset 3px 0 var(--warn); }
.msg.hl-collab { background: #00c2ff14; box-shadow: inset 3px 0 #00c2ff; }
.msg.hl-sub, .msg.hl-raid, .msg.hl-event, .msg.hl-follow, .msg.hl-redeem, .msg.hl-ad { background: #8f6bff18; box-shadow: inset 3px 0 var(--accent); }
.msg.k-system .text { color: var(--muted); font-style: italic; }
.msg.k-event .text { font-weight: 600; }
.new-pill {
  position: absolute; left: 50%; transform: translateX(-50%); bottom: 64px; z-index: 5;
  background: var(--accent); color: #fff; border: 0; border-radius: 999px; padding: 6px 14px; font-weight: 700; box-shadow: 0 4px 16px #0008;
}
.reply-bar { display: flex; justify-content: space-between; align-items: center; gap: 8px; padding: 4px 10px; background: var(--panel2); font-size: .85em; color: var(--muted); }
.composer { display: flex; gap: 6px; padding: 8px 8px calc(8px); background: var(--panel); border-top: 1px solid var(--line); }
.composer input { flex: 1; min-width: 0; }

/* ---------- monitor strip + live view ---------- */
.monitor { display: flex; flex-wrap: wrap; gap: 6px; padding: 6px 8px; background: var(--panel); border-bottom: 1px solid var(--line); }
.mcell { flex: none; background: var(--panel2); border: 1px solid var(--line); border-radius: 10px; padding: 4px 10px; min-width: 0; text-align: left; color: inherit; font: inherit; }
.mcell .mv { font-weight: 800; font-size: 1.25em; font-variant-numeric: tabular-nums; line-height: 1.15; white-space: nowrap; }
.mcell .mv .unit { font-size: .6em; font-weight: 700; color: var(--muted); }
.mcell .ml { font-size: .68em; color: var(--muted); white-space: nowrap; max-width: 46vw; overflow: hidden; text-overflow: ellipsis; }
.mcell.dur.on { border-color: #6e2228; }
.mcell.dur.on .ml { color: var(--err); font-weight: 800; }
.mcell.br { flex: 1 1 140px; min-width: 0; overflow: hidden; }
.mcell.br .ml { max-width: none; }
.mcell.vw { flex: 1 1 auto; }
.links { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 4px; }
.link { font-size: .66em; font-weight: 700; padding: 1px 6px; border-radius: 999px; background: #ffffff10; font-variant-numeric: tabular-nums; white-space: nowrap; }
.link.dead { background: #3a1517; color: #ffb4b6; }
.mcell.br .meter { height: 4px; background: #ffffff14; border-radius: 3px; margin-top: 3px; overflow: hidden; }
.mcell.br .fill { height: 100%; background: var(--ok); transition: width .6s; }
.mcell.br.low .fill { background: var(--warn); } .mcell.br.low .mv { color: var(--warn); }
.mcell.br.alarm { border-color: var(--err); background: #3a1517; animation: pulse 1s infinite; }
.mcell.br.alarm .fill { background: var(--err); } .mcell.br.alarm .mv { color: #ffb4b6; }
.vrow { display: flex; gap: 8px; align-items: center; white-space: nowrap; }
.vchip { display: inline-flex; align-items: center; gap: 3px; font-variant-numeric: tabular-nums; font-size: 1.05em; }
.vchip .pchip { min-width: 18px; height: 15px; font-size: 8px; }
.vchip.total { color: var(--muted); }
.vchip.total b { color: var(--fg); }
.tvbtn { cursor: pointer; text-align: center; }
.tvbtn.on { border-color: var(--accent); }
.player { position: relative; background: #000; overflow: hidden; border-bottom: 1px solid var(--line); }
.player .pinner { transform-origin: 0 0; }
.player .pframe { display: block; width: 100%; aspect-ratio: 16 / 9; border: 0; object-fit: contain; background: #000; }
.player .pmsg { padding: 14px; color: var(--muted); text-align: center; font-size: .9em; }
@media (orientation: landscape) and (max-height: 540px) {
  .monitor { padding: 4px 6px; gap: 4px; } .mcell { padding: 2px 8px; } .mcell .mv { font-size: 1.05em; }
  .mcell.dur .ml, .mcell.vw .ml, .tvbtn .ml { display: none; }
  .links { margin-top: 2px; }
}

/* ---------- events ---------- */
.event-list { padding: 0 8px 12px; }
.event { display: flex; gap: 6px; align-items: center; padding: 6px; border-bottom: 1px solid var(--line); }
.event .ts { color: var(--muted); font-size: .78em; font-variant-numeric: tabular-nums; }
.event .ev-body { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.event .ev-detail { color: var(--muted); }
.event.hl-super { background: #f3b33d14; }
.event.hl-sub, .event.hl-raid { background: #8f6bff14; }
.event.hl-collab { background: #00c2ff12; }
.collab-on { color: #00c2ff; font-weight: 700; }
.linkbox { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: .78em; word-break: break-all; background: var(--panel2); border: 1px solid var(--line); border-radius: 8px; padding: 8px; }
.preview-frame { width: 100%; aspect-ratio: 16 / 9; border: 1px solid var(--line); border-radius: 10px; background: #111; }
.typechips label { display: inline-flex; align-items: center; gap: 4px; font-size: .9em; }

/* ---------- controls ---------- */
input, select, textarea {
  background: var(--panel2); border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; min-height: 38px; width: 100%;
}
input[type=checkbox], input[type=range] { width: auto; min-height: 0; }
input[type=checkbox] { width: 20px; height: 20px; accent-color: var(--accent); }
input[type=range] { accent-color: var(--accent); }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.num { width: 76px !important; }
.btn {
  background: var(--panel2); border: 1px solid var(--line); border-radius: 8px; padding: 8px 12px; cursor: pointer;
  min-height: 38px; white-space: nowrap; font-weight: 600;
}
.btn:hover { border-color: #444; }
.btn:disabled { opacity: .4; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.danger { background: #3a1517; border-color: #6b2226; color: #ffb4b6; }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.sm { padding: 5px 10px; min-height: 32px; font-size: .9em; }
.btn.xs { padding: 2px 7px; min-height: 26px; font-size: .8em; }
.row { display: flex; align-items: center; }
.row.between { justify-content: space-between; }
.gap { gap: 8px; } .wrap { flex-wrap: wrap; } .grow { flex: 1; min-width: 0; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; margin: 6px 0; }
.muted { color: var(--muted); } .small { font-size: .85em; }
.warn { color: var(--warn); }
.ellipsis { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 12px; margin-bottom: 10px; }
.card h3 { margin: 0 0 8px; font-size: 1.05em; }
.card h4 { margin: 14px 0 4px; font-size: .9em; color: var(--muted); text-transform: uppercase; letter-spacing: .5px; }
.list .item { padding: 6px 0; border-bottom: 1px solid var(--line); }
.list .item:last-child { border-bottom: 0; }
.form { display: grid; gap: 10px; margin-top: 10px; }
.form label { display: grid; gap: 4px; font-size: .9em; }
.form label.row { display: flex; }
.form small { font-weight: 400; }
details.setting > summary { cursor: pointer; font-weight: 700; list-style: none; display: flex; align-items: center; gap: 4px; }
details.setting > summary::-webkit-details-marker { display: none; }
details.setting > summary::after { content: '▸'; margin-left: auto; color: var(--muted); }
details.setting[open] > summary::after { content: '▾'; }
.st { font-weight: 700; } .st-live { color: var(--ok); } .st-error { color: var(--err); } .st-connecting { color: var(--warn); }
.statgrid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; }
.stat { background: var(--panel2); border-radius: 8px; padding: 6px; text-align: center; }
.stat.red .stat-v { color: var(--err); }
.stat-v { font-weight: 800; font-variant-numeric: tabular-nums; font-size: .95em; }
.stat-l { color: var(--muted); font-size: .72em; }
.spark { width: 100%; height: 50px; color: var(--ok); margin-top: 8px; }
.vol { width: 120px; }
.db { width: 64px; text-align: right; color: var(--muted); font-size: .8em; font-variant-numeric: tabular-nums; }
.toggle { width: 22px; height: 22px; }
.bar { position: relative; background: var(--panel2); border-radius: 6px; margin: 4px 0; padding: 5px 8px; overflow: hidden; }
.bar-fill { position: absolute; inset: 0 auto 0 0; background: #8f6bff55; }
.bar span { position: relative; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.quote { background: var(--panel2); padding: 8px 10px; border-radius: 8px; overflow-wrap: anywhere; }
@media (max-width: 480px) { .statgrid { grid-template-columns: repeat(2, 1fr); } }

/* ---------- touch devices: 44px targets, no iOS focus-zoom (inputs ≥16px), no double-tap zoom ---------- */
button, a, .tile, .msg, label, summary { touch-action: manipulation; }
@media (pointer: coarse) {
  input, select, textarea { font-size: 16px; min-height: 44px; }
  input[type=checkbox] { width: 24px; height: 24px; min-height: 0; }
  .btn { min-height: 44px; }
  .btn.sm { min-height: 40px; padding: 6px 12px; }
  .btn.xs { min-height: 34px; padding: 4px 10px; font-size: .85em; }
  .iconbtn { width: 40px; height: 36px; }
  .pill { min-height: 30px; }
  .msg { padding: 4px 4px; }
  .tile { min-height: 96px; }
  .sheet .btn { min-height: 48px; }
  .event { padding: 8px 6px; }
}

/* ---------- board ---------- */
.board-tools { margin-bottom: 10px; }
.board-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 8px; }
.tile {
  position: relative; display: flex; flex-direction: column; justify-content: center; align-items: center; gap: 2px;
  min-height: 84px; padding: 10px; border-radius: 12px; border: 1px solid var(--line); background: var(--panel2);
  font-weight: 700; text-align: center; cursor: pointer; user-select: none; transition: transform .06s;
}
.tile:active { transform: scale(.97); }
.tile .tile-label { font-size: 1.02em; overflow-wrap: anywhere; }
.tile .tile-kind { font-size: .68em; color: var(--muted); font-weight: 600; }
.tile.slider { cursor: default; }
.tile.slider input { width: 100%; }
.tile.busy { opacity: .5; }
.tile.active { outline: 3px solid var(--ok); outline-offset: -3px; }
.tile.red { background: #3b1518; border-color: #6e2228; } .tile.orange { background: #3a2610; border-color: #6e4618; }
.tile.green { background: #12301e; border-color: #1f5a36; } .tile.blue { background: #10243d; border-color: #1c4170; }
.tile.purple { background: #24183d; border-color: #43307a; } .tile.pink { background: #3a1430; border-color: #6c2559; }
.tile.gray { background: #23232a; }
.edit-ctrl { display: flex; gap: 4px; margin-top: 6px; }

/* ---------- tabs ---------- */
.tabs {
  display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; background: var(--panel); border-top: 1px solid var(--line);
  padding-bottom: var(--safe-b);
}
.tabs button {
  position: relative; background: none; border: 0; padding: 6px 2px; color: var(--muted); font-weight: 700; font-size: .8em; cursor: pointer;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 1px; min-height: 50px;
}
.tabs .ti { font-size: 1.35em; line-height: 1; filter: grayscale(.6); }
.tabs button.on .ti { filter: none; }
.tabs .tb {
  position: absolute; top: 4px; left: calc(50% + 8px); min-width: 18px; height: 18px; padding: 0 5px; border-radius: 999px;
  background: var(--err); color: #fff; font-size: 11px; line-height: 18px; text-align: center;
}
.tabs button.on { color: var(--fg); box-shadow: inset 0 2px var(--accent); }

/* ---------- sheets / toasts ---------- */
.sheet-wrap { position: fixed; inset: 0; background: #000a; z-index: 50; display: flex; align-items: flex-end; justify-content: center; }
.sheet {
  background: var(--panel); width: min(560px, 100%); max-height: 85vh; overflow: auto; border-radius: 16px 16px 0 0;
  border: 1px solid var(--line); padding: 12px 14px calc(14px + var(--safe-b));
}
@media (min-width: 700px) { .sheet-wrap { align-items: center; } .sheet { border-radius: 16px; } }
.sheet-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }
#toasts { position: fixed; top: calc(52px + var(--safe-t)); left: 50%; transform: translateX(-50%); z-index: 60; display: grid; gap: 6px; width: min(92vw, 420px); pointer-events: none; }
.toast { background: #2a2a35; border: 1px solid var(--line); border-radius: 10px; padding: 8px 12px; box-shadow: 0 6px 20px #0009; transition: opacity .35s, transform .35s; }
.toast.ok { border-color: #1f5a36; } .toast.error { border-color: #6e2228; background: #2e1a1c; }
.toast.out { opacity: 0; transform: translateY(-8px); }

/* relay auto-detect status (Settings → Monitor) */
.relay-status { padding: 8px 10px; border-radius: 8px; background: #ffffff0d; color: var(--muted); line-height: 1.4; word-break: break-word; }
.relay-status.live { color: var(--ok); background: #1f3a2a; }
.relay-status.error { color: #ffb4b6; background: #3a1517; }
