:root {
  --bg: #1e2030;
  --surface: #24273a;
  --surface2: #363a4f;
  --text: #cad3f5;
  --subtext: #a5adcb;
  --dim: #6e738d;
  --red: #ed8796;
  --green: #a6da95;
  --yellow: #eed49f;
  --blue: #8aadf4;
  --sab: env(safe-area-inset-bottom, 0px);
}
* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }
html, body { height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.4 system-ui, sans-serif;
  overscroll-behavior: none;
}
button { font: inherit; color: inherit; background: none; border: none; cursor: pointer; }

#app { display: flex; flex-direction: column; height: 100dvh; }
#topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(env(safe-area-inset-top, 0px) + 10px) 16px 8px;
}
.logo { font-weight: 700; font-size: 17px; color: var(--blue); }
.conn { width: 9px; height: 9px; border-radius: 50%; background: var(--dim); }
.conn.ok { background: var(--green); }
.conn.err { background: var(--red); }

#tabs { display: flex; gap: 6px; padding: 4px 12px 8px; overflow-x: auto; scrollbar-width: none; }
#tabs::-webkit-scrollbar { display: none; }
.tab {
  flex: 0 0 auto; display: flex; align-items: center; gap: 6px;
  padding: 7px 14px; border-radius: 18px; background: var(--surface);
  color: var(--subtext); font-weight: 600; font-size: 14px;
  border: 1px solid transparent;
}
.tab.active { color: var(--text); border-color: var(--tabcolor); background: var(--surface2); }
.tab .badge {
  min-width: 18px; height: 18px; border-radius: 9px; background: var(--red);
  color: #1e2030; font-size: 11px; font-weight: 700;
  display: flex; align-items: center; justify-content: center; padding: 0 5px;
}

#limits { padding: 0 16px 6px; font-size: 12px; color: var(--subtext); }
.limitrow { display: flex; align-items: center; gap: 8px; margin-bottom: 3px; }
.limitrow .lbl { width: 24px; color: var(--dim); }
.limitrow .val { width: 38px; text-align: right; }
.limitrow .reset { color: var(--dim); }
.bar { flex: 1; height: 5px; border-radius: 3px; background: var(--surface2); overflow: hidden; }
.bar i { display: block; height: 100%; border-radius: 3px; background: var(--green); }
.bar i.warn { background: var(--yellow); }
.bar i.crit { background: var(--red); }
#limits.stale { opacity: 0.45; }

#sessions { flex: 1; overflow-y: auto; padding: 4px 12px calc(90px + var(--sab)); }
.card {
  background: var(--surface); border-radius: 14px; padding: 12px 14px;
  margin-bottom: 10px; border-left: 4px solid var(--cardcolor, var(--surface2));
}
.card .row1 { display: flex; align-items: center; gap: 8px; }
.card .title { font-weight: 650; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.card .title input {
  width: 100%; background: var(--surface2); border: none; color: var(--text);
  font: inherit; border-radius: 6px; padding: 2px 6px;
}
.card .win { font-size: 12px; color: var(--dim); background: var(--surface2); padding: 2px 8px; border-radius: 9px; }
.card .menu-btn { color: var(--dim); font-size: 19px; padding: 0 4px; }
.dot { width: 10px; height: 10px; border-radius: 50%; flex: 0 0 auto; }
.dot.busy { background: var(--yellow); animation: pulse 1.2s infinite; }
.dot.idle, .dot.shell { background: var(--dim); }
.dot.waiting { background: var(--red); }
.dot.done { background: var(--green); }
.dot.unknown { background: var(--surface2); }
@keyframes pulse { 50% { opacity: 0.35; } }
.card .row2 { font-size: 12.5px; color: var(--subtext); margin-top: 3px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.card .row3 { display: flex; align-items: center; gap: 8px; margin-top: 7px; font-size: 12px; color: var(--subtext); }
.card .row3 .bar { max-width: 90px; }
.card .row3.stale { opacity: 0.45; }
.card .actions { display: flex; gap: 8px; margin-top: 9px; }
.card .actions button {
  background: var(--surface2); border-radius: 8px; padding: 6px 12px; font-size: 13px;
}
.card .actions button.danger { color: var(--red); }
.empty { text-align: center; color: var(--dim); margin-top: 40px; }

#fab {
  position: fixed; right: 18px; bottom: calc(18px + var(--sab));
  width: 54px; height: 54px; border-radius: 50%;
  background: var(--blue); color: #1e2030; font-size: 30px; font-weight: 600;
  box-shadow: 0 4px 14px rgba(0,0,0,0.4);
}

#termview { position: fixed; inset: 0; background: #181926; display: flex; flex-direction: column; }
#termbar {
  display: flex; align-items: center; gap: 10px;
  padding: calc(env(safe-area-inset-top, 0px) + 6px) 12px 6px;
}
#term-back { font-size: 26px; line-height: 1; color: var(--blue); padding: 0 6px; }
#term-title { flex: 1; font-weight: 650; font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#term { flex: 1; min-height: 0; padding: 0 2px; }
#term .xterm { height: 100%; }
#keysrow { display: flex; gap: 5px; padding: 6px 8px 2px; overflow-x: auto; scrollbar-width: none; }
#keysrow::-webkit-scrollbar { display: none; }
#keysrow button {
  flex: 0 0 auto; min-width: 40px; padding: 7px 10px; border-radius: 8px;
  background: var(--surface); color: var(--subtext); font-size: 13.5px; font-weight: 600;
}
#keysrow button:active { background: var(--surface2); color: var(--text); }
#inputrow { display: flex; gap: 6px; padding: 6px 8px calc(8px + var(--sab)); align-items: flex-end; }
#prompt-input {
  flex: 1; background: var(--surface); border: 1px solid var(--surface2);
  border-radius: 10px; color: var(--text); font: 14px/1.35 system-ui, sans-serif;
  padding: 9px 12px; resize: none; max-height: 40dvh;
}
#prompt-input:focus { outline: none; border-color: var(--blue); }
#inputrow button {
  background: var(--blue); color: #1e2030; font-weight: 700; font-size: 14px;
  border-radius: 10px; padding: 10px 14px;
}
#inputrow #enter-btn { background: var(--surface2); color: var(--text); }
#inputrow #mic-btn { background: var(--surface2); color: var(--text); font-size: 16px; padding: 10px 12px; }
#inputrow #mic-btn.rec { background: var(--red); animation: pulse 1.2s infinite; }

.overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,0.55);
  display: flex; align-items: center; justify-content: center; padding: 24px; z-index: 50;
}
.dialog {
  background: var(--surface); border-radius: 16px; padding: 20px;
  width: 100%; max-width: 420px; max-height: 80dvh; overflow-y: auto;
}
.dialog h2 { font-size: 16px; margin-bottom: 10px; }
.dialog p { font-size: 13px; color: var(--subtext); margin-bottom: 12px; }
.dialog code { background: var(--surface2); padding: 1px 5px; border-radius: 4px; font-size: 12px; }
.dialog input {
  width: 100%; background: var(--bg); border: 1px solid var(--surface2);
  border-radius: 8px; color: var(--text); font: inherit; padding: 10px 12px; margin-bottom: 12px;
}
.dialog > button {
  background: var(--blue); color: #1e2030; font-weight: 700;
  border-radius: 10px; padding: 10px 16px; width: 100%;
}
.dialog > button.secondary, #spawn-cancel { background: var(--surface2); color: var(--text); }
#spawn-dirs { display: flex; flex-direction: column; gap: 6px; margin-bottom: 12px; }
#spawn-dirs button {
  background: var(--bg); border-radius: 8px; padding: 11px 14px;
  text-align: left; font-size: 14px;
}
#spawn-dirs button:active { background: var(--surface2); }

#term-copy { color: var(--subtext); font-size: 19px; padding: 2px 8px; }
#term-copy:active { color: var(--blue); }
#term-cmd { color: var(--subtext); font-size: 19px; font-weight: 700; padding: 2px 8px; }
#term-cmd:active { color: var(--blue); }

#term-models { display: flex; gap: 4px; flex: 0 0 auto; }
#term-models button {
  padding: 5px 9px; border-radius: 7px; background: var(--surface);
  border: 1px solid transparent; color: var(--dim);
  font-size: 11.5px; font-weight: 700; letter-spacing: 0.02em;
}
#term-models button.active { background: var(--surface2); border-color: var(--blue); color: var(--text); }
#term-models button.pending { opacity: 0.5; }
#term-models button.fail { border-color: var(--red); color: var(--red); }

.dialog.cmd { max-width: 480px; }
.cmd-sec { margin-top: 14px; }
.cmd-sec h3 { font-size: 11.5px; font-weight: 700; color: var(--dim);
  text-transform: uppercase; letter-spacing: 0.05em; margin-bottom: 8px; }
.cmd-grid { display: flex; flex-wrap: wrap; gap: 7px; }
.cmd-grid button {
  background: var(--bg); border: 1px solid transparent; border-radius: 9px;
  padding: 11px 16px; font-size: 14px; font-weight: 600;
}
.cmd-grid button:active { background: var(--surface2); }
.cmd-grid button.active { background: var(--surface2); border-color: var(--blue); }
.cmd-grid button.danger { color: var(--red); }
.cmd-grid button:disabled { opacity: 0.6; }
.cmd-note { font-size: 12px; color: var(--dim); margin-top: 8px; }

.dialog.copy { max-width: 640px; max-height: 88dvh; padding: 14px; }
.copy-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; }
.copy-head h2 { margin: 0; }
#copy-close { color: var(--subtext); font-size: 17px; padding: 2px 6px; }
.copy-info { color: var(--dim); font-size: 13px; padding: 14px 2px; text-align: center; }
.copy-block { background: var(--bg); border-radius: 12px; padding: 10px 12px; margin-bottom: 10px; }
.copy-block-head { display: flex; align-items: baseline; gap: 8px; margin-bottom: 6px; }
.copy-title { flex: 1; font-size: 12.5px; color: var(--subtext); overflow: hidden;
  text-overflow: ellipsis; white-space: nowrap; }
.copy-meta { font-size: 11.5px; color: var(--dim); }
.copy-chev { font-size: 12px; color: var(--dim); padding-left: 2px; }
.copy-text {
  font: 12.5px/1.45 ui-monospace, monospace; color: var(--text);
  white-space: pre-wrap; word-break: break-word; user-select: text; -webkit-user-select: text;
  max-height: 4.5em; overflow: hidden; position: relative;
  -webkit-mask-image: linear-gradient(#000 60%, transparent);
  mask-image: linear-gradient(#000 60%, transparent);
}
.copy-text.open { max-height: 34dvh; overflow-y: auto; -webkit-mask-image: none; mask-image: none; }
.copy-acts { display: flex; gap: 7px; margin-top: 9px; }
.copy-acts button {
  background: var(--surface2); color: var(--text); border-radius: 8px;
  padding: 8px 14px; font-size: 13px; font-weight: 600;
}
.copy-acts button.primary { background: var(--blue); color: #1e2030; }
.copy-acts button.done { background: var(--green); color: #1e2030; }
.copy-acts button.fail { background: var(--yellow); color: #1e2030; }
.copy-chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.copy-chips button {
  background: var(--surface2); color: var(--subtext); border-radius: 7px;
  padding: 6px 10px; font: 12px/1 ui-monospace, monospace; max-width: 100%;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.copy-chips button.done { background: var(--green); color: #1e2030; }
.copy-chips button.fail { background: var(--yellow); color: #1e2030; }
