:root {
  color-scheme: light;
  --bg: #f4f6f8;
  --surface: #ffffff;
  --surface-muted: #f8fafb;
  --text: #17212b;
  --muted: #647383;
  --border: #dce2e7;
  --accent: #b71920;
  --accent-hover: #97151b;
  --success: #16834b;
  --warning: #a85d00;
  --danger: #b42318;
  --radius: 10px;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-width: 320px;
  background: var(--bg);
  color: var(--text);
  font-family:
    Inter, "Noto Sans TC", "Microsoft JhengHei", system-ui, sans-serif;
}

button,
input,
select {
  font: inherit;
}

button {
  cursor: pointer;
}

.hidden {
  display: none !important;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  min-height: 78px;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  padding: 12px 24px;
  border-bottom: 1px solid var(--border);
  background: rgb(255 255 255 / 94%);
  backdrop-filter: blur(12px);
}

.topbar h1,
.topbar p,
.section-title h2,
.section-title p,
.sidebar-heading h2,
.empty-state h2,
.empty-state p {
  margin: 0;
}

.topbar h1 {
  font-size: 22px;
}

.eyebrow {
  color: var(--accent);
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 0.14em;
}

.topbar-actions,
.dialog-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

.button {
  min-height: 40px;
  padding: 8px 14px;
  border: 1px solid transparent;
  border-radius: 7px;
  font-weight: 700;
}

.button.primary {
  background: var(--accent);
  color: white;
}

.button.primary:hover {
  background: var(--accent-hover);
}

.button.secondary {
  border-color: var(--border);
  background: var(--surface);
  color: var(--text);
}

.button.danger {
  border-color: #efb0ac;
  background: var(--surface);
  color: var(--danger);
}

.button:disabled {
  cursor: wait;
  opacity: 0.55;
}

.button.full {
  width: 100%;
}

.status-text {
  min-width: 80px;
  color: var(--muted);
  font-size: 13px;
  text-align: right;
}

.inbox-panel {
  margin-top: 18px;
}

.inbox-panel .section-title {
  align-items: flex-start;
}

.inbox-summary {
  margin: 14px 0;
}

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

.health-badge {
  display: inline-flex;
  min-height: 28px;
  align-items: center;
  padding: 4px 10px;
  border-radius: 999px;
  background: #edf0f3;
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}

.health-badge.active {
  background: #e1f5e9;
  color: var(--success);
}

.health-badge.paused {
  background: #fff0d9;
  color: var(--warning);
}

.inbox-qr-dialog {
  width: min(560px, calc(100vw - 28px));
}

.inbox-qr {
  display: grid;
  margin: 18px auto;
  place-items: center;
}

.inbox-qr svg {
  width: min(360px, 78vw);
  height: auto;
  border: 12px solid white;
  background: white;
}

.secure-link {
  max-height: 110px;
  overflow-wrap: anywhere;
  white-space: normal;
}

@media print {
  body > :not(dialog[open]) {
    display: none !important;
  }

  dialog[open] {
    position: static;
    width: 100%;
    max-width: none;
    border: 0;
    box-shadow: none;
  }

  dialog[open] .dialog-actions,
  dialog[open] .icon-button,
  dialog[open] .secure-link {
    display: none !important;
  }
}

.app-shell {
  display: grid;
  min-height: calc(100vh - 78px);
  grid-template-columns: 250px minmax(0, 1fr);
}

.sidebar {
  padding: 20px 14px;
  border-right: 1px solid var(--border);
  background: var(--surface);
}

.sidebar-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 8px 12px;
}

.sidebar-heading h2 {
  font-size: 15px;
}

.count,
.slot-badge {
  display: inline-grid;
  min-width: 26px;
  height: 26px;
  place-items: center;
  border-radius: 999px;
  background: #edf0f3;
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}

.device-list {
  display: grid;
  gap: 6px;
}

.device-item {
  width: 100%;
  padding: 11px 10px;
  border: 1px solid transparent;
  border-radius: 8px;
  background: transparent;
  color: inherit;
  text-align: left;
}

.device-item:hover,
.device-item.active {
  border-color: var(--border);
  background: var(--surface-muted);
}

.device-item.active {
  border-color: #e6a7aa;
}

.device-item strong,
.device-item span {
  display: block;
}

.device-item span {
  margin-top: 3px;
  color: var(--muted);
  font-size: 12px;
}

.owner-email-field {
  display: grid;
  gap: 6px;
  margin-top: 10px;
  max-width: 360px;
}

.owner-actions {
  margin-top: 8px;
}

.local-auth {
  margin-top: 24px;
  padding: 12px 8px;
  border-top: 1px solid var(--border);
  color: var(--muted);
  font-size: 12px;
}

.local-auth label {
  margin: 12px 0 8px;
}

.content {
  width: 100%;
  max-width: 1120px;
  padding: 28px;
  margin: 0 auto;
}

.empty-state {
  padding: 80px 24px;
  color: var(--muted);
  text-align: center;
}

.empty-state h2 {
  margin-bottom: 8px;
  color: var(--text);
}

.device-form {
  display: grid;
  gap: 20px;
}

.section-header,
.settings-panel,
.routes-section {
  padding: 20px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
}

.section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
}

.device-name-input {
  width: min(520px, 100%);
  padding: 2px 0 6px;
  border: 0;
  border-bottom: 1px solid transparent;
  background: transparent;
  color: var(--text);
  font-size: 28px;
  font-weight: 800;
}

.device-name-input:focus {
  border-bottom-color: var(--accent);
  outline: 0;
}

.health-line {
  margin: 0 0 5px;
  color: var(--success);
  font-size: 13px;
  font-weight: 700;
}

.muted,
.section-title p {
  color: var(--muted);
}

.muted {
  margin: 6px 0 0;
  font-size: 13px;
}

.section-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 18px;
}

.section-title h2 {
  font-size: 18px;
}

.section-title p {
  margin-top: 4px;
  font-size: 13px;
}

.settings-grid,
.route-fields {
  display: grid;
  gap: 14px;
  grid-template-columns: repeat(5, minmax(0, 1fr));
}

.theme-field {
  display: grid;
  gap: 10px;
  margin-bottom: 18px;
}

.theme-field-label {
  color: var(--muted);
  font-size: 13px;
  font-weight: 650;
}

.theme-picker {
  display: grid;
  gap: 10px;
  grid-template-columns: repeat(6, minmax(0, 1fr));
}

.theme-swatch {
  display: grid;
  gap: 8px;
  padding: 10px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--bg);
  color: var(--text);
  text-align: left;
  cursor: pointer;
}

.theme-swatch:hover {
  border-color: color-mix(in srgb, var(--accent) 45%, var(--border));
}

.theme-swatch.is-selected {
  border-color: var(--accent);
  box-shadow: inset 0 0 0 1px var(--accent);
}

.theme-swatch-preview {
  display: block;
  height: 56px;
  border-radius: 8px;
  background:
    linear-gradient(90deg, var(--swatch-eta) 0 30%, transparent 30%),
    linear-gradient(
      180deg,
      var(--swatch-chrome) 0 38%,
      var(--swatch-bg) 38% 78%,
      var(--swatch-chrome) 78%
    );
  box-shadow: inset 0 0 0 1px
    color-mix(in srgb, var(--swatch-text) 14%, transparent);
  position: relative;
}

.theme-swatch-preview::before {
  content: "";
  position: absolute;
  left: 10px;
  top: 10px;
  width: 14px;
  height: 14px;
  border-radius: 4px;
  background: var(--swatch-eta);
  box-shadow:
    18px 0 0 var(--swatch-chrome),
    36px 0 0 var(--swatch-text);
}

.theme-swatch-preview::after {
  content: "12";
  position: absolute;
  right: 10px;
  top: 50%;
  transform: translateY(-58%);
  color: var(--swatch-eta);
  font-size: 18px;
  font-weight: 800;
  letter-spacing: 0.02em;
}

.theme-swatch-name {
  font-size: 14px;
  font-weight: 800;
}

.theme-swatch-hint {
  color: var(--muted);
  font-size: 11px;
  font-weight: 600;
  line-height: 1.3;
}

label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 650;
}

input,
select {
  width: 100%;
  min-height: 40px;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 7px;
  background: var(--surface);
  color: var(--text);
}

input:focus,
select:focus {
  border-color: #bd5e63;
  outline: 2px solid rgb(183 25 32 / 12%);
}

input[type="range"] {
  padding: 0;
  accent-color: var(--accent);
}

output {
  color: var(--text);
}

.route-list {
  display: grid;
  gap: 12px;
}

.route-editor {
  position: relative;
  padding: 16px;
  border: 1px solid var(--border);
  border-radius: 9px;
  background: var(--surface-muted);
  transition:
    border-color 120ms ease,
    box-shadow 120ms ease,
    opacity 120ms ease;
}

.route-editor.is-dragging {
  border-color: var(--accent);
  box-shadow: 0 8px 24px rgb(23 33 43 / 16%);
  opacity: 0.72;
}

.route-editor-header,
.route-summary {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.route-editor-header {
  margin-bottom: 14px;
}

.route-editor-order {
  display: flex;
  align-items: center;
  gap: 8px;
}

.route-drag-handle {
  display: inline-grid;
  width: 34px;
  height: 34px;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 7px;
  place-items: center;
  background: var(--surface);
  color: var(--muted);
  cursor: grab;
  font-size: 20px;
  line-height: 1;
  touch-action: none;
  user-select: none;
}

.route-drag-handle:hover,
.route-drag-handle:focus-visible {
  border-color: #bd5e63;
  color: var(--accent);
  outline: 2px solid rgb(183 25 32 / 12%);
}

.route-drag-handle:active {
  cursor: grabbing;
}

.enabled-toggle {
  display: flex;
  align-items: center;
  gap: 7px;
}

.enabled-toggle input {
  width: 18px;
  min-height: 18px;
  accent-color: var(--accent);
}

.route-search-field,
.wide-field {
  grid-column: span 2;
}

.input-action {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
}

.input-action input {
  border-radius: 7px 0 0 7px;
}

.input-action button {
  border-radius: 0 7px 7px 0;
}

.route-summary {
  min-height: 54px;
  margin-top: 14px;
  padding-top: 14px;
  border-top: 1px solid var(--border);
}

.route-summary strong,
.route-summary span {
  display: inline-block;
  margin-right: 10px;
}

.summary-route {
  color: var(--accent);
  font-size: 18px;
}

.summary-destination,
.summary-stop {
  color: var(--muted);
  font-size: 13px;
}

.eta-preview-result {
  min-height: 0;
  margin-top: 8px;
  color: var(--success);
  font-size: 13px;
  font-weight: 700;
}

dialog {
  width: min(480px, calc(100vw - 32px));
  padding: 22px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  color: var(--text);
}

dialog.wide-dialog {
  width: min(820px, calc(100vw - 32px));
}

dialog::backdrop {
  background: rgb(16 24 32 / 55%);
}

.dialog-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 18px;
}

.dialog-header h2 {
  margin: 0;
}

.icon-button {
  width: 36px;
  height: 36px;
  border: 0;
  background: transparent;
  color: var(--muted);
  font-size: 26px;
}

.dialog-actions {
  justify-content: flex-end;
  margin-top: 20px;
}

.token-box {
  display: block;
  padding: 14px;
  overflow-wrap: anywhere;
  border: 1px solid #e9b5b8;
  border-radius: 7px;
  background: #fff7f7;
  color: #781217;
  user-select: all;
}

.toast-region {
  position: fixed;
  right: 18px;
  bottom: 18px;
  z-index: 50;
  display: grid;
  max-width: min(380px, calc(100vw - 36px));
  gap: 8px;
}

.toast {
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
}

.toast.error {
  border-color: #efb0ac;
  color: var(--danger);
}

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

.firmware-list {
  display: grid;
  gap: 8px;
  max-height: 300px;
  margin-top: 20px;
  overflow-y: auto;
}

.firmware-release {
  display: grid;
  align-items: end;
  gap: 10px;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  grid-template-columns: minmax(120px, 1fr) 110px 100px auto;
}

.firmware-release p {
  margin: 3px 0 0;
  color: var(--muted);
  font-size: 12px;
}

@media (max-width: 900px) {
  .app-shell {
    grid-template-columns: 1fr;
  }

  .sidebar {
    position: sticky;
    top: 78px;
    z-index: 8;
    padding: 10px 14px;
    overflow-x: auto;
    border-right: 0;
    border-bottom: 1px solid var(--border);
  }

  .sidebar-heading,
  .local-auth {
    display: none;
  }

  .device-list {
    display: flex;
  }

  .device-item {
    width: auto;
    min-width: 150px;
  }

  .content {
    padding: 18px;
  }

  .settings-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .theme-picker {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .route-fields {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .firmware-release {
    grid-template-columns: 1fr 1fr;
  }
}

@media (max-width: 580px) {
  .topbar {
    align-items: flex-start;
    padding: 10px 14px;
  }

  .topbar-actions {
    flex-wrap: wrap;
    justify-content: flex-end;
  }

  #refresh-button,
  .status-text {
    display: none;
  }

  .content {
    padding: 12px;
  }

  .section-header {
    align-items: stretch;
    flex-direction: column;
  }

  .device-name-input {
    font-size: 23px;
  }

  .settings-grid,
  .route-fields,
  .firmware-fields {
    grid-template-columns: 1fr;
  }

  .theme-picker {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .route-search-field,
  .wide-field {
    grid-column: auto;
  }

  .route-summary {
    align-items: flex-start;
    flex-direction: column;
  }
}
