:root {
  --bg: #0d1117; --panel: #151b23; --panel2: #1c2430; --line: #2a3441; --text: #e6edf3; --muted: #8b98a8;
  --accent: #3b82f6; --ok: #22c55e; --warn: #f59e0b; --err: #ef4444; --crit: #d946ef;
  --radius: 10px; font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); font-size: 14px; }
.hidden { display: none !important; }
.muted { color: var(--muted); } .small { font-size: 12px; } .grow { flex: 1; }
.err { color: var(--err); min-height: 18px; }
code, pre, .mono { font-family: Consolas, "Cascadia Mono", monospace; }
a { color: var(--accent); }

.btn { background: var(--panel2); color: var(--text); border: 1px solid var(--line); border-radius: 7px; padding: 7px 12px; cursor: pointer; font: inherit; white-space: nowrap; }
.btn:hover { border-color: #3d4a5c; background: #222c3a; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: #2563eb; }
.btn.danger { color: #fca5a5; }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.small { padding: 4px 9px; font-size: 12px; }
.btn:disabled { opacity: .5; cursor: default; }
.btn-icon { width: 15px; height: 15px; margin-right: 5px; vertical-align: -3px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
input, select, textarea { background: var(--bg); color: var(--text); border: 1px solid var(--line); border-radius: 7px; padding: 7px 10px; font: inherit; }
.select.small { padding: 3px 6px; font-size: 12px; }

.dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; background: #4b5563; flex: none; }
.dot.on { background: var(--ok); box-shadow: 0 0 8px var(--ok); }
.dot.off { background: #4b5563; }
.chip { display: inline-flex; align-items: center; gap: 6px; background: var(--panel2); border: 1px solid var(--line); border-radius: 20px; padding: 4px 10px; font-size: 12px; }
.chip.warn b { color: var(--warn); }
.badge { display: inline-block; padding: 2px 7px; border-radius: 10px; font-size: 11px; background: var(--panel2); border: 1px solid var(--line); }
.badge.ok { color: var(--ok); border-color: #14532d; } .badge.err { color: var(--err); border-color: #7f1d1d; } .badge.warn { color: var(--warn); border-color: #78350f; }

/* login */
.login { position: fixed; inset: 0; display: grid; place-items: center; background: radial-gradient(circle at 30% 20%, #16233a, var(--bg)); }
.login-card { width: min(380px, 92vw); box-sizing: border-box; background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 28px; display: flex; flex-direction: column; gap: 12px; }
.login-card label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); }
.brand { font-weight: 700; font-size: 17px; display: flex; align-items: center; gap: 8px; }
.brand.big { font-size: 22px; white-space: nowrap; } .logo { color: var(--accent); }

/* top bar */
.topbar { display: flex; align-items: center; gap: 10px; padding: 10px 16px; border-bottom: 1px solid var(--line); background: var(--panel); position: sticky; top: 0; z-index: 5; flex-wrap: wrap; }
.stats { display: flex; gap: 8px; margin-left: 12px; }
.search { width: 220px; }
.conn { width: 10px; height: 10px; border-radius: 50%; background: var(--err); }
.conn.up { background: var(--ok); }

.layout { display: flex; height: calc(100vh - 57px); }
.main { flex: 1; overflow: auto; padding: 16px; }
.grid { display: flex; flex-direction: column; gap: 20px; }
.group-section { display: flex; flex-direction: column; gap: 10px; }
.group-card { background: linear-gradient(100deg, #182536, var(--panel)); border: 1px solid #30445c; border-radius: var(--radius); padding: 11px 14px; color: var(--text); font: inherit; text-align: left; cursor: pointer; display: flex; align-items: center; justify-content: space-between; }
.group-card:hover { border-color: var(--accent); }
.group-card b { font-size: 15px; }
.group-card span { color: var(--muted); font-size: 12px; margin-left: 10px; }
.group-card .group-chevron { margin-left: 16px; font-size: 16px; color: var(--text); }
.group-section.collapsed .group-devices { display: none; }
.group-devices { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 14px; }
.group-manager { display: flex; flex-direction: column; gap: 8px; margin-top: 6px; }
.group-manager-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--panel2); }
.group-manager-row > div:first-child { display: flex; flex-direction: column; gap: 2px; min-width: 0; overflow-wrap: anywhere; }
.group-manager-row .toolbar { margin: 0; flex-wrap: nowrap; }
.empty { text-align: center; padding: 80px 20px; color: var(--muted); }
.empty code { display: inline-block; margin-top: 10px; background: var(--panel); padding: 10px 14px; border-radius: 8px; color: var(--text); }

/* device card */
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; display: flex; flex-direction: column; gap: 10px; transition: border-color .15s; }
.card:hover { border-color: #3d4a5c; }
.card.offline { opacity: .6; }
.card.has-alert { border-left: 3px solid var(--err); }
.card-head { display: flex; align-items: center; gap: 8px; }
.card-name { font-weight: 600; font-size: 15px; cursor: pointer; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.card-name:hover { color: var(--accent); }
.card-sub { font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.meters { display: grid; grid-template-columns: 40px 1fr 44px; gap: 4px 8px; align-items: center; font-size: 12px; }
.bar { height: 6px; background: var(--bg); border-radius: 3px; overflow: hidden; }
.bar > i { display: block; height: 100%; background: var(--accent); border-radius: 3px; transition: width .4s; }
.bar > i.hi { background: var(--warn); } .bar > i.crit { background: var(--err); }
.card-tags { display: flex; gap: 6px; flex-wrap: wrap; }
.card-actions { display: flex; gap: 6px; }
.card-actions .btn { flex: 1; padding: 6px 4px; font-size: 12px; }
.sel { accent-color: var(--accent); }

/* alerts */
.alerts { width: 340px; border-left: 1px solid var(--line); background: var(--panel); display: flex; flex-direction: column; }
.alerts-head { display: flex; align-items: center; justify-content: space-between; padding: 12px 14px; border-bottom: 1px solid var(--line); }
.alerts-head h3 { margin: 0; font-size: 14px; }
.alert-list { overflow: auto; flex: 1; }
.alert { padding: 10px 14px; border-bottom: 1px solid var(--line); font-size: 12px; display: grid; grid-template-columns: 4px 1fr; gap: 10px; cursor: pointer; }
.alert:hover { background: var(--panel2); }
.alert .lv { border-radius: 2px; background: var(--muted); }
.alert.error .lv { background: var(--err); } .alert.critical .lv { background: var(--crit); } .alert.warning .lv { background: var(--warn); } .alert.info .lv { background: var(--accent); }
.alert.acked { opacity: .45; }
.alert b { font-size: 13px; }
.alert .msg { color: var(--muted); margin-top: 2px; word-break: break-word; }

/* drawer */
.drawer { position: fixed; inset: 0; left: max(0px, calc(100vw - 1180px)); background: var(--bg); border-left: 1px solid var(--line); z-index: 20; display: flex; flex-direction: column; box-shadow: -20px 0 60px #0009; }
.drawer-head { display: flex; align-items: center; gap: 12px; padding: 12px 16px; background: var(--panel); border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.drawer-head .title { font-size: 18px; font-weight: 600; display: flex; align-items: center; gap: 8px; }
.actions { display: flex; gap: 6px; flex-wrap: wrap; }
.tabs { display: flex; gap: 2px; padding: 0 12px; background: var(--panel); border-bottom: 1px solid var(--line); overflow-x: auto; }
.tabs button { background: none; border: none; color: var(--muted); padding: 10px 12px; cursor: pointer; font: inherit; border-bottom: 2px solid transparent; white-space: nowrap; }
.tabs button.active { color: var(--text); border-bottom-color: var(--accent); }
.tab-body { flex: 1; overflow: auto; padding: 16px; }
.toolbar { display: flex; gap: 8px; align-items: center; margin-bottom: 12px; flex-wrap: wrap; }

.menu { position: relative; }
.menu-items { display: none; position: absolute; right: 0; top: 100%; background: var(--panel2); border: 1px solid var(--line); border-radius: 8px; padding: 4px; z-index: 40; min-width: 200px; }
.menu:hover .menu-items, .menu:focus-within .menu-items { display: block; }
.menu-items button { display: block; width: 100%; text-align: left; background: none; border: none; color: var(--text); padding: 8px 10px; border-radius: 5px; cursor: pointer; font: inherit; }
.menu-items button:hover { background: var(--line); }

.panels { display: grid; grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); gap: 12px; }
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; }
.panel h4 { margin: 0 0 10px; font-size: 13px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.game-log-panels { grid-template-columns: repeat(2, minmax(0, 1fr)); width: 100%; }
.game-log-panels .panel { min-width: 0; }
.game-log-panels .t { width: 100%; table-layout: fixed; }
.game-log-panels .t th:first-child, .game-log-panels .t td:first-child { overflow-wrap: anywhere; }
.log-actions { display: inline-flex; gap: 4px; white-space: nowrap; }
.icon-btn { min-width: 30px; padding-inline: 7px; }
.kv { display: grid; grid-template-columns: max-content 1fr; gap: 5px 14px; font-size: 13px; }
.kv > div:nth-child(odd) { color: var(--muted); }
.kv > div { word-break: break-word; }
.big-num { font-size: 30px; font-weight: 700; }
.big-num small { font-size: 14px; color: var(--muted); font-weight: 400; }

table.t { width: 100%; border-collapse: collapse; font-size: 13px; }
table.t th { text-align: left; color: var(--muted); font-weight: 500; padding: 7px 8px; border-bottom: 1px solid var(--line); position: sticky; top: 0; background: var(--bg); cursor: pointer; }
table.t td { padding: 6px 8px; border-bottom: 1px solid #1d2530; vertical-align: top; word-break: break-word; }
table.t tr:hover td { background: var(--panel); }
.loading { color: var(--muted); padding: 30px; text-align: center; }
.spinner { display: inline-block; width: 14px; height: 14px; border: 2px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; vertical-align: -2px; margin-right: 6px; }
@keyframes spin { to { transform: rotate(360deg); } }
pre.log { background: #090c10; border: 1px solid var(--line); border-radius: 8px; padding: 12px; max-height: 65vh; overflow: auto; white-space: pre-wrap; font-size: 12px; margin: 0; }

/* terminal */
.term { background: #090c10; border: 1px solid var(--line); border-radius: 8px; display: flex; flex-direction: column; height: calc(100vh - 220px); min-height: 300px; }
.term-out { flex: 1; overflow: auto; padding: 12px; margin: 0; white-space: pre-wrap; word-break: break-all; font-size: 13px; line-height: 1.35; color: #d1d5db; }
.term-in { display: flex; border-top: 1px solid var(--line); align-items: center; }
.term-in span { padding: 0 10px; color: var(--ok); font-family: Consolas, monospace; }
.term-in input { flex: 1; background: transparent; border: none; padding: 10px 4px; font-family: Consolas, monospace; font-size: 13px; outline: none; }

/* remote desktop */
.rd { position: fixed; inset: 0; background: #000; z-index: 50; display: flex; flex-direction: column; }
.rd-bar { display: flex; align-items: center; gap: 10px; padding: 7px 12px; background: var(--panel); border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.rd-bar label { display: flex; align-items: center; gap: 4px; color: var(--muted); }
.rd-stage { flex: 1; display: grid; place-items: center; overflow: hidden; position: relative; }
.rd-stage img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; cursor: default; outline: none; user-select: none; }
.rd-stage img:focus { box-shadow: 0 0 0 2px var(--accent); }
.rd-msg { position: absolute; color: var(--muted); font-size: 16px; }

/* modal + toasts */
.modal { position: fixed; inset: 0; background: #0009; display: grid; place-items: center; z-index: 60; }
.modal-card { width: min(900px, 94vw); max-height: 88vh; background: var(--panel); border: 1px solid var(--line); border-radius: 12px; display: flex; flex-direction: column; }
.modal-card.wide { width: 98vw; max-height: 94vh; }
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 12px 16px; border-bottom: 1px solid var(--line); }
.modal-body { padding: 16px; overflow: auto; display: flex; flex-direction: column; gap: 10px; }
.modal-body img { max-width: 100%; border-radius: 6px; }
.modal-body textarea { min-height: 80px; resize: vertical; }
.game-log-viewer { min-height: 60vh; margin: 0; }
.toasts { position: fixed; bottom: 16px; right: 16px; display: flex; flex-direction: column; gap: 8px; z-index: 70; }
.toast { background: var(--panel2); border: 1px solid var(--line); border-left: 4px solid var(--accent); border-radius: 8px; padding: 10px 14px; min-width: 260px; max-width: 380px; box-shadow: 0 8px 30px #0008; animation: slide .2s; font-size: 13px; }
.toast.error { border-left-color: var(--err); } .toast.critical { border-left-color: var(--crit); } .toast.warning { border-left-color: var(--warn); } .toast.ok { border-left-color: var(--ok); }
@keyframes slide { from { transform: translateX(30px); opacity: 0; } }

@media (max-width: 900px) {
  .alerts { position: fixed; right: 0; top: 57px; bottom: 0; z-index: 10; width: min(340px, 100vw); }
  .search { width: 140px; }
  .drawer { left: 0; }
}
