:root {
  --bg: #0f1115;
  --surface: #171a21;
  --surface-2: #1f242d;
  --border: #2a303b;
  --text: #e6e8ec;
  --text-dim: #9aa3b2;
  --accent: #7cc4ff;
  --accent-2: #4d9fe0;
  --danger: #ff6b6b;
  --ok: #6bd495;
  --radius: 12px;
  --radius-lg: 16px;
  --shadow: 0 4px 16px rgba(0, 0, 0, 0.35);
  --tap: 48px;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  font-size: 17px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

body {
  padding-bottom: env(safe-area-inset-bottom);
}

h1,
h2,
h3 {
  margin: 0 0 0.5em;
  font-weight: 600;
}

.accent {
  color: var(--accent);
}

/* Flash messages */
.flash {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  background: var(--danger);
  color: white;
  padding: 12px 16px;
  z-index: 100;
  text-align: center;
  font-weight: 500;
}

/* Login page */
.login-wrap {
  max-width: 380px;
  margin: 15vh auto 0;
  padding: 24px;
  text-align: center;
}

.login-wrap h1 {
  font-size: 32px;
  margin-bottom: 32px;
}

.login-form {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.login-form input {
  height: var(--tap);
  padding: 0 16px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 17px;
}

.login-form input:focus {
  outline: none;
  border-color: var(--accent);
}

.login-form button {
  height: var(--tap);
  border: none;
  border-radius: var(--radius);
  background: var(--accent);
  color: #001726;
  font-size: 17px;
  font-weight: 600;
  cursor: pointer;
}

/* Top bar */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  position: sticky;
  top: 0;
  z-index: 10;
}

.topbar h1 {
  font-size: 20px;
  margin: 0;
}

.btn-link {
  color: var(--text-dim);
  text-decoration: none;
  font-size: 14px;
  padding: 8px;
}

/* Tabs */
.tabs {
  display: flex;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 49px;
  z-index: 9;
}

.tab {
  flex: 1;
  height: var(--tap);
  background: none;
  border: none;
  border-bottom: 2px solid transparent;
  color: var(--text-dim);
  font-size: 15px;
  font-weight: 500;
  cursor: pointer;
}

.tab.active {
  color: var(--accent);
  border-bottom-color: var(--accent);
}

.badge {
  display: inline-block;
  min-width: 20px;
  padding: 1px 6px;
  border-radius: 10px;
  background: var(--surface-2);
  color: var(--text-dim);
  font-size: 12px;
  font-weight: 600;
  vertical-align: middle;
}

.tab.active .badge {
  background: var(--accent-2);
  color: white;
}

/* Panels */
main {
  padding: 16px;
}

.tab-panel {
  display: none;
}

.tab-panel.active {
  display: block;
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: var(--tap);
  padding: 0 20px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 16px;
  font-weight: 500;
  cursor: pointer;
  text-decoration: none;
  user-select: none;
}

.btn.big {
  min-height: 64px;
  font-size: 18px;
  width: 100%;
  border-radius: var(--radius-lg);
}

.btn.primary {
  background: var(--accent);
  color: #001726;
  border-color: var(--accent);
}

.btn.ghost {
  background: transparent;
}

.btn:active {
  transform: scale(0.98);
}

.actions {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

/* Preview */
.preview {
  margin-top: 16px;
  padding: 12px;
  border-radius: var(--radius-lg);
  background: var(--surface);
  border: 1px solid var(--border);
}

.preview.hidden {
  display: none;
}

#preview-img {
  width: 100%;
  max-height: 60vh;
  object-fit: contain;
  border-radius: var(--radius);
  background: var(--surface-2);
}

.preview-actions {
  display: flex;
  gap: 12px;
  margin-top: 12px;
}

.preview-actions .btn {
  flex: 1;
}

/* Status */
.status {
  margin-top: 16px;
  padding: 12px;
  border-radius: var(--radius);
  font-size: 14px;
  color: var(--text-dim);
  min-height: 20px;
}

.status.ok {
  background: rgba(107, 212, 149, 0.12);
  color: var(--ok);
}

.status.err {
  background: rgba(255, 107, 107, 0.12);
  color: var(--danger);
}

/* Breadcrumb navigation */
.files-breadcrumb {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
  padding: 8px 12px;
  margin-bottom: 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 13px;
}

.files-breadcrumb a {
  color: var(--accent);
  text-decoration: none;
}

.files-breadcrumb a:hover {
  text-decoration: underline;
}

.files-breadcrumb .crumb-sep {
  color: var(--text-dim);
}

.files-breadcrumb .crumb-current {
  color: var(--text);
  font-weight: 500;
}

/* Folder list items */
.folder-item .file-main {
  font-weight: 500;
}

.thumb-folder {
  background: rgba(124, 196, 255, 0.12);
  color: var(--accent);
}

.folder-check-spacer {
  width: 18px;
  padding: 0 6px 0 10px;
}

/* Files — upload zone */
.files-upload-zone {
  border: 2px dashed var(--border);
  border-radius: var(--radius-lg);
  padding: 16px;
  text-align: center;
  background: var(--surface);
  transition: border-color 0.18s, background 0.18s;
  margin-bottom: 14px;
}

.files-upload-zone.drag-over {
  border-color: var(--accent);
  background: rgba(124, 196, 255, 0.08);
}

.files-upload-btn {
  width: 100%;
  margin-bottom: 8px;
}

.files-drop-hint {
  color: var(--text-dim);
  font-size: 13px;
  padding: 4px 0;
}

.files-upload-status {
  margin-top: 8px;
  font-size: 13px;
  color: var(--text-dim);
  min-height: 18px;
}

.files-upload-status.ok {
  color: var(--ok);
}

.files-upload-status.err {
  color: var(--danger);
}

/* Bulk action toolbar */
.files-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 8px 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin-bottom: 10px;
}

.files-select-all {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  color: var(--text-dim);
  cursor: pointer;
  user-select: none;
}

.files-select-all input[type="checkbox"] {
  width: 18px;
  height: 18px;
  cursor: pointer;
  accent-color: var(--accent);
}

.btn.danger,
.btn.ghost.danger {
  color: var(--danger);
  border-color: rgba(255, 107, 107, 0.4);
}

.btn.ghost.danger:not(:disabled):hover {
  background: rgba(255, 107, 107, 0.12);
}

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.files-bulk-del {
  font-size: 14px;
  padding: 0 14px;
  min-height: 38px;
}

/* Files */
.file-list {
  list-style: none;
  padding: 0;
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.file-item {
  display: flex;
  align-items: stretch;
  gap: 8px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}

.file-item:has(.file-check-input:checked) {
  border-color: var(--accent);
  background: rgba(124, 196, 255, 0.06);
}

.file-check {
  display: flex;
  align-items: center;
  padding: 0 6px 0 10px;
  cursor: pointer;
}

.file-check-input {
  width: 18px;
  height: 18px;
  cursor: pointer;
  accent-color: var(--accent);
}

.file-main {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px;
  text-decoration: none;
  color: var(--text);
  min-width: 0;
}

.thumb {
  width: 56px;
  height: 56px;
  border-radius: 8px;
  object-fit: cover;
  flex-shrink: 0;
  background: var(--surface-2);
}

.thumb-doc {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
}

.file-meta {
  min-width: 0;
  flex: 1;
}

.file-name {
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.file-sub {
  font-size: 12px;
  color: var(--text-dim);
}

.file-del {
  display: flex;
  margin: 0;
}

.file-del button {
  width: var(--tap);
  border: none;
  background: transparent;
  color: var(--text-dim);
  font-size: 18px;
  cursor: pointer;
}

.file-del button:active {
  background: rgba(255, 107, 107, 0.12);
  color: var(--danger);
}

.empty {
  text-align: center;
  color: var(--text-dim);
  padding: 40px 20px;
}

/* Hover preview popup (desktop only) */
.file-hover-preview {
  position: fixed;
  z-index: 1000;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 8px;
  display: none;
  pointer-events: none;
  max-width: 420px;
  max-height: 480px;
}

.file-hover-preview.visible {
  display: block;
}

.file-hover-preview .hp-img {
  display: block;
  max-width: 400px;
  max-height: 460px;
  width: auto;
  height: auto;
  border-radius: 6px;
  background: var(--surface-2);
}

.file-hover-preview .hp-pdf {
  width: 400px;
  height: 460px;
  border: none;
  border-radius: 6px;
  background: white;
}

.file-hover-preview .hp-info {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding: 18px 24px;
  min-width: 180px;
}

.hp-info-icon {
  font-size: 48px;
}

.hp-info-name {
  font-size: 13px;
  text-align: center;
  word-break: break-all;
  color: var(--text);
  max-width: 280px;
}

.hp-info-meta {
  font-size: 12px;
  color: var(--text-dim);
}

/* 3D preview */
.file-hover-preview .hp-3d-img {
  background: linear-gradient(180deg, #f0f0f0 0%, #d8d8d8 100%);
}

.hp-3d-loading {
  width: 320px;
  height: 360px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  color: var(--text-dim);
  font-size: 13px;
}

.hp-spinner {
  width: 32px;
  height: 32px;
  border: 3px solid var(--surface-2);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: hp-spin 0.8s linear infinite;
}

@keyframes hp-spin {
  to { transform: rotate(360deg); }
}

/* Hide hover preview on touch devices (no real hover) */
@media (hover: none) {
  .file-hover-preview {
    display: none !important;
  }
}

/* Chess placeholder */
.placeholder {
  padding: 24px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  text-align: center;
}

.placeholder h2 {
  color: var(--accent);
}

.placeholder .hint {
  color: var(--text-dim);
  font-size: 14px;
  margin-top: 16px;
}

/* ─────────────────────────────────
   NFC / Chess tab
   ───────────────────────────────── */

.nfc-hw-status {
  padding: 8px 12px;
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text-dim);
  font-size: 13px;
  margin-bottom: 14px;
  text-align: center;
}

.nfc-hw-status.ok {
  background: rgba(107, 212, 149, 0.12);
  color: var(--ok);
}

.nfc-hw-status.err {
  background: rgba(255, 107, 107, 0.12);
  color: var(--danger);
}

.nfc-card {
  background: var(--surface);
  border: 2px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 20px;
  margin-bottom: 20px;
  min-height: 120px;
  transition: border-color 0.25s, background 0.25s;
}

.nfc-card.nfc-known {
  border-color: var(--ok);
  background: rgba(107, 212, 149, 0.06);
}

.nfc-card.nfc-unknown {
  border-color: var(--accent);
  background: rgba(124, 196, 255, 0.06);
}

.nfc-idle-msg {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
  color: var(--text-dim);
  padding: 12px 0;
}

.nfc-idle-msg.hidden {
  display: none;
}

.nfc-pulse-dot {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 0 0 rgba(124, 196, 255, 0.6);
  animation: nfc-pulse 1.6s infinite;
}

@keyframes nfc-pulse {
  0% {
    box-shadow: 0 0 0 0 rgba(124, 196, 255, 0.6);
  }
  70% {
    box-shadow: 0 0 0 14px rgba(124, 196, 255, 0);
  }
  100% {
    box-shadow: 0 0 0 0 rgba(124, 196, 255, 0);
  }
}

.nfc-detected.hidden {
  display: none;
}

.nfc-uid-row {
  display: flex;
  align-items: baseline;
  gap: 12px;
  margin-bottom: 12px;
  flex-wrap: wrap;
}

.nfc-label {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--text-dim);
}

.nfc-uid {
  font-family: "SF Mono", "Consolas", monospace;
  font-size: 15px;
  color: var(--text);
  word-break: break-all;
}

.nfc-known-info {
  display: flex;
  align-items: center;
  gap: 14px;
}

.nfc-known-info.hidden {
  display: none;
}

.nfc-name {
  font-size: 22px;
  font-weight: 600;
  color: var(--text);
  flex: 1;
}

.nfc-color-chip {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  flex-shrink: 0;
  border: 2px solid rgba(255, 255, 255, 0.2);
}

.nfc-register-form {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-top: 8px;
}

.nfc-register-form.hidden {
  display: none;
}

.nfc-register-form input[type="text"] {
  width: 100%;
  height: var(--tap);
  padding: 0 14px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  font-size: 16px;
}

.nfc-form-actions {
  display: flex;
  gap: 10px;
}

.nfc-form-actions .btn {
  flex: 1;
}

.nfc-known-actions {
  display: flex;
  justify-content: center;
  margin-top: 12px;
}

.nfc-known-actions.hidden {
  display: none;
}

.nfc-known-actions .btn {
  min-width: 120px;
}

.nfc-register-form input[type="text"]:focus {
  outline: none;
  border-color: var(--accent);
}

.nfc-color-choice {
  display: flex;
  gap: 8px;
}

.nfc-color-opt {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  height: var(--tap);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 14px;
  padding: 0 8px;
  transition: border-color 0.15s, background 0.15s;
}

.nfc-color-opt input {
  display: none;
}

.nfc-color-opt:has(input:checked) {
  border-color: var(--accent);
  background: rgba(124, 196, 255, 0.12);
}

/* Color dots */
.dot {
  display: inline-block;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  border: 1px solid rgba(255, 255, 255, 0.2);
  flex-shrink: 0;
}

.dot-yellow {
  background: #f1c40f;
  box-shadow: 0 0 8px rgba(241, 196, 15, 0.5);
}

.dot-green {
  background: #27ae60;
  box-shadow: 0 0 8px rgba(39, 174, 96, 0.5);
}

.dot-red {
  background: #e74c3c;
  box-shadow: 0 0 8px rgba(231, 76, 60, 0.5);
}

.nfc-color-chip.dot {
  width: 32px;
  height: 32px;
  border-width: 2px;
}

/* Tags list */
.nfc-tags-section {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 16px;
  margin-bottom: 20px;
}

.nfc-tags-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}

.nfc-tags-header h3 {
  margin: 0;
  font-size: 15px;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 1px;
}

.nfc-tags-list {
  list-style: none;
  padding: 0;
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.nfc-tags-list .empty {
  padding: 20px 10px;
  color: var(--text-dim);
  text-align: center;
  font-size: 14px;
}

.nfc-tag-item {
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--surface-2);
  border-radius: var(--radius);
  padding: 10px 12px;
}

.nfc-tag-color {
  width: 16px;
  height: 16px;
  flex-shrink: 0;
}

.nfc-tag-meta {
  flex: 1;
  min-width: 0;
}

.nfc-tag-name {
  font-weight: 500;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.nfc-tag-uid {
  font-family: "SF Mono", "Consolas", monospace;
  font-size: 11px;
  color: var(--text-dim);
}

.nfc-tag-del {
  width: 36px;
  height: 36px;
  border: none;
  background: transparent;
  color: var(--text-dim);
  font-size: 16px;
  cursor: pointer;
  border-radius: 50%;
}

.nfc-tag-del:active {
  background: rgba(255, 107, 107, 0.12);
  color: var(--danger);
}

/* LED test buttons */
.nfc-led-test {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 16px;
}

.nfc-led-test .nfc-label {
  display: block;
  margin-bottom: 10px;
}

.nfc-led-buttons {
  display: flex;
  gap: 8px;
}

.led-btn {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 0 8px;
  font-size: 14px;
}

.nfc-reset-btn {
  width: 100%;
  margin-top: 10px;
  font-size: 14px;
}

/* Magnet section */
.magnet-section {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 16px;
  margin-top: 14px;
  text-align: center;
}

.magnet-section .nfc-label {
  display: block;
  margin-bottom: 12px;
}

.magnet-btn {
  width: 100%;
  min-height: 72px;
  font-size: 19px;
  font-weight: 600;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  background: var(--surface-2);
  border: 2px solid var(--border);
  transition: background 0.2s, border-color 0.2s, color 0.2s;
}

.magnet-btn .magnet-icon {
  font-size: 28px;
  filter: grayscale(0.3);
  transition: filter 0.2s;
}

.magnet-btn.active {
  background: rgba(255, 107, 107, 0.18);
  border-color: var(--danger);
  color: var(--danger);
  animation: magnet-glow 1.2s ease-in-out infinite alternate;
}

.magnet-btn.active .magnet-icon {
  filter: none;
}

.magnet-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

@keyframes magnet-glow {
  from {
    box-shadow: 0 0 0 0 rgba(255, 107, 107, 0.3);
  }
  to {
    box-shadow: 0 0 18px 4px rgba(255, 107, 107, 0.35);
  }
}

.magnet-timer {
  margin-top: 10px;
  font-size: 13px;
  color: var(--text-dim);
  min-height: 18px;
}

/* ─── Motors tab ─────────────────────────────────────── */
.motors-header { display: flex; justify-content: space-between; align-items: center;
  margin: 12px 4px; gap: 12px; }
#motor-state.ok { color: #4ade80; font-weight: 600; }
#motor-state.err { color: #f87171; font-weight: 600; }

.motor-card { background: rgba(255,255,255,0.04); border: 1px solid rgba(255,255,255,0.1);
  border-radius: 14px; padding: 16px; margin-bottom: 14px; }
.motor-title { font-size: 1.2rem; font-weight: 700; margin-bottom: 10px; }

.motor-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 12px; }
.motor-row label { display: flex; flex-direction: column; font-size: 0.85rem; gap: 4px; }
.motor-row input { padding: 10px; border-radius: 8px; border: 1px solid rgba(255,255,255,0.15);
  background: rgba(0,0,0,0.3); color: inherit; font-size: 1rem; }

.motor-jog { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 10px; }
.btn.jog { padding: 16px; font-size: 1.1rem; font-weight: 600;
  background: linear-gradient(135deg, #3b82f6, #2563eb); color: white; border: none;
  border-radius: 10px; cursor: pointer; }
.btn.jog:active { transform: scale(0.97); }
.btn.jog-neg { background: linear-gradient(135deg, #6b7280, #4b5563); }

.motor-quick { display: flex; align-items: center; gap: 6px; flex-wrap: wrap;
  font-size: 0.85rem; }
.motor-quick .hint { opacity: 0.6; margin-right: 4px; }
.btn-chip { padding: 6px 12px; border-radius: 16px; border: 1px solid rgba(255,255,255,0.2);
  background: transparent; color: inherit; cursor: pointer; font-size: 0.85rem; }
.btn-chip:hover { background: rgba(255,255,255,0.08); }

.motor-stop { width: 100%; padding: 14px; background: #dc2626; color: white; border: none;
  border-radius: 10px; font-weight: 700; font-size: 1rem; margin-top: 4px; cursor: pointer; }
.motor-stop:active { background: #991b1b; }

.motor-log { margin-top: 14px; max-height: 200px; overflow-y: auto;
  font-family: monospace; font-size: 0.78rem; background: rgba(0,0,0,0.3);
  border-radius: 8px; padding: 8px; }
.motor-log .log-line { padding: 2px 4px; border-bottom: 1px dotted rgba(255,255,255,0.05); }
.motor-log .log-line.ok { color: #4ade80; }
.motor-log .log-line.err { color: #f87171; }
