:root {
  color-scheme: light;
  --bg: #f4f6f5;
  --surface: #ffffff;
  --surface-muted: #eef2f0;
  --text: #17201d;
  --text-muted: #66716d;
  --line: #d9e0dd;
  --accent: #17745b;
  --accent-hover: #105e49;
  --accent-soft: #e1f1eb;
  --info: #296ca8;
  --info-soft: #e6f0f8;
  --warning: #a85d12;
  --warning-soft: #fbefdf;
  --danger: #b43c3c;
  --danger-soft: #fae7e7;
  --shadow: 0 8px 28px rgba(31, 47, 41, 0.09);
  font-family: Inter, "PingFang SC", "Microsoft YaHei", system-ui, sans-serif;
  font-size: 16px;
  letter-spacing: 0;
}

* { box-sizing: border-box; }

html { min-height: 100%; background: var(--bg); }

body {
  min-height: 100vh;
  margin: 0;
  color: var(--text);
  background: var(--bg);
  line-height: 1.45;
  letter-spacing: 0;
}

button, input, select, textarea { font: inherit; letter-spacing: 0; }
button, a { -webkit-tap-highlight-color: transparent; }
button { cursor: pointer; }
a { color: inherit; text-decoration: none; }

.app-header {
  position: sticky;
  top: 0;
  z-index: 20;
  background: rgba(255, 255, 255, 0.96);
  border-bottom: 1px solid var(--line);
  backdrop-filter: blur(10px);
}

.header-inner {
  width: min(1120px, 100%);
  height: 64px;
  margin: 0 auto;
  padding: 0 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.brand-lockup { display: flex; align-items: center; gap: 10px; }
.brand-lockup div { display: flex; flex-direction: column; }
.brand-lockup strong { font-size: 18px; }
.brand-lockup span:not(.brand-mark) { color: var(--text-muted); font-size: 12px; }

.brand-mark {
  width: 54px;
  height: 54px;
  display: grid;
  place-items: center;
  border-radius: 8px;
  background: var(--accent);
  color: white;
  font-size: 24px;
  font-weight: 800;
}

.brand-mark.small { width: 36px; height: 36px; font-size: 16px; }

.app-main {
  width: min(1120px, 100%);
  margin: 0 auto;
  padding: 24px 20px 72px;
}

.overview {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 1px;
  overflow: hidden;
  background: var(--line);
  border: 1px solid var(--line);
  border-radius: 8px;
}

.metric { padding: 15px 18px; background: var(--surface); }
.metric span { display: block; margin-bottom: 4px; color: var(--text-muted); font-size: 12px; }
.metric strong { display: block; overflow: hidden; font-size: 20px; white-space: nowrap; text-overflow: ellipsis; }
.traffic-track { grid-column: 1 / -1; height: 4px; background: var(--surface-muted); }
.traffic-track span { display: block; width: 0; height: 100%; background: var(--accent); transition: width 240ms ease; }
.traffic-track span.warning { background: var(--warning); }
.traffic-track span.danger { background: var(--danger); }

.tab-bar {
  display: flex;
  gap: 4px;
  margin: 22px 0 8px;
  border-bottom: 1px solid var(--line);
}

.tab {
  min-height: 44px;
  padding: 0 16px;
  border: 0;
  border-bottom: 2px solid transparent;
  background: transparent;
  color: var(--text-muted);
}

.tab span { margin-right: 6px; }
.tab:hover { color: var(--text); }
.tab.active { border-bottom-color: var(--accent); color: var(--accent); font-weight: 700; }

.tab-panel { display: none; padding-top: 12px; }
.tab-panel.active { display: block; }

.section-heading {
  min-height: 58px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 12px;
}

.section-heading h2 { margin: 0; font-size: 20px; }
.section-heading p { margin: 2px 0 0; color: var(--text-muted); font-size: 13px; }

.button {
  min-height: 40px;
  padding: 0 15px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border: 1px solid transparent;
  border-radius: 6px;
  background: transparent;
  color: var(--text);
  font-weight: 650;
  white-space: nowrap;
}

.button.primary { background: var(--accent); color: white; }
.button.primary:hover { background: var(--accent-hover); }
.button.secondary { border-color: var(--line); background: var(--surface); }
.button.secondary:hover { border-color: #aeb9b5; }
.button.quiet { color: var(--text-muted); }
.button.quiet:hover { background: var(--surface-muted); color: var(--text); }
.button.danger { background: var(--danger-soft); color: var(--danger); }
.button.full { width: 100%; }
.button:disabled { cursor: not-allowed; opacity: 0.5; }

.icon-button {
  width: 40px;
  height: 40px;
  padding: 0;
  display: grid;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text);
  font-size: 22px;
}

.icon-button:hover { background: var(--surface-muted); }
.icon-button.danger { color: var(--danger); }

.room-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }

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

.room-card.recording { border-color: #85bfae; box-shadow: inset 3px 0 0 var(--accent); }
.room-top { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; }
.room-title { min-width: 0; }
.room-title h3 { margin: 0; overflow: hidden; font-size: 17px; white-space: nowrap; text-overflow: ellipsis; }
.room-title p { margin: 3px 0 0; overflow: hidden; color: var(--text-muted); font-size: 12px; white-space: nowrap; text-overflow: ellipsis; }

.status-badge {
  flex: 0 0 auto;
  padding: 4px 8px;
  border-radius: 999px;
  background: var(--surface-muted);
  color: var(--text-muted);
  font-size: 12px;
  font-weight: 700;
}

.status-badge.recording { background: var(--accent-soft); color: var(--accent); }
.status-badge.error, .status-badge.disk_full { background: var(--danger-soft); color: var(--danger); }
.status-badge.checking, .status-badge.waiting_slot { background: var(--warning-soft); color: var(--warning); }
.status-badge.offline { background: var(--info-soft); color: var(--info); }

.room-detail {
  min-height: 43px;
  margin: 14px 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: start;
  gap: 10px;
}

.room-detail strong, .room-detail span { display: block; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.room-detail strong { font-size: 14px; }
.room-detail span { margin-top: 3px; color: var(--text-muted); font-size: 12px; }
.quality-mark { color: var(--text-muted); font-size: 12px; }
.room-error { margin: 0 0 12px; color: var(--danger); font-size: 12px; overflow-wrap: anywhere; }

.room-actions { display: flex; align-items: center; justify-content: space-between; gap: 8px; border-top: 1px solid var(--line); padding-top: 12px; }
.room-actions .action-group { display: flex; gap: 6px; }
.room-actions .button { min-height: 36px; padding: 0 10px; font-size: 13px; }

.monitor-switch { position: relative; display: inline-flex; flex-direction: row; align-items: center; gap: 8px; color: var(--text-muted); font-size: 12px; }
.monitor-switch input, .switch-row input {
  position: relative;
  width: 42px;
  height: 24px;
  min-height: 24px;
  flex: 0 0 42px;
  margin: 0;
  padding: 0;
  border: 1px solid #aebbb5;
  border-radius: 999px;
  appearance: none;
  background: #dce3df;
  cursor: pointer;
  transition: background-color 160ms ease, border-color 160ms ease;
}
.monitor-switch input::after, .switch-row input::after {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 3px rgba(20, 35, 29, 0.28);
  transition: transform 160ms ease;
}
.monitor-switch input:checked, .switch-row input:checked { border-color: var(--accent); background: var(--accent); }
.monitor-switch input:checked::after, .switch-row input:checked::after { transform: translateX(18px); }
.monitor-switch input:focus, .switch-row input:focus { box-shadow: none; }
.monitor-switch input:focus-visible, .switch-row input:focus-visible { outline: 3px solid rgba(25, 126, 95, 0.22); outline-offset: 2px; }
.monitor-switch input:disabled, .switch-row input:disabled { cursor: wait; opacity: 0.65; }

.recording-list { border-top: 1px solid var(--line); }
.recording-item {
  display: grid;
  grid-template-columns: 144px minmax(0, 1fr) auto;
  align-items: center;
  gap: 16px;
  padding: 12px 0;
  border-bottom: 1px solid var(--line);
}

.recording-thumb {
  width: 144px;
  aspect-ratio: 16 / 9;
  overflow: hidden;
  display: grid;
  place-items: center;
  border-radius: 6px;
  background: #dfe5e2;
  color: var(--text-muted);
}

.recording-thumb img { width: 100%; height: 100%; object-fit: cover; }
.recording-info { min-width: 0; }
.recording-info strong { display: block; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.recording-info p { margin: 5px 0 0; color: var(--text-muted); font-size: 12px; }
.recording-actions { display: flex; gap: 6px; }
.clip-badge { margin-left: 7px; padding: 2px 5px; border-radius: 4px; background: var(--info-soft); color: var(--info); font-size: 11px; }

.empty-state {
  min-height: 180px;
  display: grid;
  place-items: center;
  border: 1px dashed #bfc9c5;
  border-radius: 8px;
  color: var(--text-muted);
  text-align: center;
}

.empty-state strong { display: block; margin-bottom: 4px; color: var(--text); }

label { display: flex; flex-direction: column; gap: 7px; color: var(--text-muted); font-size: 13px; }
input, select, textarea {
  width: 100%;
  min-height: 42px;
  padding: 9px 11px;
  border: 1px solid #c7d0cd;
  border-radius: 6px;
  outline: none;
  background: var(--surface);
  color: var(--text);
}

textarea { resize: vertical; }
input:focus, select:focus, textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }

.settings-form { margin-bottom: 22px; }
.settings-band { padding: 18px 0; border-top: 1px solid var(--line); }
.settings-band h3 { margin: 0 0 14px; font-size: 15px; }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 14px; }
.form-grid.compact { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.form-grid .wide { grid-column: 1 / -1; }
.logout-form { display: flex; align-items: center; justify-content: space-between; padding: 16px 0; border-top: 1px solid var(--line); color: var(--text-muted); }

.app-dialog, .video-dialog {
  width: min(620px, calc(100% - 24px));
  max-height: calc(100vh - 32px);
  padding: 0;
  overflow: auto;
  border: 0;
  border-radius: 8px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.app-dialog::backdrop, .video-dialog::backdrop { background: rgba(18, 27, 24, 0.56); }
.app-dialog form { padding: 18px; }
.dialog-heading { display: flex; align-items: center; justify-content: space-between; gap: 16px; margin-bottom: 16px; }
.dialog-heading h2 { margin: 0; min-width: 0; overflow: hidden; font-size: 18px; white-space: nowrap; text-overflow: ellipsis; }
.dialog-actions { display: flex; align-items: center; justify-content: flex-end; gap: 8px; margin-top: 18px; }
.switch-list { margin-top: 16px; border-top: 1px solid var(--line); }
.switch-row { min-height: 48px; flex-direction: row; align-items: center; justify-content: space-between; border-bottom: 1px solid var(--line); color: var(--text); }
.hidden { display: none !important; }

.video-dialog { padding: 16px; }
.video-dialog video, .clip-dialog video { width: 100%; max-height: 52vh; aspect-ratio: 16 / 9; display: block; background: #101412; border-radius: 6px; object-fit: contain; }
.video-meta { margin-top: 10px; color: var(--text-muted); font-size: 13px; }

.clip-selection { min-height: 52px; display: flex; align-items: center; justify-content: space-between; gap: 12px; border-bottom: 1px solid var(--line); }
.clip-selection strong { font-size: 16px; }
.clip-selection span { color: var(--text-muted); font-size: 12px; text-align: right; }
.clip-sliders { display: grid; gap: 12px; padding: 14px 0; }
.clip-sliders label > span { display: flex; justify-content: space-between; }
.clip-sliders output { color: var(--text); font-variant-numeric: tabular-nums; }
.clip-sliders input[type="range"] { min-height: 30px; padding: 0; border: 0; box-shadow: none; accent-color: var(--accent); }
.clip-marker-actions { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
.clip-marker-actions .button { min-width: 0; padding: 0 8px; }
.clip-presets { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 1px; margin-top: 10px; overflow: hidden; border: 1px solid var(--line); border-radius: 6px; background: var(--line); }
.clip-presets button { min-height: 38px; padding: 0 6px; border: 0; background: var(--surface); color: var(--text-muted); }
.clip-presets button:hover { background: var(--surface-muted); color: var(--text); }

.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  z-index: 100;
  max-width: calc(100% - 32px);
  padding: 11px 16px;
  transform: translate(-50%, 20px);
  border-radius: 6px;
  background: #1d2824;
  color: white;
  opacity: 0;
  pointer-events: none;
  transition: opacity 180ms ease, transform 180ms ease;
  box-shadow: var(--shadow);
}

.toast.visible { opacity: 1; transform: translate(-50%, 0); }
.toast.error { background: var(--danger); }

.auth-page { display: grid; place-items: center; padding: 24px; background: #edf2ef; }
.auth-shell { width: min(380px, 100%); padding: 30px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); box-shadow: var(--shadow); }
.auth-shell .brand-mark { margin: 0 auto 14px; }
.auth-shell h1 { margin: 0; text-align: center; font-size: 24px; }
.auth-heading { margin: 4px 0 22px; color: var(--text-muted); text-align: center; }
.auth-form { display: grid; gap: 14px; }
.form-error { margin-bottom: 14px; padding: 10px 12px; border-radius: 6px; background: var(--danger-soft); color: var(--danger); font-size: 13px; }

@media (max-width: 760px) {
  .header-inner { height: 58px; padding: 0 14px; }
  .app-main { padding: 14px 12px 86px; }
  .overview { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .metric { padding: 12px; }
  .metric strong { font-size: 17px; }
  .tab-bar {
    position: fixed;
    right: 0;
    bottom: 0;
    left: 0;
    z-index: 30;
    margin: 0;
    padding: 6px max(8px, env(safe-area-inset-right)) calc(6px + env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-left));
    justify-content: space-around;
    border-top: 1px solid var(--line);
    border-bottom: 0;
    background: rgba(255, 255, 255, 0.98);
  }
  .tab { min-width: 92px; min-height: 48px; padding: 3px 8px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 1px; border: 0; font-size: 12px; }
  .tab span { margin: 0; font-size: 15px; }
  .tab.active { border: 0; color: var(--accent); }
  .tab-panel { padding-top: 2px; }
  .section-heading { margin-bottom: 8px; }
  .section-heading h2 { font-size: 18px; }
  .room-grid { grid-template-columns: 1fr; gap: 10px; }
  .room-card { padding: 14px; }
  .room-actions { align-items: flex-end; }
  .recording-item { grid-template-columns: 104px minmax(0, 1fr); gap: 10px; }
  .recording-thumb { width: 104px; }
  .recording-actions { grid-column: 1 / -1; display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)) 40px; }
  .recording-actions .button { min-width: 0; padding-right: 8px; padding-left: 8px; }
  .form-grid, .form-grid.compact { grid-template-columns: 1fr; }
  .app-dialog { width: calc(100% - 16px); }
  .app-dialog form { padding: 14px; }
  .video-dialog { width: calc(100% - 16px); padding: 12px; }
  .video-dialog .dialog-actions { display: grid; grid-template-columns: 1fr 1.4fr; }
  .video-dialog .dialog-actions .button { width: 100%; }
  .clip-dialog { width: calc(100% - 16px); }
  .clip-dialog form { padding: 12px; }
  .clip-marker-actions .button { font-size: 13px; }
  .toast { bottom: calc(74px + env(safe-area-inset-bottom)); }
}

@media (max-width: 370px) {
  .button { padding-right: 11px; padding-left: 11px; }
  .room-actions { flex-direction: column; align-items: stretch; }
  .room-actions .action-group { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .monitor-switch { justify-content: space-between; }
}
