:root {
  --bg: #0f1115;
  --surface: #171a21;
  --surface-2: #1f232c;
  --border: #2a2f3a;
  --text: #e8eaef;
  --muted: #9aa1ae;
  --accent: #6aa7ff;
  --accent-text: #0b1220;
  --ok: #3ecf8e;
  --warn: #f5b84b;
  --bad: #ff6b6b;
  --radius: 12px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); }
body {
  font: 15px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior: none;
}
[hidden] { display: none !important; }
button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; -webkit-tap-highlight-color: transparent; }
code { font-family: ui-monospace, Menlo, Consolas, monospace; font-size: .9em; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
.muted { color: var(--muted); }
.error { color: var(--bad); min-height: 1.4em; margin: 0; }

.view { height: 100vh; height: 100dvh; }
.center { display: flex; align-items: center; justify-content: center; padding: 16px; }

.card {
  width: 100%; max-width: 360px; background: var(--surface); border: 1px solid var(--border);
  border-radius: 16px; padding: 24px; display: flex; flex-direction: column; gap: 12px; text-align: center;
}
.card h1 { margin: 0; font-size: 22px; }
.card p { margin: 0; }
.logo { width: 64px; height: 64px; border-radius: 14px; align-self: center; }
.card input {
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 10px;
  padding: 12px 14px; font-size: 16px; /* 16px stops iOS zooming on focus */
}

.btn {
  border: 1px solid var(--border); background: var(--surface-2); border-radius: 10px;
  padding: 10px 14px; font-weight: 600; min-height: 44px;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.danger { background: var(--bad); border-color: var(--bad); color: #fff; }
.btn.wide { width: 100%; }
.btn:disabled { opacity: .5; }
.link { background: none; border: 0; color: var(--muted); padding: 6px 4px; font-size: 13px; }
.link:hover, .link:focus-visible { color: var(--text); }

/* ---- main layout ------------------------------------------------------- */
#main-view { display: flex; flex-direction: column; }

.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: calc(6px + var(--safe-top)) 10px 6px; background: var(--surface); border-bottom: 1px solid var(--border);
}
.status { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); }
.status-label { margin-right: 6px; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.dot.ok { background: var(--ok); }
.dot.warn { background: var(--warn); }
.dot.bad { background: var(--bad); }
.tools { display: flex; gap: 4px; }
.icon-btn {
  width: 40px; height: 40px; border-radius: 10px; border: 1px solid transparent; background: none;
  display: grid; place-items: center;
}
.icon-btn svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.icon-btn[aria-pressed="true"] { background: var(--surface-2); border-color: var(--border); color: var(--accent); }
.icon-btn.danger { color: var(--bad); }

.keybar {
  display: flex; gap: 6px; overflow-x: auto; padding: 6px 10px; background: var(--surface);
  border-bottom: 1px solid var(--border); scrollbar-width: none;
}
.keybar::-webkit-scrollbar { display: none; }
.keybar button {
  flex: none; min-width: 44px; height: 36px; padding: 0 10px; border-radius: 8px;
  border: 1px solid var(--border); background: var(--surface-2); font-size: 13px; font-weight: 600;
}
.keybar button.armed { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }

.screen-wrap { position: relative; flex: 1 1 auto; min-height: 120px; background: #000; overflow: hidden; }
#screen { position: absolute; inset: 0; }
.overlay {
  position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 12px; background: rgba(0, 0, 0, .72); text-align: center; padding: 16px;
}
.overlay p { margin: 0; color: var(--muted); max-width: 320px; }

/* ---- AI panel ----------------------------------------------------------- */
.ai {
  flex: none; display: flex; flex-direction: column; background: var(--surface);
  border-top: 1px solid var(--border); padding: 0 10px calc(6px + var(--safe-bottom));
  max-height: 60%;
}
.ai-handle {
  display: flex; align-items: center; justify-content: center; gap: 8px; position: relative;
  background: none; border: 0; padding: 10px 0 6px; font-size: 13px; font-weight: 600; color: var(--muted);
}
.grip { position: absolute; top: 4px; left: 50%; width: 36px; height: 4px; margin-left: -18px; border-radius: 2px; background: var(--border); }
.spinner {
  width: 12px; height: 12px; border-radius: 50%; border: 2px solid var(--accent); border-right-color: transparent;
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .spinner { animation-duration: 2.4s; } }

.log {
  display: none; flex: 1 1 auto; overflow-y: auto; -webkit-overflow-scrolling: touch;
  padding: 4px 0 8px; min-height: 0; height: 30vh;
}
.ai[data-expanded="true"] .log { display: flex; flex-direction: column; gap: 6px; }

.msg { max-width: 92%; padding: 8px 11px; border-radius: 12px; white-space: pre-wrap; word-wrap: break-word; font-size: 14px; }
.msg.user { align-self: flex-end; background: var(--accent); color: var(--accent-text); border-bottom-right-radius: 4px; }
.msg.text { align-self: flex-start; background: var(--surface-2); border-bottom-left-radius: 4px; }
.msg.text.ask { border: 1px solid var(--warn); }
.msg.tool, .msg.tool_error {
  align-self: flex-start; background: none; padding: 0 4px; font: 12px/1.35 ui-monospace, Menlo, Consolas, monospace; color: var(--muted);
}
.msg.tool_error { color: var(--bad); }
.msg.system, .msg.done { align-self: center; background: none; padding: 0; font-size: 12px; color: var(--muted); }
.msg.done.fail, .msg.error { color: var(--bad); }
.msg.error { align-self: stretch; background: rgba(255, 107, 107, .08); border: 1px solid rgba(255, 107, 107, .3); font-size: 13px; }
.msg .meta { display: block; font-size: 11px; opacity: .7; margin-bottom: 2px; }
.empty { color: var(--muted); font-size: 13px; text-align: center; margin: auto 0; }

.confirm { background: rgba(245, 184, 75, .1); border: 1px solid var(--warn); border-radius: var(--radius); padding: 10px; margin-bottom: 8px; }
.confirm p { margin: 0 0 8px; font-size: 14px; }
.row { display: flex; gap: 8px; }
.row .btn { flex: 1; }

.composer { display: flex; gap: 8px; align-items: flex-end; }
.composer textarea {
  flex: 1; resize: none; max-height: 120px; min-height: 44px; padding: 11px 12px; font-size: 16px;
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 10px;
}
.composer textarea:focus, .card input:focus, .schedule input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

.ai-actions { display: flex; gap: 10px; justify-content: flex-start; padding-top: 4px; }
.ai-actions #btn-logout { margin-left: auto; }

.schedule { display: flex; flex-direction: column; gap: 8px; padding: 6px 0 4px; font-size: 14px; }
.schedule label { display: flex; align-items: center; gap: 8px; }
.schedule input[type="datetime-local"] {
  flex: 1; background: var(--surface-2); border: 1px solid var(--border); border-radius: 8px; padding: 8px; font-size: 16px; min-height: 40px;
}
.schedule .check { color: var(--muted); font-size: 13px; }
#jobs { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
#jobs li { display: flex; gap: 8px; align-items: center; font-size: 13px; background: var(--surface-2); border-radius: 8px; padding: 6px 8px; }
#jobs li span { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* Off-screen but focusable, so iOS shows the keyboard for remote typing. */
.kbd-sink {
  position: fixed; left: 0; bottom: 0; width: 1px; height: 1px; opacity: 0; font-size: 16px;
  border: 0; padding: 0; resize: none; pointer-events: none;
}

/* Small phones (iPhone SE 1st gen is 320px wide). */
@media (max-width: 340px) {
  .status-label { display: none; }
  .icon-btn { width: 36px; height: 36px; }
}
