/*
 * TokenArmy 运维 (ops.tokenarmy.shop): an operations tool, so the TokenArmy tokens (src/app/globals.css: colours,
 * radii, type) on calm, solid surfaces. Light and dark follow the system. No blur, no gradients, no motion beyond a
 * short colour transition; touch targets are at least 44 px; every text colour meets 4.5:1 on its surface.
 */
:root {
  color-scheme: light dark;
  --bg: light-dark(#eef1f8, #07080d);
  --panel: light-dark(#ffffff, #15171f);
  --panel-2: light-dark(#f6f7fb, #1c1f29);
  --fg: light-dark(#1d1d1f, #f5f5f7);
  --muted: light-dark(#55555c, #c5c5cc);
  --faint: light-dark(#636368, #a1a1a8);
  --accent: light-dark(#0062c4, #4da3ff);
  --accent-fill: light-dark(#0062c4, #0a63c9);
  --accent-soft: light-dark(rgba(0, 113, 227, 0.1), rgba(10, 132, 255, 0.16));
  --on-accent: #fff;
  --ok: light-dark(#13702e, #4cd964);
  --ok-soft: light-dark(rgba(52, 199, 89, 0.14), rgba(48, 209, 88, 0.14));
  --warn: light-dark(#8a3f00, #ffb340);
  --warn-soft: light-dark(rgba(255, 149, 0, 0.14), rgba(255, 159, 10, 0.14));
  --danger: light-dark(#b00018, #ff6961);
  --danger-fill: light-dark(#c4001a, #d70015);
  --danger-soft: light-dark(rgba(255, 59, 48, 0.1), rgba(255, 69, 58, 0.14));
  --line: light-dark(rgba(60, 60, 67, 0.16), rgba(255, 255, 255, 0.12));
  --line-strong: light-dark(rgba(60, 60, 67, 0.42), rgba(255, 255, 255, 0.34));
  --fill: light-dark(rgba(118, 118, 128, 0.1), rgba(120, 120, 128, 0.2));
  --code-bg: light-dark(#f2f3f7, #0d0e13);
  --r-lg: 16px;
  --r-md: 12px;
  --r-sm: 8px;
  --font-sans:
    -apple-system, BlinkMacSystemFont, "SF Pro Text", "PingFang SC", "Hiragino Sans GB", "Helvetica Neue", "Segoe UI",
    Roboto, "Microsoft YaHei", "Noto Sans SC", "Noto Sans CJK SC", sans-serif;
  --font-mono: ui-monospace, "SF Mono", SFMono-Regular, Menlo, Consolas, "Noto Sans Mono CJK SC", monospace;
}

@media (prefers-contrast: more) {
  :root {
    --muted: light-dark(#3a3a3f, #d1d1d6);
    --faint: light-dark(#48484d, #c7c7cc);
    --line: var(--line-strong);
  }
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  background: var(--bg);
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  min-height: 100vh;
  color: var(--fg);
  background: var(--bg);
  font-family: var(--font-sans);
  font-size: 15px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
}

h1,
h2,
h3 {
  line-height: 1.25;
  margin: 0;
  font-weight: 600;
}

p {
  margin: 0;
  text-wrap: pretty;
}

::placeholder {
  color: var(--faint);
  opacity: 1;
}

a {
  color: var(--accent);
  text-underline-offset: 0.2em;
}

::selection {
  background: var(--accent-soft);
}

input,
textarea {
  caret-color: var(--accent-fill);
}

html {
  scrollbar-color: var(--line-strong) transparent;
}

code,
pre,
.mono {
  font-family: var(--font-mono);
  font-size: 0.9em;
}

:focus-visible {
  outline: 3px solid color-mix(in srgb, var(--accent-fill) 65%, transparent);
  outline-offset: 2px;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

.muted {
  color: var(--muted);
}

.small {
  font-size: 13px;
}

.nowrap {
  white-space: nowrap;
}

.hint {
  color: var(--muted);
  font-size: 13px;
}

/* Buttons and fields */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 44px;
  padding: 0 18px;
  border: 1px solid var(--line-strong);
  border-radius: var(--r-md);
  background: var(--panel);
  color: var(--fg);
  font: inherit;
  font-weight: 500;
  cursor: pointer;
  transition:
    background-color 120ms ease,
    border-color 120ms ease;
}

.btn:hover {
  background: var(--fill);
}

.btn:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.btn.primary {
  background: var(--accent-fill);
  border-color: var(--accent-fill);
  color: var(--on-accent);
}

.btn.primary:hover {
  background: color-mix(in srgb, var(--accent-fill) 88%, black);
}

.btn.danger {
  background: var(--danger-fill);
  border-color: var(--danger-fill);
  color: var(--on-accent);
}

.btn.danger:hover {
  background: color-mix(in srgb, var(--danger-fill) 88%, black);
}

.btn.ghost {
  border-color: transparent;
  background: transparent;
}

.btn.ghost:hover {
  background: var(--fill);
}

.btn.small {
  min-height: 44px;
  padding: 0 12px;
  font-size: 14px;
}

.btn.block {
  width: 100%;
}

.field {
  display: grid;
  gap: 6px;
}

.field label,
.composer-label {
  font-weight: 600;
  font-size: 14px;
}

input[type="text"],
input[type="password"],
input[type="number"],
input[type="date"],
input:not([type]),
textarea {
  width: 100%;
  min-height: 44px;
  padding: 10px 12px;
  border: 1px solid var(--line-strong);
  border-radius: var(--r-sm);
  background: var(--panel);
  color: var(--fg);
  font: inherit;
}

textarea {
  resize: vertical;
  line-height: 1.5;
}

input:focus-visible,
textarea:focus-visible {
  outline: 3px solid color-mix(in srgb, var(--accent-fill) 45%, transparent);
  outline-offset: 0;
  border-color: var(--accent-fill);
}

input[type="checkbox"] {
  width: 22px;
  height: 22px;
  margin: 11px;
  accent-color: var(--accent-fill);
}

.stack {
  display: grid;
  gap: 16px;
}

.alert {
  padding: 10px 14px;
  border-radius: var(--r-sm);
  font-size: 14px;
}

.alert.error {
  background: var(--danger-soft);
  color: var(--danger);
}

.alert.info {
  background: var(--accent-soft);
  color: var(--fg);
}

.tag {
  display: inline-block;
  padding: 1px 8px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
  white-space: nowrap;
  background: var(--fill);
  color: var(--muted);
}

.tag.ok {
  background: var(--ok-soft);
  color: var(--ok);
}

.tag.warn {
  background: var(--warn-soft);
  color: var(--warn);
}

.tag.bad {
  background: var(--danger-soft);
  color: var(--danger);
}

details.plain > summary {
  cursor: pointer;
  color: var(--accent);
  min-height: 32px;
}

/* Sign-in */

body.auth {
  display: grid;
  place-items: center;
  padding: 24px 16px;
}

.auth-panel {
  width: min(420px, 100%);
  padding: 32px 28px;
  border: 1px solid var(--line);
  border-radius: var(--r-lg);
  background: var(--panel);
  display: grid;
  gap: 18px;
}

.auth-panel.wide {
  width: min(820px, 100%);
}

.auth-panel h1 {
  font-size: 22px;
}

.auth-panel h2 {
  font-size: 16px;
  margin-bottom: 8px;
}

.lede {
  color: var(--muted);
}

.setup-grid {
  display: grid;
  grid-template-columns: minmax(0, 260px) minmax(0, 1fr);
  gap: 32px;
}

.setup-grid section {
  display: grid;
  gap: 10px;
  align-content: start;
}

.qr {
  width: 220px;
  height: 220px;
  border-radius: var(--r-sm);
  background: #fff;
  border: 1px solid var(--line);
}

.secret-text {
  word-break: break-all;
  font-size: 15px;
  letter-spacing: 0.04em;
}

/* App shell */

.topbar {
  display: flex;
  align-items: center;
  gap: 20px;
  height: 56px;
  padding: 0 20px;
  border-bottom: 1px solid var(--line);
  background: var(--panel);
  position: sticky;
  top: 0;
  z-index: 2;
}

.brand {
  display: inline-flex;
  align-items: center;
  min-height: 44px;
  font-weight: 700;
  color: var(--fg);
  text-decoration: none;
}

.topnav {
  display: flex;
  gap: 4px;
}

.topnav a {
  display: inline-flex;
  align-items: center;
  min-height: 44px;
  padding: 0 12px;
  border-radius: var(--r-sm);
  color: var(--muted);
  text-decoration: none;
}

.topnav a[aria-current="page"] {
  color: var(--fg);
  background: var(--fill);
  font-weight: 600;
}

.who {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 8px;
}

.user-name {
  color: var(--muted);
}

.banner {
  padding: 10px 20px;
  background: var(--warn-soft);
  color: var(--warn);
  font-weight: 500;
  border-bottom: 1px solid var(--line);
}

.banner.bad {
  background: var(--danger-soft);
  color: var(--danger);
}

.layout {
  display: grid;
  grid-template-columns: 268px minmax(0, 1fr);
  height: calc(100vh - 56px);
}

.layout > * {
  min-width: 0;
}

.sidebar {
  border-right: 1px solid var(--line);
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  overflow-y: auto;
  background: var(--panel-2);
}

.sidebar-title {
  font-size: 13px;
  color: var(--muted);
  font-weight: 600;
  margin-top: 8px;
}

.conversation-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 2px;
}

.conversation-list button {
  width: 100%;
  min-height: 48px;
  padding: 6px 10px;
  border: 0;
  border-radius: var(--r-sm);
  background: transparent;
  color: var(--fg);
  font: inherit;
  text-align: left;
  cursor: pointer;
  display: grid;
  gap: 2px;
}

.conversation-list button:hover {
  background: var(--fill);
}

.conversation-list button[aria-current="true"] {
  background: var(--accent-soft);
}

.conversation-list .title {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 14px;
}

.conversation-list .meta {
  font-size: 12px;
  color: var(--muted);
}

.sidebar-note {
  margin-top: auto;
}

/* Chat */

.chat {
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr) auto;
  min-height: 0;
}

.chat > * {
  min-width: 0;
}

/* Rows by name: the keys strip between head and messages is often hidden. */
.chat-head {
  grid-row: 1;
}

.pending-keys {
  grid-row: 2;
}

.messages {
  grid-row: 3;
}

.composer {
  grid-row: 4;
}

.chat-head {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 12px 24px;
  border-bottom: 1px solid var(--line);
  min-height: 64px;
}

.chat-head h1 {
  font-size: 20px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
}

.pane-state {
  color: var(--muted);
  font-size: 14px;
  white-space: nowrap;
}

.pane-state.working {
  color: var(--accent);
  font-weight: 500;
}

.chat-head .btn {
  margin-left: auto;
}

.messages {
  list-style: none;
  margin: 0;
  padding: 20px 24px 32px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.messages > li {
  width: min(820px, 100%);
  margin: 0 auto;
}

.empty {
  padding: 40px 0;
  color: var(--muted);
}

.empty h2 {
  font-size: 16px;
  color: var(--fg);
  margin-bottom: 10px;
}

.examples {
  margin: 0;
  padding-left: 20px;
  display: grid;
  gap: 6px;
}

.who-label {
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
  margin-bottom: 4px;
}

.msg.operator {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
}

.msg.operator .bubble {
  max-width: 80%;
  padding: 10px 14px;
  border-radius: var(--r-md);
  background: var(--accent-soft);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.msg.web .note,
.notice {
  font-size: 14px;
  color: var(--muted);
  background: var(--fill);
  border-radius: var(--r-sm);
  padding: 8px 12px;
  overflow-wrap: anywhere;
}

.msg.assistant .md {
  overflow-wrap: anywhere;
}

.md > * + * {
  margin-top: 10px;
}

.md ul,
.md ol {
  margin: 0;
  padding-left: 22px;
}

.md pre {
  margin: 0;
  padding: 12px 14px;
  border-radius: var(--r-sm);
  background: var(--code-bg);
  overflow-x: auto;
  font-size: 13px;
  line-height: 1.5;
}

.md code {
  padding: 1px 5px;
  border-radius: 5px;
  background: var(--fill);
}

.md pre code {
  padding: 0;
  background: none;
}

.md table {
  border-collapse: collapse;
  font-size: 14px;
  display: block;
  max-width: 100%;
  overflow-x: auto;
  overflow-wrap: normal;
}

.md th,
.md td {
  padding: 6px 10px;
  border-bottom: 1px solid var(--line);
  text-align: left;
  vertical-align: top;
}

.md th {
  font-weight: 600;
}

.md .md-heading {
  font-weight: 600;
}

/* Tool calls: one line each, details folded */

.tool details {
  border: 1px solid var(--line);
  border-radius: var(--r-sm);
  background: var(--panel);
}

.tool details > summary {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 44px;
  padding: 6px 12px;
  cursor: pointer;
  list-style: none;
}

.tool details > summary::-webkit-details-marker {
  display: none;
}

.tool details > summary::before {
  content: "";
  flex: none;
  width: 7px;
  height: 7px;
  margin: 0 4px 0 2px;
  border-right: 2px solid var(--faint);
  border-bottom: 2px solid var(--faint);
  transform: rotate(-45deg);
}

.tool details[open] > summary::before {
  transform: rotate(45deg);
}

.tool-name {
  font-family: var(--font-mono);
  font-size: 13px;
  font-weight: 600;
}

.tool-args {
  color: var(--muted);
  font-size: 13px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
  flex: 1;
}

.tool-body {
  padding: 4px 12px 12px;
  display: grid;
  gap: 6px;
}

.tool-body .label {
  font-size: 12px;
  color: var(--muted);
  font-weight: 600;
}

pre.json {
  margin: 0;
  padding: 10px 12px;
  border-radius: var(--r-sm);
  background: var(--code-bg);
  font-size: 12.5px;
  line-height: 1.5;
  max-height: 360px;
  overflow: auto;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

/* Cards (confirmation, authorization link, VPS form): one level, never nested */

.card {
  border: 1px solid var(--line-strong);
  border-radius: var(--r-md);
  padding: 16px 18px;
  background: var(--panel);
  display: grid;
  gap: 12px;
}

.card.irreversible {
  border-color: var(--danger);
}

.card h2 {
  font-size: 16px;
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.card .facts {
  margin: 0;
  display: grid;
  grid-template-columns: max-content minmax(0, 1fr);
  gap: 6px 16px;
  font-size: 14px;
}

.card .facts dt {
  color: var(--muted);
}

.card .facts dd {
  margin: 0;
  overflow-wrap: anywhere;
}

.card-actions {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  align-items: end;
}

.card-actions .field {
  flex: 1 1 220px;
}

.card .status {
  font-weight: 600;
}

.card .status.ok {
  color: var(--ok);
}

.card .status.bad {
  color: var(--danger);
}

.login-grid {
  display: grid;
  grid-template-columns: 220px minmax(0, 1fr);
  gap: 18px;
  align-items: start;
}

.login-grid .qr {
  width: 220px;
  height: 220px;
}

.login-link {
  word-break: break-all;
  font-size: 13px;
}

/* A risk the operator must read before confirming (TokenArmy unknown, an uncounted egress) */
.alert.warn {
  background: var(--warn-soft);
  color: var(--warn);
  font-weight: 500;
}

/* A new key, shown once */

.key-value {
  font-family: var(--font-mono);
  font-size: 13px;
  width: 100%;
}

.key-row {
  display: flex;
  gap: 10px;
  align-items: center;
}

.key-row .key-value {
  flex: 1 1 auto;
  min-width: 0;
}

.key-row > span {
  flex: 1 1 auto;
}

.key-row .btn {
  flex: none;
  white-space: nowrap;
}

.pending-keys {
  padding: 12px 24px;
  border-bottom: 1px solid var(--line);
  display: grid;
  gap: 10px;
  background: var(--panel-2);
}

.pending-keys > * {
  width: min(820px, 100%);
  margin: 0 auto;
}

.pending-keys ul {
  list-style: none;
  margin: 0 auto;
  padding: 0;
  display: grid;
  gap: 10px;
}

.steps {
  margin: 0;
  padding-left: 20px;
  display: grid;
  gap: 4px;
  font-size: 14px;
}

/* Composer */

.composer {
  border-top: 1px solid var(--line);
  padding: 14px 24px 18px;
  display: grid;
  gap: 8px;
  background: var(--panel);
}

.composer > * {
  width: min(820px, 100%);
  margin: 0 auto;
}

.composer-row {
  display: flex;
  gap: 12px;
  align-items: center;
}

.composer-row .hint {
  flex: 1;
}

/* Admin */

.admin {
  padding: 24px;
  display: grid;
  gap: 16px;
  max-width: 1280px;
  margin: 0 auto;
}

.admin-head {
  display: flex;
  align-items: baseline;
  gap: 16px;
  flex-wrap: wrap;
}

.admin-head h1 {
  font-size: 22px;
}

.chain {
  font-size: 14px;
  font-weight: 500;
}

.chain.ok {
  color: var(--ok);
}

.chain.bad {
  color: var(--danger);
}

.tabs {
  display: flex;
  gap: 4px;
  border-bottom: 1px solid var(--line);
  overflow-x: auto;
}

.tabs a {
  display: inline-flex;
  align-items: center;
  min-height: 44px;
  padding: 0 14px;
  color: var(--muted);
  text-decoration: none;
  border-bottom: 2px solid transparent;
  white-space: nowrap;
}

.tabs a[aria-current="page"] {
  color: var(--fg);
  border-bottom-color: var(--accent-fill);
  font-weight: 600;
}

.admin-body {
  display: grid;
  gap: 14px;
}

.admin-body h2 {
  font-size: 16px;
  margin-top: 8px;
}

.filters {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  align-items: end;
}

.filters .field {
  width: 180px;
}

.count {
  color: var(--muted);
  font-size: 14px;
}

.table-wrap {
  overflow-x: auto;
  border: 1px solid var(--line);
  border-radius: var(--r-md);
  background: var(--panel);
}

.table-wrap table {
  border-collapse: collapse;
  width: 100%;
  font-size: 13.5px;
  font-variant-numeric: tabular-nums;
}

.table-wrap th,
.table-wrap td {
  padding: 8px 12px;
  border-bottom: 1px solid var(--line);
  text-align: left;
  vertical-align: top;
}

.table-wrap th {
  font-weight: 600;
  color: var(--muted);
  white-space: nowrap;
  background: var(--panel-2);
}

.table-wrap tbody tr:last-child td {
  border-bottom: 0;
}

.table-wrap td.num {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

code.inline {
  font-size: 12.5px;
  overflow-wrap: anywhere;
}

.level.irreversible {
  color: var(--danger);
  font-weight: 600;
}

.level.write {
  color: var(--warn);
  font-weight: 600;
}

.switches {
  display: grid;
  gap: 10px;
  padding: 16px;
  border: 1px solid var(--line);
  border-radius: var(--r-md);
  background: var(--panel);
}

.inline-form {
  display: inline;
}

.meter {
  display: grid;
  gap: 14px;
  max-width: 640px;
}

.meter-figure {
  font-size: 18px;
}

meter {
  width: 100%;
  height: 14px;
}

.meter .facts {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: 12px;
  margin: 0;
}

.meter .facts dt {
  color: var(--muted);
  font-size: 13px;
}

.meter .facts dd {
  margin: 0;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

/* The hidden attribute wins over any display rule above (the interrupt button, alerts, the banner). */
.btn[hidden],
[hidden] {
  display: none;
}

@media (max-width: 860px) {
  .layout {
    grid-template-columns: 1fr;
    height: auto;
  }

  .sidebar {
    border-right: 0;
    border-bottom: 1px solid var(--line);
    max-height: 32vh;
  }

  .sidebar-note {
    display: none;
  }

  .chat {
    min-height: calc(100vh - 56px);
  }

  .setup-grid,
  .login-grid {
    grid-template-columns: 1fr;
  }

  .messages,
  .composer,
  .chat-head,
  .pending-keys {
    padding-left: 14px;
    padding-right: 14px;
  }

  .key-row {
    flex-wrap: wrap;
  }

  .composer-row {
    flex-direction: column;
    align-items: stretch;
  }

  .chat-head {
    flex-wrap: wrap;
  }

  .pane-state {
    white-space: normal;
  }
}

@media (prefers-reduced-motion: reduce) {
  .btn {
    transition: none;
  }
}
