:root {
  color-scheme: light dark;
  font-family: system-ui, sans-serif;

  /* Light theme (default) */
  --bg: #f8fafc;
  --surface: #ffffff;
  --surface-2: #f1f5f9;
  --border: #cbd5e1;
  --border-soft: #e2e8f0;
  --text: #0f172a;
  --text-muted: #64748b;
  --primary: #0f172a;
  --primary-hover: #1e293b;
  --primary-text: #f8fafc;
  --danger: #dc2626;
  --danger-hover: #b91c1c;
  --danger-text: #ffffff;
  --success: #15803d;
  --accent: #0c4a6e;
  --field-bg: #ffffff;
  --field-border: #cbd5e1;
  --shadow: rgba(15, 23, 42, 0.08);
  --radius: 10px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1120;
    --surface: #111827;
    --surface-2: #1e293b;
    --border: #334155;
    --border-soft: #1e293b;
    --text: #e2e8f0;
    --text-muted: #94a3b8;
    --primary: #3b82f6;
    --primary-hover: #2563eb;
    --primary-text: #f8fafc;
    --danger: #ef4444;
    --danger-hover: #dc2626;
    --danger-text: #ffffff;
    --success: #4ade80;
    --accent: #7dd3fc;
    --field-bg: #0f172a;
    --field-border: #334155;
    --shadow: rgba(0, 0, 0, 0.45);
  }
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

header,
main {
  max-width: 900px;
  margin: 0 auto;
  padding: 1rem;
  width: 100%;
  box-sizing: border-box;
}

main {
  flex: 1;
}

header {
  padding-bottom: 0;
}

.header-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.icon-btn {
  background: none;
  border: none;
  font-size: 1.2rem;
  color: inherit;
  padding: 0.3rem 0.5rem;
  cursor: pointer;
  opacity: 0.7;
}

.icon-btn:hover {
  opacity: 1;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 1rem;
  margin-bottom: 1rem;
}

.row {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  margin-bottom: 0.75rem;
}

.row.inline {
  flex-direction: row;
  align-items: center;
  gap: 0.5rem;
}

.row.inline.wrap {
  flex-wrap: wrap;
}

input,
textarea,
select,
button {
  font: inherit;
  padding: 0.55rem 0.7rem;
}

input,
textarea,
select {
  background: var(--field-bg);
  color: var(--text);
  border: 1px solid var(--field-border);
  border-radius: 8px;
  box-sizing: border-box;
}

input::placeholder,
textarea::placeholder {
  color: var(--text-muted);
}

input:focus-visible,
textarea:focus-visible,
select:focus-visible,
button:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
}

/* Modern default button */
button {
  cursor: pointer;
  background: var(--primary);
  color: var(--primary-text);
  border: 1px solid transparent;
  border-radius: 999px;
  padding: 0.55rem 1.1rem;
  font-weight: 600;
  transition: background-color 0.15s ease, transform 0.05s ease, box-shadow 0.15s ease;
}

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

button:active {
  transform: translateY(1px);
}

button.danger {
  background: var(--danger);
  color: var(--danger-text);
  border: 1px solid transparent;
  border-radius: 999px;
}

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

.tabs {
  display: flex;
  gap: 0.5rem;
  overflow-x: auto;
  margin-bottom: 1rem;
  padding-bottom: 0.25rem;
}

.tab {
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--text);
  white-space: nowrap;
  font-weight: 600;
}

.tab:hover {
  background: var(--border-soft);
}

.tab.active {
  background: var(--primary);
  border-color: var(--primary);
  color: var(--primary-text);
}

.tab.active:hover {
  background: var(--primary-hover);
}

.tab-panel {
  margin-bottom: 1rem;
}

.camera-shell {
  position: relative;
  display: grid;
  place-items: center;
  min-height: 80px;
  max-height: 120px;
  margin-bottom: 1rem;
  overflow: hidden;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #0f172a;
}

.camera-shell--compact {
  min-height: 80px;
  max-height: 120px;
}

#quaggaContainer {
  width: 100%;
  height: 120px;
  position: relative;
  overflow: hidden;
}

#quaggaContainer video,
#quaggaContainer canvas {
  width: 100%;
  height: 120px;
  object-fit: cover;
}

#quaggaContainer canvas.drawingBuffer {
  position: absolute;
  top: 0;
  left: 0;
}

.camera-placeholder {
  padding: 1rem;
  text-align: center;
  color: #e2e8f0;
  font-size: 0.85rem;
}

.camera-placeholder--tappable {
  cursor: pointer;
  user-select: none;
}

.camera-placeholder--tappable:hover {
  color: #fff;
}

.camera-placeholder--tappable i {
  font-size: 1.5rem;
  margin-bottom: 0.25rem;
}

.nested-card {
  margin-top: 1rem;
  padding-top: 1rem;
  border-top: 1px solid var(--border);
}

.hidden {
  display: none !important;
}

.message {
  min-height: 1.2rem;
  color: var(--accent);
}

.message--error {
  color: var(--danger);
}

.message--success {
  color: var(--success);
}

.account-panel {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
}

.account-panel .message {
  display: block;
}

.account-actions {
  margin-bottom: 0;
}

ul {
  padding-left: 1.2rem;
}

#schemaList {
  list-style: none;
  padding-left: 0;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

#schemaList li {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.4rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--field-bg);
}

.schema-remove-btn {
  flex: none;
  padding: 0.35rem 0.8rem;
  font-size: 0.9rem;
}

table {
  width: 100%;
  border-collapse: collapse;
}

th,
td {
  border-bottom: 1px solid var(--border-soft);
  padding: 0.5rem;
  text-align: left;
}

.small {
  font-size: 0.9rem;
  opacity: 0.8;
}

@media (max-width: 640px) {
  .row.inline {
    flex-direction: column;
    align-items: stretch;
  }
}

.piece-row {
  cursor: pointer;
}

.piece-row:hover {
  background: var(--surface-2);
}

.piece-fields {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 0.25rem 1rem;
  margin: 0;
}

.piece-fields dt {
  font-weight: 600;
  text-transform: capitalize;
}

.piece-fields dd {
  margin: 0;
}

.piece-photo {
  max-width: 120px;
  max-height: 120px;
  margin-top: 0.5rem;
  border-radius: 4px;
}

.photo-error {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 2.5rem;
  padding: 0.35rem;
  color: var(--danger);
  font-size: 0.8rem;
  text-align: center;
}

.datamatrix-output {
  margin-top: 1rem;
}

.datamatrix-preview {
  display: flex;
  justify-content: center;
  padding: 1rem;
  margin-bottom: 1rem;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 8px;
}

.datamatrix-preview svg {
  max-width: 200px;
  max-height: 200px;
}

.reconnect-pulse {
  animation: pulse-highlight 1.5s ease-in-out 3;
  background-color: #fbbf24;
  color: #1e293b;
}

@keyframes pulse-highlight {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.6; }
}

.app-footer {
  max-width: 900px;
  margin: 0 auto;
  padding: 0.75rem 1rem;
  border-top: 1px solid var(--border);
  color: var(--text-muted);
  width: 100%;
  box-sizing: border-box;
  position: sticky;
  bottom: 0;
  background: var(--bg);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem 1rem;
  flex-wrap: wrap;
}

.footer-drive-btn {
  background: none;
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 0.4rem 0.8rem;
  font-size: 0.85rem;
  cursor: pointer;
  color: inherit;
  font-weight: 600;
}

.footer-drive-btn:hover {
  background: var(--surface-2);
}

.deploy-status {
  display: inline-flex;
  align-items: center;
  justify-content: flex-end;
  flex-wrap: wrap;
  gap: 0.35rem;
  margin-left: auto;
}

.deploy-status__link {
  color: inherit;
  font-weight: 600;
  text-decoration: underline;
  text-decoration-thickness: 1px;
  text-underline-offset: 0.15em;
}

.deploy-status__time {
  white-space: nowrap;
}

.deploy-status__badge {
  display: inline-flex;
  align-items: center;
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 0.15rem 0.45rem;
  background: var(--surface);
  color: var(--text-muted);
  font-size: 0.75rem;
  font-weight: 700;
  line-height: 1.2;
}

.deploy-status__badge--latest {
  border-color: var(--success);
  color: var(--success);
}

.deploy-status__badge--stale {
  border-color: #f59e0b;
  color: #b45309;
}

@media (prefers-color-scheme: dark) {
  .deploy-status__badge--stale {
    color: #fbbf24;
  }
}

@media (max-width: 640px) {
  .app-footer {
    justify-content: center;
  }

  .deploy-status {
    justify-content: center;
    margin-left: 0;
  }
}

.piece-thumb {
  width: 32px;
  height: 32px;
  object-fit: cover;
  border-radius: 4px;
  vertical-align: middle;
}

/* Tabs */
.tab {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.45rem 0.9rem;
}

.tab-label {
  font-size: 0.9rem;
}

@media (max-width: 480px) {
  .tab-label {
    display: none;
  }
}

/* Pieces toolbar */
.pieces-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
}

.pieces-header h2 {
  margin: 0;
}

.pieces-header-actions {
  display: inline-flex;
  align-items: center;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 0.5rem;
}

.pieces-export-btn {
  padding: 0.4rem 0.75rem;
}

.view-toggle {
  display: inline-flex;
  border: 1px solid var(--border);
  border-radius: 999px;
  overflow: hidden;
}

.view-btn {
  background: var(--surface);
  border: none;
  color: var(--text-muted);
  padding: 0.4rem 0.7rem;
  border-radius: 0;
}

.view-btn:hover {
  background: var(--surface-2);
}

.view-btn.active {
  background: var(--primary);
  color: var(--primary-text);
}

.view-btn.active:hover {
  background: var(--primary-hover);
}

.pieces-search {
  position: relative;
  margin-top: 0.75rem;
  flex-direction: row;
  align-items: center;
}

.pieces-search .search-icon {
  position: absolute;
  left: 0.6rem;
  color: var(--text-muted);
  pointer-events: none;
}

.pieces-search input {
  width: 100%;
  box-sizing: border-box;
  padding-left: 2rem;
  border: 1px solid var(--field-border);
  border-radius: 8px;
}

/* Gallery */
.gallery {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
  gap: 0.75rem;
}

.gallery-card {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  padding: 0;
  background: var(--surface);
  border: 1px solid var(--border-soft);
  border-radius: 10px;
  overflow: hidden;
  cursor: pointer;
  text-align: left;
}

.gallery-card:hover {
  border-color: var(--border);
  box-shadow: 0 2px 8px var(--shadow);
}

.gallery-thumb {
  width: 100%;
  aspect-ratio: 1 / 1;
  background: var(--surface-2);
  display: grid;
  place-items: center;
  overflow: hidden;
}

.gallery-thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.gallery-thumb--empty {
  color: var(--border);
  font-size: 1.8rem;
}

.gallery-caption {
  display: block;
  padding: 0 0.5rem 0.15rem;
  font-size: 0.85rem;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.gallery-meta {
  display: block;
  padding: 0 0.5rem 0.5rem;
  font-size: 0.72rem;
  color: var(--text-muted);
}

/* Piece dialog */
.piece-dialog {
  border: none;
  border-radius: 12px;
  padding: 1.25rem;
  max-width: 420px;
  width: 90%;
  background: var(--surface);
  color: var(--text);
}

.piece-dialog::backdrop {
  background: rgba(15, 23, 42, 0.45);
}

.piece-dialog h3 {
  margin-top: 0;
  text-transform: capitalize;
}

.dialog-actions {
  margin-top: 1rem;
  display: flex;
  justify-content: flex-end;
}

.revert-scan-btn {
  border: 1px solid #f59e0b;
  border-radius: 999px;
  background: #fffbeb;
  color: #92400e;
  padding: 0.45rem 0.9rem;
}

.piece-actions {
  margin-top: 1rem;
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.edit-piece-btn {
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--text);
  padding: 0.45rem 0.9rem;
}

.edit-piece-btn:hover {
  background: var(--border-soft);
}

button.secondary {
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
  color: var(--text-muted);
}

button.secondary:hover {
  background: var(--surface-2);
}

.piece-edit-form .piece-actions button {
  border-radius: 999px;
}

.pieces-sort {
  margin-top: 0.5rem;
  flex-direction: row;
  align-items: center;
  gap: 0.5rem;
}

.pieces-sort select {
  flex: 1;
  border: 1px solid var(--field-border);
  border-radius: 8px;
}

/* Modern toggle switch */
.dev-toggle {
  cursor: pointer;
  gap: 0.6rem;
}

.dev-toggle input[type="checkbox"] {
  appearance: none;
  -webkit-appearance: none;
  position: relative;
  flex: 0 0 auto;
  width: 44px;
  height: 26px;
  margin: 0;
  padding: 0;
  border: none;
  border-radius: 999px;
  background: var(--border);
  cursor: pointer;
  transition: background-color 0.2s ease;
}

.dev-toggle input[type="checkbox"]::before {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: #ffffff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
  transition: transform 0.2s ease;
}

.dev-toggle input[type="checkbox"]:checked {
  background: var(--primary);
}

.dev-toggle input[type="checkbox"]:checked::before {
  transform: translateX(18px);
}

.dev-toggle input[type="checkbox"]:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
}

.dialog-actions button {
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--text);
}

.dialog-actions button:hover {
  background: var(--border-soft);
}
