:root {
  --bg: #f6f6f6;
  --surface: #ffffff;
  --ink: #111111;
  --muted: #6b6b6b;
  --line: #e5e5e5;
  --ok: #1f7a4d;
  --ok-bg: #eaf6ef;
  --danger: #b42318;
  --danger-bg: #fcebea;
  --control-h: 40px;
  --radius: 4px;
  --font: "Manrope", "Noto Sans TC", "Microsoft JhengHei", sans-serif;
  --font-mono: "IBM Plex Mono", ui-monospace, monospace;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: var(--font);
  color: var(--ink);
  background: var(--bg);
  min-height: 100vh;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

a { color: var(--ink); }
.muted { color: var(--muted); }
.error { color: var(--danger); margin-top: 0.75rem; }
.ok { color: var(--ok); }
.hidden { display: none !important; }

/* —— Login —— */
.login-body {
  display: grid;
  place-items: center;
  min-height: 100vh;
  padding: 1rem;
  background: var(--bg);
}

.login-card {
  width: min(380px, 100%);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 1.75rem 1.5rem;
}

.login-card .brand {
  font-size: 1.75rem;
  font-weight: 700;
  letter-spacing: -0.03em;
  margin: 0 0 0.25rem;
}

.login-card .lede {
  margin: 0 0 1.35rem;
  color: var(--muted);
  font-size: 0.92rem;
}

.login-card button[type="submit"] {
  width: 100%;
  margin-top: 0.35rem;
}

/* —— Topbar —— */
.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1rem;
  padding: 0.9rem 1.25rem;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 10;
}

.topbar .brand {
  font-size: 1.35rem;
  font-weight: 700;
  letter-spacing: -0.03em;
  margin: 0;
  line-height: 1.15;
}

.topbar .brand-sub {
  display: block;
  font-size: 0.72rem;
  font-weight: 500;
  color: var(--muted);
  margin-top: 0.15rem;
  letter-spacing: 0.02em;
}

.layout {
  max-width: 1080px;
  margin: 0 auto;
  padding: 1rem 1.25rem 2.5rem;
}

/* —— Tabs —— */
.tabs {
  display: flex;
  flex-wrap: nowrap;
  gap: 0;
  margin: 0 0 1.25rem;
  border-bottom: 1px solid var(--line);
  overflow-x: auto;
}

.tabs::-webkit-scrollbar {
  height: 0;
  display: none;
}

.tabs button {
  flex: 0 0 auto;
  border: 0;
  border-bottom: 2px solid transparent;
  background: transparent;
  color: var(--muted);
  border-radius: 0;
  padding: 0.7rem 0.9rem;
  margin-bottom: -1px;
  cursor: pointer;
  font: inherit;
  font-size: 0.9rem;
  font-weight: 500;
  white-space: nowrap;
  min-height: var(--control-h);
  transition: color 0.15s ease, border-color 0.15s ease;
}

.tabs button:hover {
  color: var(--ink);
  background: transparent;
}

.tabs button.active {
  color: var(--ink);
  border-bottom-color: var(--ink);
  background: transparent;
  font-weight: 700;
}

/* —— Surface —— */
.surface {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 1.25rem 1.35rem 1.35rem;
  margin-bottom: 1rem;
}

.surface h2 {
  font-size: 1.15rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  margin: 0 0 1rem;
}

.section-heading {
  font-size: 0.95rem;
  font-weight: 700;
  margin: 1.35rem 0 0.75rem;
  padding-top: 1rem;
  border-top: 1px solid var(--line);
}

/* —— Stats —— */
.grid-stats {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 0;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
  background: var(--surface);
}

.stat {
  padding: 0.9rem 1rem;
  border-right: 1px solid var(--line);
}

.stat:last-child { border-right: 0; }

.stat .label {
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.stat .value {
  font-size: 1.5rem;
  font-weight: 700;
  margin-top: 0.3rem;
  letter-spacing: -0.03em;
  line-height: 1.2;
}

.stat .value.value-time {
  font-size: 0.92rem;
  font-weight: 600;
  letter-spacing: 0;
}

/* —— Forms —— */
label {
  display: block;
  margin-bottom: 0;
  font-size: 0.82rem;
  color: var(--muted);
  font-weight: 600;
}

input, select, textarea {
  width: 100%;
  height: var(--control-h);
  margin-top: 0.35rem;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 0 0.7rem;
  font: inherit;
  font-size: 0.92rem;
  color: var(--ink);
  background: #fff;
}

textarea {
  height: auto;
  min-height: 80px;
  padding: 0.55rem 0.7rem;
}

input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--ink);
}

/* Toolbar: label+field + same-height button */
.toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 0.65rem;
  align-items: flex-end;
  margin-bottom: 1rem;
}

.toolbar > label {
  flex: 1 1 220px;
  min-width: 0;
}

.toolbar > button {
  flex: 0 0 auto;
  height: var(--control-h);
  margin: 0;
}

.row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.65rem;
  align-items: flex-end;
  margin-bottom: 0.65rem;
}

.row > * { flex: 1 1 160px; min-width: 0; }

.settings-form > label {
  margin-bottom: 0.85rem;
}

.settings-form > button[type="submit"] {
  margin-top: 0.35rem;
}

/* —— Buttons —— */
button, .btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  height: var(--control-h);
  border: 1px solid var(--ink);
  border-radius: var(--radius);
  padding: 0 0.9rem;
  font: inherit;
  font-size: 0.88rem;
  font-weight: 600;
  cursor: pointer;
  background: var(--ink);
  color: #fff;
  transition: background 0.12s ease, transform 0.08s ease, border-color 0.12s ease;
}

button:hover { background: #2a2a2a; border-color: #2a2a2a; }
button:active { transform: translateY(1px); }

button.ghost {
  background: #fff;
  color: var(--ink);
  border: 1px solid var(--line);
}

button.ghost:hover {
  background: var(--bg);
  border-color: #cfcfcf;
}

button.danger {
  background: var(--danger);
  border-color: var(--danger);
  color: #fff;
}

button.danger:hover {
  background: #912018;
  border-color: #912018;
}

button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
  transform: none;
}

.actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  align-items: center;
}

.actions button {
  height: 32px;
  padding: 0 0.65rem;
  font-size: 0.8rem;
}

.topbar .actions button {
  height: var(--control-h);
  font-size: 0.88rem;
  padding: 0 0.85rem;
}

.actions-spaced { margin: 0.75rem 0; }

/* —— Tables —— */
table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
}

th, td {
  border-bottom: 1px solid var(--line);
  padding: 0.7rem 0.45rem;
  text-align: left;
  vertical-align: middle;
}

th {
  color: var(--muted);
  font-weight: 600;
  font-size: 0.72rem;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

tbody tr:hover { background: #fafafa; }

td.actions-cell {
  white-space: nowrap;
}

code {
  font-family: var(--font-mono);
  font-size: 0.8rem;
  word-break: break-all;
}

/* —— Status —— */
.pill {
  display: inline-block;
  border-radius: var(--radius);
  padding: 0.12rem 0.4rem;
  font-size: 0.75rem;
  font-weight: 600;
  background: var(--bg);
  color: var(--muted);
}

.pill.online {
  background: var(--ok-bg);
  color: var(--ok);
}

.pill.offline {
  background: var(--bg);
  color: #444;
}

.pill.error {
  background: var(--danger-bg);
  color: var(--danger);
}

.msg {
  margin-top: 0.75rem;
  padding: 0.75rem 0.85rem;
  border-radius: var(--radius);
  background: var(--bg);
  border: 1px solid var(--line);
  white-space: pre-wrap;
  word-break: break-word;
  overflow-x: auto;
  font-family: var(--font-mono);
  font-size: 0.8rem;
}

.msg.ok {
  border-color: var(--ok);
  color: var(--ok);
  font-family: var(--font);
}

.msg.error {
  border-color: var(--danger);
  color: var(--danger);
  font-family: var(--font);
}

.tab-panel:not(.hidden) {
  animation: fade-in 0.18s ease-out;
}

@keyframes fade-in {
  from { opacity: 0; transform: translateY(3px); }
  to { opacity: 1; transform: translateY(0); }
}

@media (prefers-reduced-motion: reduce) {
  .tab-panel:not(.hidden),
  button {
    animation: none !important;
    transition: none !important;
  }
  button:active { transform: none; }
}

/* —— Tablet / mobile —— */
@media (max-width: 820px) {
  .grid-stats {
    grid-template-columns: repeat(2, 1fr);
  }
  .stat:nth-child(2) { border-right: 0; }
  .stat:nth-child(1),
  .stat:nth-child(2) {
    border-bottom: 1px solid var(--line);
  }
}

@media (max-width: 720px) {
  .topbar {
    flex-direction: column;
    align-items: stretch;
    gap: 0.75rem;
  }

  .topbar .actions {
    width: 100%;
  }

  .topbar .actions button {
    flex: 1 1 auto;
  }

  .layout {
    padding: 0.85rem 1rem 2rem;
  }

  .surface {
    padding: 1rem;
  }

  .toolbar {
    flex-direction: column;
    align-items: stretch;
  }

  .toolbar > label,
  .toolbar > button {
    flex: none;
    width: 100%;
  }

  .row {
    flex-direction: column;
    align-items: stretch;
  }

  .row > * {
    flex: none;
    width: 100%;
  }

  .settings-form > button[type="submit"] {
    width: 100%;
  }

  .actions-spaced {
    display: flex;
    flex-direction: column;
  }

  .actions-spaced button {
    width: 100%;
    height: var(--control-h);
  }

  table, thead, tbody, th, td, tr { display: block; }
  thead { display: none; }

  tr {
    border: 1px solid var(--line);
    border-radius: var(--radius);
    margin-bottom: 0.75rem;
    padding: 0.65rem 0.75rem;
    background: var(--surface);
  }

  tbody tr:hover { background: var(--surface); }

  td {
    border: 0;
    padding: 0.3rem 0;
    vertical-align: middle;
  }

  td::before {
    content: attr(data-label) "：";
    color: var(--muted);
    font-weight: 600;
    font-size: 0.78rem;
  }

  td.actions-cell {
    white-space: normal;
    padding-top: 0.55rem;
    margin-top: 0.35rem;
    border-top: 1px solid var(--line);
  }

  td.actions-cell::before {
    display: block;
    margin-bottom: 0.35rem;
  }
}

@media (max-width: 480px) {
  .login-body { padding: 1rem; }
  .topbar { padding: 0.85rem 1rem; }
  .topbar .brand { font-size: 1.25rem; }
  .tabs button { padding: 0.65rem 0.75rem; }
  .stat .value { font-size: 1.25rem; }
  .stat .value.value-time { font-size: 0.85rem; }
}
