:root {
  color-scheme: light dark;
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 16px;
  line-height: 1.45;
  color: #172033;
  background: #f4f6fa;
  --bg: #f4f6fa;
  --surface: #ffffff;
  --surface-muted: #f8faff;
  --border: #dce2ec;
  --border-strong: #c7d0df;
  --text: #172033;
  --muted: #667085;
  --primary: #335dce;
  --primary-hover: #274cae;
  --primary-contrast: #ffffff;
  --green: #1c9b62;
  --red: #cf4750;
  --grey-dot: #98a2b3;
  --shadow: 0 12px 35px rgb(31 43 71 / 8%);
}

* { box-sizing: border-box; }

/* The UI toggles visibility through the [hidden] attribute (setView, panels, form fields). Author
   rules below set an explicit `display` on many of those same elements (.view, .field, .status-grid,
   .security-password-form, ...), and an author `display` overrides the user-agent [hidden]{display:none}
   -- so without this guard `element.hidden = true` never actually hides anything (e.g. the loading
   view keeps filling the screen after the session loads). Make the attribute authoritative. */
[hidden] { display: none !important; }

body {
  min-width: 320px;
  min-height: 100vh;
  margin: 0;
  background: var(--bg);
  color: var(--text);
}

button, input { font: inherit; }
button { cursor: pointer; }
button:disabled { cursor: not-allowed; opacity: .55; }

.app-shell { min-height: 100vh; }
.view { width: 100%; }
.loading-view { min-height: 100vh; display: grid; place-content: center; justify-items: center; gap: 12px; color: var(--muted); }
.loading-view p { margin: 0; }

.login-view { min-height: 100vh; display: grid; place-items: center; padding: 24px; }
.login-card { width: min(100%, 440px); padding: 34px; }
.brand-mark { display: grid; place-items: center; width: 42px; height: 42px; border-radius: 12px; background: var(--primary); color: var(--primary-contrast); font-weight: 800; letter-spacing: .04em; }

.card { background: var(--surface); border: 1px solid var(--border); border-radius: 16px; box-shadow: var(--shadow); }
.eyebrow { margin: 0 0 5px; color: var(--primary); font-size: .72rem; font-weight: 750; letter-spacing: .1em; text-transform: uppercase; }
h1, h2, p { margin-top: 0; }
h1 { margin-bottom: 8px; font-size: clamp(1.45rem, 3vw, 2rem); letter-spacing: -.025em; }
h2 { margin-bottom: 0; font-size: 1.18rem; letter-spacing: -.015em; }
.muted { color: var(--muted); }

.login-card > .muted { margin-bottom: 26px; }
.mode-toggle { display: grid; grid-template-columns: 1fr 1fr; gap: 4px; padding: 4px; margin-bottom: 22px; border: 1px solid var(--border); border-radius: 10px; background: var(--surface-muted); }
.mode-button { border: 0; border-radius: 7px; padding: 8px 10px; color: var(--muted); background: transparent; }
.mode-button.active { background: var(--surface); color: var(--text); box-shadow: 0 1px 4px rgb(31 43 71 / 10%); font-weight: 700; }
.field { display: grid; gap: 6px; margin-bottom: 16px; font-weight: 650; font-size: .9rem; }
.field input, .command-arg { width: 100%; border: 1px solid var(--border-strong); border-radius: 9px; padding: 10px 11px; background: var(--surface); color: var(--text); outline: none; }
.field input:focus, .command-arg:focus, .logs-panel:focus { border-color: var(--primary); box-shadow: 0 0 0 3px rgb(51 93 206 / 15%); }
.field small { color: var(--muted); font-size: .76rem; font-weight: 450; }

.primary-button, .secondary-button, .danger-button { border-radius: 9px; padding: 9px 13px; border: 1px solid transparent; font-weight: 700; transition: background .15s ease, border-color .15s ease, color .15s ease; }
.primary-button { background: var(--primary); color: var(--primary-contrast); }
.primary-button:hover:not(:disabled) { background: var(--primary-hover); }
.secondary-button { border-color: var(--border-strong); background: var(--surface); color: var(--text); }
.secondary-button:hover:not(:disabled) { border-color: var(--primary); color: var(--primary); }
.danger-button { border-color: rgb(207 71 80 / 35%); background: transparent; color: var(--red); }
.danger-button:hover:not(:disabled) { background: rgb(207 71 80 / 8%); }
.text-button, .link-button { display: block; border: 0; padding: 0; background: transparent; color: var(--primary); font-weight: 650; text-align: left; }
.text-button { margin: 17px 0 0; font-size: .88rem; }
.link-button { margin-top: 24px; font-size: .86rem; }
.text-button:hover, .link-button:hover { color: var(--primary-hover); text-decoration: underline; }
.full-width { width: 100%; }
.form-message { margin: 12px 0 0; padding: 9px 11px; border-radius: 8px; font-size: .86rem; }
.error-message { color: var(--red); background: rgb(207 71 80 / 9%); }
.success-message { color: var(--green); background: rgb(28 155 98 / 10%); }
.info-message { color: var(--primary); background: rgb(51 93 206 / 9%); }

.dashboard-view { max-width: 1440px; margin: 0 auto; }
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 20px; padding: 28px clamp(20px, 4vw, 54px) 24px; border-bottom: 1px solid var(--border); }
.topbar h1 { margin: 0; }
.account-actions { display: flex; align-items: center; gap: 13px; }
.user-email { color: var(--muted); font-size: .9rem; overflow-wrap: anywhere; }
.dashboard-content { display: grid; gap: 22px; padding: 26px clamp(20px, 4vw, 54px) 50px; }
.devices-card, .status-card, .controls-card, .logs-card, .security-card, .audit-card { padding: 22px; }
.account-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 18px; }
.security-status { color: var(--muted); font-size: .82rem; font-weight: 750; white-space: nowrap; }
.security-status.on { color: var(--green); }
.security-status.off { color: var(--muted); }
.security-card > .muted { margin: 18px 0; font-size: .9rem; }
.security-password-form, .setup-verify-form { display: flex; align-items: end; flex-wrap: wrap; gap: 9px; margin-top: 14px; }
.inline-security-field { display: grid; flex: 1 1 180px; gap: 5px; color: var(--muted); font-size: .78rem; font-weight: 650; }
.inline-security-field input { width: 100%; min-width: 0; border: 1px solid var(--border-strong); border-radius: 9px; padding: 9px 10px; background: var(--surface); color: var(--text); outline: none; }
.inline-security-field input:focus { border-color: var(--primary); box-shadow: 0 0 0 3px rgb(51 93 206 / 15%); }
.two-factor-setup { margin-top: 18px; padding-top: 18px; border-top: 1px solid var(--border); }
.two-factor-setup h3 { margin: 0 0 7px; font-size: .98rem; }
.two-factor-setup > .muted { margin-bottom: 15px; font-size: .86rem; }
.manual-account { display: grid; gap: 3px; margin-bottom: 9px; padding: 9px 10px; border: 1px solid var(--border); border-radius: 9px; background: var(--surface-muted); }
.manual-account code { overflow-wrap: anywhere; color: var(--text); font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: .78rem; user-select: all; }
.copy-field { display: flex; align-items: center; gap: 9px; margin-top: 9px; padding: 9px 10px; border: 1px solid var(--border); border-radius: 9px; background: var(--surface-muted); }
.copy-field > div { display: grid; flex: 1; gap: 3px; min-width: 0; }
.copy-field-label { color: var(--muted); font-size: .72rem; font-weight: 700; }
.copy-field code { overflow-wrap: anywhere; color: var(--text); font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: .78rem; user-select: all; }
.backup-code-help { margin: 17px 0 8px; color: var(--muted); font-size: .82rem; }
.backup-code-list { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 5px 18px; margin: 0; padding: 10px 12px 10px 30px; border: 1px solid var(--border); border-radius: 9px; background: var(--surface-muted); font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: .84rem; }
.audit-card { min-width: 0; }
.audit-list { display: grid; gap: 8px; max-height: 330px; overflow: auto; margin-top: 16px; padding-right: 2px; }
.audit-entry { padding: 11px 12px; border: 1px solid var(--border); border-radius: 9px; background: var(--surface-muted); }
.audit-entry-heading { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; }
.audit-entry-heading strong { overflow-wrap: anywhere; }
.audit-entry-heading time { flex: 0 0 auto; color: var(--muted); font-size: .74rem; white-space: nowrap; }
.audit-detail { margin: 5px 0 0; overflow-wrap: anywhere; font-size: .84rem; }
.audit-metadata { margin: 6px 0 0; color: var(--muted); overflow-wrap: anywhere; font-size: .74rem; }
.section-heading, .selected-device-heading, .pair-heading { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; }
.compact-heading { align-items: center; }
.add-device-controls { display: flex; align-items: center; gap: 8px; }
.inline-field input { width: 190px; border: 1px solid var(--border-strong); border-radius: 9px; padding: 9px 10px; background: var(--surface); color: var(--text); outline: none; }
.inline-field input:focus { border-color: var(--primary); box-shadow: 0 0 0 3px rgb(51 93 206 / 15%); }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0; }

.pair-panel { margin-top: 20px; padding: 18px; border: 1px solid rgb(51 93 206 / 30%); border-radius: 12px; background: rgb(51 93 206 / 6%); }
.pair-code { margin: 2px 0 0; font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: clamp(2rem, 7vw, 3.25rem); font-weight: 800; letter-spacing: .14em; line-height: 1.15; }
.pair-expiry { margin: 6px 0 0; font-size: .84rem; }
.pair-instructions { margin: 18px 0 7px; font-size: .9rem; }
.pair-command { display: block; overflow-x: auto; padding: 10px 12px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface); color: var(--text); font-size: .82rem; white-space: nowrap; }

.device-list { display: grid; gap: 8px; margin-top: 20px; }
.device-row { display: grid; grid-template-columns: minmax(145px, 1fr) minmax(240px, 1.4fr) auto; align-items: center; gap: 16px; width: 100%; padding: 13px 14px; border: 1px solid var(--border); border-radius: 10px; background: var(--surface); color: var(--text); text-align: left; }
.device-row:hover, .device-row.selected { border-color: var(--primary); background: var(--surface-muted); }
.device-row.revoked { opacity: .55; filter: grayscale(.35); }
.device-identity { display: flex; align-items: center; flex-wrap: wrap; gap: 8px; min-width: 0; }
.device-identity strong { overflow-wrap: anywhere; }
.device-signals { display: flex; flex-wrap: wrap; gap: 12px; }
.status-label { display: inline-flex; align-items: center; gap: 7px; color: var(--muted); font-size: .82rem; white-space: nowrap; }
.status-dot { display: inline-block; width: 9px; height: 9px; flex: 0 0 9px; border-radius: 50%; background: var(--grey-dot); }
.status-dot.online { background: var(--green); }
.status-dot.offline { background: var(--grey-dot); }
.status-dot.running { box-shadow: 0 0 0 4px rgb(28 155 98 / 15%); }
.status-dot.paused { background: #d49b2c; box-shadow: 0 0 0 4px rgb(212 155 44 / 15%); }
.last-seen { color: var(--muted); font-size: .82rem; white-space: nowrap; }
.tag { display: inline-block; padding: 2px 7px; border-radius: 999px; font-size: .68rem; font-weight: 750; letter-spacing: .04em; text-transform: uppercase; }
.revoked-tag { background: rgb(152 162 179 / 20%); color: var(--muted); }
.empty-state { margin: 20px 0 0; color: var(--muted); font-size: .9rem; }

.device-panel { display: grid; gap: 18px; }
.selected-device-heading { padding: 0 2px; align-items: center; }
.selected-device-heading h2 { font-size: 1.35rem; }
.panel-grid { display: grid; grid-template-columns: minmax(0, 1.25fr) minmax(300px, .75fr); gap: 18px; }
.run-state-row { display: flex; align-items: center; gap: 9px; margin: 23px 0 19px; }
.run-state-row strong { font-size: 1.35rem; text-transform: capitalize; }
.run-state-running { color: var(--green); }
.run-state-paused { color: #b27c15; }
.run-state-stopped, .run-state-none { color: var(--muted); }
.status-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }
.stat { min-width: 0; padding: 10px; border: 1px solid var(--border); border-radius: 9px; background: var(--surface-muted); }
.stat span, .stat strong { display: block; overflow-wrap: anywhere; }
.stat span { color: var(--muted); font-size: .72rem; }
.stat strong { margin-top: 2px; font-size: .9rem; }
.controls-list { display: grid; gap: 9px; margin-top: 18px; }
.control-row { display: grid; grid-template-columns: minmax(0, 1fr) auto; align-items: center; gap: 12px; padding-bottom: 9px; border-bottom: 1px solid var(--border); }
.control-row:last-of-type { border-bottom: 0; padding-bottom: 0; }
.control-description { display: grid; gap: 2px; min-width: 0; }
.control-description strong { overflow-wrap: anywhere; }
.control-description small { color: var(--muted); font-size: .75rem; }
.control-action { display: flex; align-items: center; gap: 7px; min-width: 0; }
.command-arg { width: min(150px, 35vw); padding: 8px 9px; font-size: .85rem; }
.command-button { white-space: nowrap; }
.command-notice { margin: 4px 0 0; padding: 8px 10px; border-radius: 7px; font-size: .8rem; }
.command-notice.success { color: var(--green); background: rgb(28 155 98 / 10%); }
.command-notice.error { color: var(--red); background: rgb(207 71 80 / 9%); }
.command-notice.info { color: var(--primary); background: rgb(51 93 206 / 9%); }

.logs-card { min-width: 0; }
.logs-panel { height: 270px; overflow: auto; margin-top: 16px; padding: 12px; border: 1px solid var(--border); border-radius: 9px; background: #101725; color: #dce7f7; font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: .78rem; line-height: 1.55; outline: none; }
.log-line { display: grid; grid-template-columns: 86px minmax(0, 1fr); gap: 10px; white-space: pre-wrap; overflow-wrap: anywhere; }
.log-time { color: #8fa2bd; user-select: none; }
.log-text { color: #e9eff8; }
.spinner { width: 24px; height: 24px; border: 3px solid var(--border); border-top-color: var(--primary); border-radius: 50%; animation: spin .8s linear infinite; }
.clipboard-helper { position: fixed; left: -10000px; top: -10000px; }

@keyframes spin { to { transform: rotate(360deg); } }

@media (prefers-color-scheme: dark) {
  :root {
    color: #edf2fb;
    background: #101725;
    --bg: #101725;
    --surface: #182235;
    --surface-muted: #1d2a40;
    --border: #2d3b54;
    --border-strong: #40516d;
    --text: #edf2fb;
    --muted: #a6b2c5;
    --primary: #86a8ff;
    --primary-hover: #a1baff;
    --primary-contrast: #101725;
    --green: #56d294;
    --red: #ff8b91;
    --grey-dot: #718099;
    --shadow: 0 12px 35px rgb(0 0 0 / 22%);
  }
}

@media (max-width: 780px) {
  .topbar { align-items: flex-start; flex-direction: column; }
  .account-actions { width: 100%; justify-content: space-between; }
  .account-grid { grid-template-columns: 1fr; }
  .panel-grid { grid-template-columns: 1fr; }
  .device-row { grid-template-columns: 1fr auto; }
  .device-signals { grid-column: 1 / -1; grid-row: 2; }
  .last-seen { grid-column: 2; grid-row: 1; }
}

@media (max-width: 520px) {
  .login-card, .devices-card, .status-card, .controls-card, .logs-card, .security-card, .audit-card { padding: 17px; }
  .dashboard-content { padding-inline: 12px; }
  .topbar { padding-inline: 12px; }
  .section-heading, .pair-heading, .selected-device-heading { flex-direction: column; }
  .section-heading > button, .selected-device-heading > button { width: 100%; }
  .add-device-controls { width: 100%; }
  .add-device-controls .inline-field { flex: 1; }
  .add-device-controls .inline-field input { width: 100%; }
  .pair-heading { flex-direction: row; align-items: center; }
  .status-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .control-row { grid-template-columns: 1fr; gap: 8px; }
  .control-action { justify-content: stretch; }
  .control-action .command-arg { flex: 1; width: auto; }
  .control-action .command-button { flex: 0 0 auto; }
  .copy-field { align-items: stretch; flex-direction: column; }
  .copy-field .secondary-button { align-self: flex-start; }
  .backup-code-list { grid-template-columns: 1fr; }
}
