:root {
  --bg: #0f172a;
  --bg-soft: #111c34;
  --card: #ffffff;
  --card-border: #e6e9f0;
  --text: #0f172a;
  --text-muted: #64748b;
  --text-inverse: #f8fafc;
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --accent: #06b6d4;
  --success: #16a34a;
  --danger: #dc2626;
  --radius-lg: 20px;
  --radius-md: 14px;
  --shadow: 0 10px 30px rgba(15, 23, 42, 0.08);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --card: #16213d;
    --card-border: #26314f;
    --text: #eef2ff;
    --text-muted: #94a3b8;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overscroll-behavior-y: contain;
}

body {
  font-family: -apple-system, "Segoe UI", Roboto, Inter, system-ui, sans-serif;
  background:
    radial-gradient(1200px 600px at 100% -10%, #1d3a8f22, transparent),
    linear-gradient(180deg, var(--bg) 0%, var(--bg-soft) 320px, #f1f5f9 320px);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
}

@media (prefers-color-scheme: dark) {
  body {
    background:
      radial-gradient(1200px 600px at 100% -10%, #1d3a8f33, transparent),
      linear-gradient(180deg, var(--bg) 0%, var(--bg-soft) 320px, #0b1120 320px);
  }
}

.app {
  max-width: 520px;
  margin: 0 auto;
  min-height: 100%;
  padding-bottom: 48px;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 28px 20px 22px;
  color: var(--text-inverse);
}

.topbar-brand {
  display: flex;
  align-items: center;
  gap: 14px;
}

.topbar-logo {
  width: 40px;
  height: 40px;
  border-radius: 12px;
  box-shadow: 0 4px 14px rgba(0,0,0,0.35);
}

.topbar h1 {
  font-size: 19px;
  margin: 0;
  font-weight: 700;
  letter-spacing: -0.01em;
}

.topbar-sub {
  margin: 2px 0 0;
  font-size: 13px;
  color: #a8b4d0;
}

.user-status.user-ok {
  color: #86efac;
}
.user-status.user-missing {
  color: #fca5a5;
}

.conn-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--success);
  box-shadow: 0 0 0 4px rgba(22,163,74,0.18);
  flex-shrink: 0;
}
.conn-dot.offline {
  background: var(--danger);
  box-shadow: 0 0 0 4px rgba(220,38,38,0.18);
}

.content {
  padding: 0 16px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.card {
  background: var(--card);
  border: 1px solid var(--card-border);
  border-radius: var(--radius-lg);
  padding: 18px;
  box-shadow: var(--shadow);
}

.card-row {
  display: flex;
  align-items: flex-start;
  gap: 14px;
}

.card-icon {
  width: 42px;
  height: 42px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
.card-icon svg { width: 22px; height: 22px; }
.sheet-icon { background: #dbeafe; color: var(--primary); }
.device-icon { background: #e0f2fe; color: #0369a1; }
@media (prefers-color-scheme: dark) {
  .sheet-icon { background: #1e3a8a44; color: #93c5fd; }
  .device-icon { background: #0c4a6e44; color: #7dd3fc; }
}

.card-text h2 {
  margin: 0 0 4px;
  font-size: 16px;
  font-weight: 700;
}

.muted { color: var(--text-muted); font-size: 13.5px; margin: 0; line-height: 1.4; }

.card-actions {
  display: flex;
  gap: 10px;
  margin-top: 14px;
}

.btn {
  border: none;
  border-radius: var(--radius-md);
  padding: 12px 16px;
  font-size: 14.5px;
  font-weight: 600;
  cursor: pointer;
  transition: transform .08s ease, opacity .15s ease;
}
.btn:active { transform: scale(0.97); }
.btn-primary {
  background: linear-gradient(135deg, var(--primary), var(--primary-dark));
  color: white;
  flex: 1;
}
.btn-ghost {
  background: transparent;
  color: var(--text-muted);
  border: 1px solid var(--card-border);
}
.btn-small {
  background: var(--primary);
  color: white;
  padding: 10px 16px;
}
.hidden { display: none !important; }

.device-input-row {
  display: flex;
  gap: 10px;
  margin-top: 14px;
}
.device-input-row input {
  flex: 1;
  min-width: 0;
}

input[type="text"], input[type="url"] {
  border: 1px solid var(--card-border);
  border-radius: var(--radius-md);
  padding: 12px 14px;
  font-size: 14.5px;
  background: transparent;
  color: var(--text);
  width: 100%;
}
input[type="text"]:focus, input[type="url"]:focus {
  outline: 2px solid var(--primary);
  outline-offset: 1px;
}

.scan-section {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  padding: 10px 0 4px;
}

.btn-scan {
  width: 100%;
  max-width: 400px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 20px;
  border: none;
  border-radius: 999px;
  font-size: 16.5px;
  font-weight: 700;
  color: white;
  background: linear-gradient(135deg, var(--accent), var(--primary));
  box-shadow: 0 12px 24px rgba(37, 99, 235, 0.28);
  cursor: pointer;
  transition: transform .08s ease, opacity .2s ease, filter .2s ease;
}
.btn-scan:active { transform: scale(0.98); }
.btn-scan:disabled {
  background: #94a3b8;
  box-shadow: none;
  cursor: not-allowed;
  opacity: 0.7;
}
.scan-icon { width: 24px; height: 24px; }

.scan-hint {
  font-size: 12.5px;
  color: var(--text-muted);
  margin: 0;
  text-align: center;
}

.history {
  margin-top: 4px;
}
.history-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 4px 8px;
}
.history-header h2 {
  font-size: 14px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin: 0;
  font-weight: 700;
}
.pending-badge {
  font-size: 12px;
  font-weight: 700;
  color: #b45309;
  background: #fef3c7;
  padding: 3px 9px;
  border-radius: 999px;
}
@media (prefers-color-scheme: dark) {
  .pending-badge { background: #78350f44; color: #fcd34d; }
}

.history-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.history-list li {
  background: var(--card);
  border: 1px solid var(--card-border);
  border-radius: var(--radius-md);
  padding: 12px 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
}
.history-empty {
  justify-content: center !important;
  color: var(--text-muted);
  font-size: 13.5px !important;
}
.history-tombo { font-weight: 700; letter-spacing: 0.02em; }
.history-meta { color: var(--text-muted); font-size: 12px; text-align: right; }
.history-status { font-size: 11px; font-weight: 700; padding: 2px 8px; border-radius: 999px; margin-left: 8px; }
.status-ok { background: #dcfce7; color: #15803d; }
.status-pending { background: #fef3c7; color: #b45309; }
.status-error { background: #fee2e2; color: #b91c1c; }
@media (prefers-color-scheme: dark) {
  .status-ok { background: #14532d55; color: #86efac; }
  .status-pending { background: #78350f55; color: #fcd34d; }
  .status-error { background: #7f1d1d55; color: #fca5a5; }
}

/* Scan overlay */
.scan-overlay {
  position: fixed;
  inset: 0;
  background: #000;
  z-index: 50;
  display: flex;
  flex-direction: column;
}
.scan-overlay video {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.scan-overlay-top {
  position: relative;
  z-index: 2;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px;
  background: linear-gradient(180deg, rgba(0,0,0,0.6), transparent);
}
.btn-icon {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  border: none;
  background: rgba(255,255,255,0.15);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}
.btn-icon svg { width: 20px; height: 20px; }
.scan-count {
  color: white;
  font-size: 13px;
  font-weight: 600;
  background: rgba(255,255,255,0.15);
  padding: 6px 12px;
  border-radius: 999px;
}
.scan-frame {
  position: absolute;
  left: 10%;
  right: 10%;
  top: 32%;
  bottom: 32%;
  border: 3px solid rgba(255,255,255,0.85);
  border-radius: 20px;
  box-shadow: 0 0 0 2000px rgba(0,0,0,0.35);
  z-index: 1;
  transition: border-color .15s ease;
}
.scan-frame.flash {
  border-color: #22c55e;
}
.scan-feedback {
  position: absolute;
  left: 50%;
  bottom: 48px;
  z-index: 2;
  transform: translateX(-50%) translateY(8px);
  display: flex;
  align-items: center;
  gap: 8px;
  background: rgba(15, 23, 42, 0.92);
  color: white;
  font-weight: 700;
  font-size: 16px;
  padding: 12px 22px;
  border-radius: 999px;
  box-shadow: 0 10px 28px rgba(0,0,0,0.4);
  pointer-events: none;
  opacity: 0;
  transition: opacity .15s ease, transform .15s ease;
  white-space: nowrap;
}
.scan-feedback.visible {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}
.scan-feedback svg {
  width: 20px;
  height: 20px;
  color: #4ade80;
  flex-shrink: 0;
}

/* Modal */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 60;
  padding: 20px;
}
.modal {
  background: var(--card);
  border-radius: var(--radius-lg);
  padding: 22px;
  width: 100%;
  max-width: 420px;
  box-shadow: 0 20px 60px rgba(0,0,0,0.3);
}
.modal h2 { margin: 0 0 6px; font-size: 18px; }
.modal input { margin: 12px 0 6px; }
.modal-error {
  color: var(--danger);
  font-size: 13px;
  margin: 4px 0 0;
}
.modal-actions {
  display: flex;
  gap: 10px;
  margin-top: 16px;
}
.modal-actions .btn { flex: 1; }

.toast {
  position: fixed;
  left: 50%;
  bottom: 28px;
  transform: translateX(-50%);
  background: #0f172a;
  color: white;
  padding: 12px 20px;
  border-radius: 999px;
  font-size: 14px;
  font-weight: 600;
  box-shadow: 0 10px 30px rgba(0,0,0,0.3);
  z-index: 80;
  max-width: 90vw;
  text-align: center;
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.4; }
}
.pulsing { animation: pulse 1.4s ease-in-out infinite; }

.app-version {
  text-align: center;
  font-size: 11.5px;
  color: var(--text-muted);
  opacity: 0.7;
  margin: 4px 0 0;
}
