:root {
  --bg: #ffffff;
  --card: #ffffff;
  --navy: #212230;
  --accent: #1c4fd4;
  --accent-2: #527cf8;
  --text: var(--navy);
  --muted: #666674;
  --border: #e6e6e6;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: "Poppins", system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
  background: #f7f8fb;
  color: var(--text);
}

body.settings-open {
  overflow: hidden;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 1.25rem;
  background: var(--card);
  border-bottom: 1px solid var(--border);
  gap: 0.75rem;
  position: relative;
}

.brand {
  font-weight: 700;
  font-size: 1.2rem;
  letter-spacing: 0.02em;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 0.8rem;
  min-width: 0;
}

.menu-toggle {
  display: none;
  border: 1px solid var(--border);
  background: #ffffff;
  color: var(--navy);
  border-radius: 10px;
  padding: 0.4rem 0.55rem;
  font-size: 1rem;
  line-height: 1;
}

.nav {
  display: flex;
  gap: 0.75rem;
  flex-wrap: nowrap;
  align-items: center;
  justify-content: center;
  min-width: 0;
}

.link {
  color: var(--accent);
  text-decoration: none;
  font-weight: 600;
}

.link:hover {
  text-decoration: underline;
}

.content {
  padding: 1.5rem;
}

.operation-warning {
  position: fixed;
  top: 4.2rem;
  right: 1rem;
  z-index: 80;
  max-width: min(340px, calc(100vw - 2rem));
  background: #fff8e8;
  color: #6d4700;
  border: 1px solid #e6a020;
  border-radius: 8px;
  padding: 0.65rem 0.8rem;
  font-size: 0.86rem;
  font-weight: 600;
  box-shadow: 0 10px 24px rgba(33, 34, 48, 0.14);
}

.user {
  color: var(--muted);
  font-size: 0.92rem;
  white-space: nowrap;
}

.card {
  background: var(--card);
  border-radius: 16px;
  padding: 1.5rem;
  border: 1px solid var(--border);
  box-shadow: 0 12px 32px rgba(33, 34, 48, 0.08);
  max-width: 900px;
  margin: 1rem auto;
}

.card-wide {
  max-width: 1100px;
}

.dashboard-header {
  margin-bottom: 1rem;
}

.dashboard-header h1 {
  margin: 0 0 0.35rem 0;
}

.device-card-grid {
  display: grid;
  gap: 1.25rem;
}

.device-card {
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 1rem;
  background: #ffffff;
}

.device-card-header {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: flex-start;
  gap: 0.75rem;
  margin-bottom: 1rem;
}

.device-id {
  margin: 0;
  font-size: 1.05rem;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

.device-meta {
  margin: 0.25rem 0 0 0;
  font-size: 0.88rem;
}

.badge-row {
  display: grid;
  grid-template-columns: repeat(3, max-content);
  gap: 0.4rem;
  justify-content: flex-end;
  min-height: 2.1rem;
}

.badge {
  display: inline-block;
  padding: 0.25rem 0.55rem;
  border-radius: 999px;
  font-size: 0.78rem;
  font-weight: 600;
  border: 1px solid var(--border);
  background: #f3f5fb;
  color: var(--navy);
}

.badge-ok {
  background: #e8f8ec;
  border-color: #b8f0c4;
  color: #1a6b32;
}

.badge-accent {
  background: #e9ecf8;
  border-color: #c5d4fa;
  color: var(--accent);
}

.badge-muted {
  background: #f7f8fb;
  color: var(--muted);
}

.viz-row {
  display: grid;
  grid-template-columns: 1fr;
  gap: 1rem;
  justify-items: center;
}

.viz-tile h3 {
  margin: 0 0 0.5rem 0;
  font-size: 0.92rem;
  font-weight: 600;
  color: var(--muted);
}

.viz-tile-dark h3 {
  color: rgba(255, 255, 255, 0.75);
}

.viz-tile-dark {
  background: var(--navy);
  border-radius: 12px;
  padding: 0.75rem;
}

.viz-canvas {
  width: 100%;
  max-width: 320px;
  height: auto;
  display: block;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: #ffffff;
}

.viz-tile-dark .viz-canvas {
  border-color: rgba(255, 255, 255, 0.12);
}

.device-details {
  margin-top: 0.85rem;
  font-size: 0.9rem;
}

.device-details summary {
  cursor: pointer;
  color: var(--accent);
  font-weight: 600;
}

.details-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 0.75rem;
  margin-top: 0.5rem;
}

.raw-block {
  margin: 0;
  font-size: 0.75rem;
  max-height: 200px;
  overflow: auto;
}

label {
  display: block;
  margin-bottom: 0.75rem;
  font-weight: 600;
}

input {
  width: 100%;
  padding: 0.65rem;
  margin-top: 0.35rem;
  border-radius: 10px;
  border: 1px solid var(--border);
}

select {
  width: 100%;
  padding: 0.65rem;
  margin-top: 0.35rem;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: #ffffff;
}

button {
  background: var(--accent);
  color: #fff;
  border: none;
  padding: 0.7rem 1.2rem;
  border-radius: 12px;
  font-weight: 600;
  cursor: pointer;
}

.btn.secondary {
  background: #e9ecf8;
  color: var(--navy);
  border: 1px solid var(--border);
}

.stack {
  display: grid;
  gap: 1rem;
}

.panel {
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 1rem;
  background: #ffffff;
}

.row {
  display: flex;
  gap: 0.75rem;
  align-items: center;
  flex-wrap: wrap;
}

.grid2 {
  display: grid;
  grid-template-columns: 1fr;
  gap: 0.75rem;
}

.field {
  display: grid;
  gap: 0.35rem;
  margin-bottom: 0;
}

.control {
  width: 100%;
}

.textarea {
  width: 100%;
  border-radius: 10px;
  border: 1px solid var(--border);
  padding: 0.65rem;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
  font-size: 0.95rem;
}

.progress {
  width: 100%;
  height: 10px;
  background: #eef1fb;
  border: 1px solid var(--border);
  border-radius: 999px;
  overflow: hidden;
}

.progress-bar {
  height: 100%;
  background: linear-gradient(90deg, var(--accent), var(--accent-2));
  width: 0%;
}

.preview-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 1rem;
}

.tile h3 {
  margin: 0 0 0.5rem 0;
}

.canvas {
  width: 100%;
  max-width: 320px;
  height: auto;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: #ffffff;
}

.response {
  margin-top: 0.75rem;
}

.control.config-pending,
.config-pending.control {
  border-color: #e6a020 !important;
  background: #fff8e8 !important;
  box-shadow: 0 0 0 2px rgba(230, 160, 32, 0.25);
  animation: config-pending-pulse 1.4s ease-in-out infinite;
}

.control.config-error,
.config-error.control {
  border-color: #c94a3a !important;
  background: #fff0ee !important;
}

.control.config-confirmed,
.config-confirmed.control {
  animation: config-confirmed-fade 1.6s ease-out forwards;
}

@keyframes config-pending-pulse {
  0%,
  100% {
    box-shadow: 0 0 0 2px rgba(230, 160, 32, 0.2);
  }
  50% {
    box-shadow: 0 0 0 4px rgba(230, 160, 32, 0.45);
  }
}

@keyframes config-confirmed-fade {
  0% {
    background: #b8f0c4;
    border-color: #2ecc71;
    box-shadow: 0 0 0 3px rgba(46, 204, 113, 0.45);
  }
  35% {
    background: #b8f0c4;
    border-color: #2ecc71;
    box-shadow: 0 0 0 3px rgba(46, 204, 113, 0.35);
  }
  100% {
    background: #ffffff;
    border-color: var(--border);
    box-shadow: none;
  }
}

.toast-stack {
  position: fixed;
  top: 1rem;
  right: 1rem;
  z-index: 1000;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  max-width: min(360px, calc(100vw - 2rem));
  pointer-events: none;
}

.toast-wake {
  pointer-events: auto;
  background: var(--navy);
  color: #ffffff;
  border-radius: 12px;
  padding: 0.85rem 1rem;
  box-shadow: 0 12px 32px rgba(33, 34, 48, 0.25);
  font-size: 0.92rem;
  line-height: 1.4;
  animation: toast-in 0.25s ease-out;
}

.toast-wake strong {
  display: block;
  margin-bottom: 0.25rem;
  color: #b8f0c4;
}

@keyframes toast-in {
  from {
    opacity: 0;
    transform: translateY(-8px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.muted {
  color: var(--muted);
  font-size: 0.95rem;
}

.error {
  color: #a8382b;
  font-weight: 600;
}

.device-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 1.2rem;
}

.device h2 {
  margin: 0 0 0.5rem 0;
}

.telemetry {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 1rem;
}

pre {
  background: #f3f5fb;
  padding: 0.8rem;
  border-radius: 10px;
  overflow-x: auto;
  border: 1px solid var(--border);
}

.tagger-card {
  max-width: 760px;
  margin-bottom: 6rem;
}

.tagger-head h1 {
  margin: 0 0 0.4rem 0;
}

.tagger-image-wrap {
  display: flex;
  justify-content: center;
  margin: 0.8rem 0 1rem 0;
  position: relative;
  width: 100%;
  max-width: 640px;
  margin-left: auto;
  margin-right: auto;
}

.tagger-photo {
  width: 100%;
  max-width: 640px;
  border-radius: 14px;
  border: 1px solid var(--border);
  background: #f3f5fb;
  height: auto;
  object-fit: contain;
}

.photo-nav {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  z-index: 2;
  width: 2.4rem;
  height: 2.4rem;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: rgba(255, 255, 255, 0.9);
  color: var(--navy);
  padding: 0;
  font-size: 1.2rem;
}

.photo-nav-left {
  left: 0.65rem;
}

.photo-nav-right {
  right: 0.65rem;
}

.photo-count {
  position: absolute;
  right: 0.75rem;
  bottom: 0.75rem;
  z-index: 2;
  border-radius: 999px;
  background: rgba(33, 34, 48, 0.82);
  color: #ffffff;
  font-size: 0.78rem;
  font-weight: 600;
  padding: 0.25rem 0.55rem;
}

.tagger-controls {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  bottom: 0.9rem;
  z-index: 70;
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 0.6rem;
  width: min(560px, calc(100vw - 1.2rem));
  background: #ffffff;
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 0.5rem;
  box-shadow: 0 14px 30px rgba(33, 34, 48, 0.2);
}

.control-btn {
  min-height: 2.8rem;
  font-size: 1.02rem;
}

.tagger-settings {
  position: fixed;
  left: 0;
  right: 0;
  bottom: -420px;
  z-index: 68;
  transition: bottom 0.25s ease;
  padding: 0.6rem;
}

.tagger-settings.open {
  bottom: 4.6rem;
}

.tagger-settings .panel {
  max-width: 760px;
  margin: 0 auto;
  box-shadow: 0 14px 30px rgba(33, 34, 48, 0.2);
}

.tagger-settings-handle {
  width: 62px;
  height: 6px;
  border-radius: 999px;
  background: #cfd6ea;
  margin: 0 auto 0.45rem auto;
}

.settings-title-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
}

.settings-title-row h2 {
  margin: 0;
}

.tag-data-layout {
  display: grid;
  gap: 1rem;
  max-width: 1180px;
  margin: 0 auto;
}

.tag-data-list,
.tag-data-detail {
  min-width: 0;
}

.capture-list {
  display: grid;
  gap: 0.45rem;
  margin-top: 0.75rem;
}

.capture-list-item {
  width: 100%;
  text-align: left;
  background: #ffffff;
  color: var(--navy);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.65rem 0.75rem;
  font-size: 0.9rem;
}

.capture-list-item:hover {
  border-color: var(--accent);
}

.tag-data-viz {
  margin: 1rem 0;
}

.chart-canvas {
  max-width: 520px;
}

.viz-tile {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
}

@media (max-width: 767px) {
  .topbar {
    padding: 0.7rem 0.9rem;
    gap: 0.5rem;
  }

  .brand {
    font-size: 1rem;
  }

  .menu-toggle {
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }

  .topbar-actions {
    display: none;
    position: absolute;
    top: calc(100% + 0.4rem);
    right: 0.8rem;
    background: #ffffff;
    border: 1px solid var(--border);
    border-radius: 12px;
    box-shadow: 0 14px 30px rgba(33, 34, 48, 0.2);
    padding: 0.65rem;
    z-index: 50;
    min-width: 210px;
    flex-direction: column;
    align-items: stretch;
    gap: 0.45rem;
  }

  .topbar-actions.open {
    display: flex;
  }

  .nav {
    display: grid;
    gap: 0.3rem;
    justify-content: stretch;
  }

  .nav .link {
    display: block;
  }

  .link {
    font-size: 0.94rem;
    padding: 0.2rem 0.1rem;
    white-space: normal;
  }

  .user {
    font-size: 0.86rem;
  }

  .content {
    padding: 0.9rem;
  }

  .operation-warning {
    top: 3.8rem;
    right: 0.7rem;
    font-size: 0.8rem;
  }

  .tagger-settings {
    bottom: -520px;
  }

  .tagger-settings.open {
    bottom: 5.1rem;
  }

  .badge-row {
    grid-template-columns: repeat(2, max-content);
    justify-content: flex-start;
  }
}

@media (min-width: 768px) {
  .content {
    padding: 2rem 3rem;
  }

  .grid2 {
    grid-template-columns: 1fr 1fr;
  }

  .preview-grid {
    grid-template-columns: 1fr 1fr 1fr;
  }

  .viz-row {
    grid-template-columns: 1fr 1fr;
    align-items: start;
  }

  .details-grid {
    grid-template-columns: 1fr 1fr;
  }

  .tag-data-layout {
    grid-template-columns: 320px 1fr;
    align-items: start;
  }

  .tag-data-list {
    position: sticky;
    top: 1rem;
  }
}
