:root {
  --bg: #f4f4f2;
  --surface: #ffffff;
  --text: #1c1c1a;
  --muted: #6b6b66;
  --line: #deded9;
  --accent: #1f5fbf;
  --on-accent: #ffffff;
  --ok: #1e7a3c;
  --warn: #9a6200;
  --bad: #b3261e;
  --radius: 6px;
  --tabbar: 0px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #161615;
    --surface: #20201f;
    --text: #ecebe6;
    --muted: #a3a29b;
    --line: #363634;
    --accent: #6b9ee8;
    --on-accent: #10213a;
    --ok: #5cbf7a;
    --warn: #e0a94a;
    --bad: #ef7a72;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.55 -apple-system, BlinkMacSystemFont, "PingFang SC", "Microsoft YaHei", sans-serif;
  -webkit-tap-highlight-color: transparent;
}

h1, h2, h3 { margin: 0 0 12px; line-height: 1.3; }
h1 { font-size: 22px; }
h2 { font-size: 17px; }
h3 { font-size: 15px; }
p { margin: 0 0 8px; }
a { color: var(--accent); }
code { font-size: 13px; word-break: break-all; }

input, select, button { font: inherit; color: inherit; }

/* 输入框字号不小于 16px，避免 iOS 聚焦时自动放大页面 */
input, select {
  width: 100%;
  min-height: 40px;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--bg);
  font-size: 16px;
}

input:focus, select:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

label { display: block; margin-bottom: 12px; color: var(--muted); font-size: 13px; }
label input, label select { margin-top: 4px; color: var(--text); }
label.check { display: flex; gap: 8px; align-items: center; color: var(--text); font-size: 15px; min-height: 32px; cursor: pointer; }
label.check input { width: 18px; height: 18px; min-height: 0; margin: 0; flex: none; }
.hint { color: var(--muted); font-size: 13px; }
.field-hint { margin: -8px 0 12px; }
.check-hint { margin-left: 26px; }
.clear-check { margin-top: -6px; font-size: 14px; }

button {
  min-height: 36px;
  padding: 6px 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  cursor: pointer;
  touch-action: manipulation;
}
button:disabled { opacity: .5; cursor: default; }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
button.danger { color: var(--bad); }
button.link { border: 0; background: none; color: var(--accent); padding: 0; min-height: 0; }

@media (hover: hover) {
  button:not(:disabled):hover { border-color: var(--muted); }
  button.primary:not(:disabled):hover { filter: brightness(1.08); }
}

/* ---------------------------------------------------------------- 顶栏 */

.topbar {
  display: flex;
  align-items: center;
  gap: 24px;
  /* 与内容区（max-width 1280px）左右对齐 */
  padding: 0 max(24px, calc((100% - 1280px) / 2 + 24px));
  height: 52px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 5;
}
.brand { font-size: 16px; white-space: nowrap; }
.tabs { display: flex; gap: 4px; flex: 1; height: 100%; }
.tabs a {
  display: flex;
  align-items: center;
  padding: 0 12px;
  color: var(--muted);
  text-decoration: none;
  white-space: nowrap;
  border-bottom: 2px solid transparent;
}
.tabs a.active { color: var(--text); font-weight: 600; border-bottom-color: var(--accent); }
@media (hover: hover) { .tabs a:hover { color: var(--text); } }

main {
  max-width: 1280px;
  margin: 0 auto;
  padding: 24px;
}

/* ---------------------------------------------------------------- 布局 */

.grid { display: grid; gap: 16px; align-items: start; }
.grid.overview { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid.two { grid-template-columns: repeat(2, minmax(0, 1fr)); max-width: 960px; }

.columns { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; align-items: start; }
.column { display: flex; flex-direction: column; gap: 16px; min-width: 0; }

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 20px;
  min-width: 0;
}

.card-head { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; margin-bottom: 10px; }
.card-head h2 { margin: 0; }

.status { font-weight: 600; font-size: 14px; white-space: nowrap; }
.status.online { color: var(--ok); }
.status.recovering, .status.need_confirm, .status.unknown { color: var(--warn); }
.status.need_scan, .status.unreachable, .status.error { color: var(--bad); }
.status.disabled, .status.unconfigured { color: var(--muted); }

.summary { display: none; }

dl.meta { display: grid; grid-template-columns: auto 1fr; gap: 4px 12px; margin: 0 0 12px; font-size: 14px; }
dl.meta dt { color: var(--muted); white-space: nowrap; }
dl.meta dd { margin: 0; word-break: break-all; }

.detail { margin-bottom: 12px; }

.actions { display: flex; flex-wrap: wrap; gap: 8px; }

.qr { text-align: center; margin: 4px 0 14px; padding: 16px 12px; border: 1px solid var(--line); border-radius: var(--radius); }
.qr svg, .qr img { width: 220px; max-width: 100%; height: auto; display: block; margin: 0 auto 10px; background: #fff; }
.qr svg { shape-rendering: crispEdges; }
.qr .link-row { display: flex; gap: 8px; align-items: center; justify-content: center; flex-wrap: wrap; }
.qr .link-row input { width: 180px; }
.qr-link { display: block; max-width: 100%; }

.form-actions { display: flex; gap: 8px; align-items: center; margin-top: 4px; }

.checks { list-style: none; padding: 0; margin: 0 0 14px; font-size: 14px; }
.checks li { padding: 2px 0; }
.checks .yes { color: var(--ok); }
.checks .no { color: var(--muted); }

/* ---------------------------------------------------------------- 日志 */

ol.events { list-style: none; margin: 0; padding: 0; font-size: 14px; }
ol.events li {
  display: grid;
  grid-template-columns: 11em 3em 1fr;
  gap: 12px;
  padding: 8px 0;
  border-bottom: 1px solid var(--line);
}
ol.events li:last-child { border-bottom: 0; }
ol.events time { color: var(--muted); font-variant-numeric: tabular-nums; }
ol.events .source { color: var(--muted); }
ol.events .message { word-break: break-word; }
ol.events .warn { color: var(--warn); }
ol.events .error { color: var(--bad); }

.log-controls { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-top: 4px; }
.log-controls select { width: auto; min-height: 36px; padding: 4px 8px; font-size: 14px; }
.logbox {
  margin: 12px 0 0;
  padding: 12px;
  max-height: 60vh;
  overflow: auto;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  font: 12px/1.5 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  white-space: pre;
  -webkit-overflow-scrolling: touch;
}

/* ---------------------------------------------------------------- 登录 */

.login-box {
  width: min(360px, calc(100% - 32px));
  margin: 12vh auto 0;
  padding: 28px 24px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}
.login-box button { width: 100%; min-height: 44px; margin-top: 4px; }
.error { color: var(--bad); font-size: 14px; min-height: 1.5em; margin-top: 8px; }

#toast {
  position: fixed;
  left: 50%;
  bottom: calc(24px + var(--tabbar) + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  width: max-content;
  max-width: calc(100% - 32px);
  padding: 10px 16px;
  border-radius: var(--radius);
  background: var(--text);
  color: var(--bg);
  font-size: 14px;
  z-index: 10;
}

/* ---------------------------------------------------------------- 平板 / 窄屏 PC */

@media (max-width: 1179px) {
  .grid.overview { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .grid.overview .card-notify { grid-column: 1 / -1; }
}

@media (max-width: 899px) {
  main { padding: 16px; }
  .columns, .grid.two { grid-template-columns: minmax(0, 1fr); }
}

/* ---------------------------------------------------------------- 手机 */

@media (max-width: 719px) {
  :root { --tabbar: 56px; }

  body { font-size: 15px; }

  .topbar {
    height: auto;
    min-height: 48px;
    padding: env(safe-area-inset-top) 16px 0;
    justify-content: space-between;
  }

  /* 导航移到底部，方便单手操作 */
  .tabs {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    height: calc(var(--tabbar) + env(safe-area-inset-bottom));
    padding-bottom: env(safe-area-inset-bottom);
    gap: 0;
    background: var(--surface);
    border-top: 1px solid var(--line);
    z-index: 5;
  }
  .tabs a { flex: 1; justify-content: center; border-bottom: 0; border-top: 2px solid transparent; font-size: 15px; }
  .tabs a.active { border-top-color: var(--accent); }

  main { padding: 12px 12px calc(var(--tabbar) + env(safe-area-inset-bottom) + 16px); }

  .grid.overview { grid-template-columns: minmax(0, 1fr); }
  .grid, .columns, .column { gap: 12px; }
  .card { padding: 16px; }
  .card[id] { scroll-margin-top: 60px; }

  .summary {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 8px;
    margin-bottom: 12px;
  }
  .summary-item {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    gap: 2px;
    padding: 10px 12px;
    min-height: 56px;
    text-align: left;
    color: var(--muted);
    font-size: 13px;
  }
  .summary-item .status { font-size: 15px; }

  /* 操作按钮两列铺满，加大点击区域 */
  .actions { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .actions button, .form-actions button { min-height: 44px; }
  .form-actions button { flex: 1; }

  .qr svg, .qr img { width: min(240px, 100%); }
  .qr .link-row { flex-direction: column; align-items: stretch; }
  .qr .link-row input { width: 100%; }

  .log-controls { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .log-controls select { width: 100%; min-height: 44px; font-size: 16px; }
  .log-controls button { min-height: 44px; }
  .logbox { max-height: 55vh; }

  ol.events li { grid-template-columns: auto 1fr; gap: 2px 10px; }
  ol.events .message { grid-column: 1 / -1; }

  .login-box { margin-top: 8vh; }
}
