:root {
  --bg: #0b0b0d;
  --surface: #161619;
  --surface-2: #1f1f23;
  --surface-3: #27272c;
  --border: rgba(255, 255, 255, 0.07);
  --text: #f4f2ef;
  --text-dim: #8d8a86;
  --accent: #e0b98a;
  --accent-soft: rgba(224, 185, 138, 0.14);
  --accent-text: #241a0f;
  --danger: #e07a7a;
  --r-lg: 16px;
  --r-md: 13px;
  --r-sm: 10px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  font-size: 15px;
  line-height: 1.4;
  -webkit-tap-highlight-color: transparent;
  -webkit-font-smoothing: antialiased;
}

body { min-height: 100vh; }

button, input, select {
  font-family: inherit;
  font-size: inherit;
  color: inherit;
}

button {
  border: none;
  background: none;
  cursor: pointer;
  padding: 0;
}

svg { display: block; }

.app {
  width: 100%;
  max-width: 1180px;
  margin: 0 auto;
  padding: 0 16px;
}

.screen { display: none; }
.screen.active { display: block; }

/* ============ Login ============ */
#login-screen { place-items: center; min-height: 100vh; padding: 24px; }
#login-screen.active { display: grid; }

.login-card { width: 100%; max-width: 340px; }
.login-card h1 { font-size: 21px; margin: 0 0 4px; font-weight: 650; }
.login-card p { color: var(--text-dim); font-size: 14px; margin: 0 0 22px; }

.field { margin-bottom: 14px; }
.field label {
  display: block;
  font-size: 12px;
  color: var(--text-dim);
  margin-bottom: 6px;
}

.field-input,
.field input {
  width: 100%;
  padding: 11px 13px;
  border-radius: var(--r-md);
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  font-size: 15px;
  outline: none;
}

.field input:focus,
.field-input:focus { border-color: var(--accent); }

.btn-primary {
  width: 100%;
  padding: 12px;
  border-radius: var(--r-md);
  background: var(--accent);
  color: var(--accent-text);
  font-weight: 650;
  margin-top: 6px;
}
.btn-primary:active { opacity: 0.85; }

.login-error {
  color: var(--danger);
  font-size: 13px;
  margin-top: 12px;
  min-height: 16px;
}

/* ============ Top bar ============ */
#topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  background: rgba(11, 11, 13, 0.88);
  backdrop-filter: saturate(160%) blur(14px);
  border-bottom: 1px solid var(--border);
  padding: calc(env(safe-area-inset-top) + 10px) 0 10px;
}

.topbar-row {
  display: flex;
  align-items: center;
  gap: 7px;
  overflow-x: auto;
  scrollbar-width: none;
}
.topbar-row::-webkit-scrollbar { display: none; }

.chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 6px 12px;
  font-size: 13px;
  color: var(--text-dim);
  white-space: nowrap;
  flex-shrink: 0;
}

.chip svg { color: var(--text-dim); }
.chip-strong { color: var(--text); }
.chip-spacer { margin-left: auto; }

.chip-active {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-text);
  font-weight: 600;
}
.chip-active svg { color: var(--accent-text); }

/* ============ Dashboard ============ */
#dashboard { padding-top: 6px; padding-bottom: calc(env(safe-area-inset-bottom) + 40px); }

.section { margin-top: 22px; }
.section:first-child { margin-top: 14px; }

.section-head {
  display: flex;
  align-items: center;
  gap: 7px;
  margin: 0 2px 9px;
  color: var(--text-dim);
}

.section-head svg { opacity: 0.75; }

.section-name {
  font-size: 11.5px;
  font-weight: 650;
  letter-spacing: 0.07em;
  text-transform: uppercase;
  flex: 1;
  min-width: 0;
}

.section-name-input {
  flex: 1;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-sm);
  padding: 5px 9px;
  font-size: 12px;
  font-weight: 650;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text);
  outline: none;
  min-width: 0;
}

.section-tools { display: flex; gap: 4px; flex-shrink: 0; }

.icon-btn {
  width: 28px;
  height: 28px;
  border-radius: 8px;
  display: grid;
  place-items: center;
  color: var(--text-dim);
  background: var(--surface);
  border: 1px solid var(--border);
  flex-shrink: 0;
}
.icon-btn:active { background: var(--surface-2); }
.icon-btn-danger { color: var(--danger); }
.icon-btn:disabled { opacity: 1; background: none; border-color: transparent; cursor: default; }

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(168px, 1fr));
  gap: 10px;
}

.tile {
  position: relative;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  padding: 13px 14px;
  min-height: 94px;
  display: flex;
  flex-direction: column;
  gap: 7px;
  text-align: left;
  transition: background 0.15s ease;
}

.tile:not(.tile-static):active { background: var(--surface-2); }

.tile-icon { color: var(--text); opacity: 0.9; }

.tile-title {
  font-size: 13px;
  font-weight: 650;
  letter-spacing: 0.035em;
  text-transform: uppercase;
  line-height: 1.25;
}

.tile-summary {
  font-size: 12.5px;
  color: var(--text-dim);
  margin-top: auto;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.tile-add {
  align-items: center;
  justify-content: center;
  gap: 6px;
  color: var(--text-dim);
  background: none;
  border-style: dashed;
  border-color: rgba(255, 255, 255, 0.12);
}

.section-add {
  width: 100%;
  margin-top: 18px;
  padding: 13px;
  border-radius: var(--r-lg);
  border: 1px dashed rgba(255, 255, 255, 0.12);
  color: var(--text-dim);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  font-size: 13.5px;
}

.empty-hint {
  color: var(--text-dim);
  font-size: 13px;
  padding: 10px 2px;
}

/* ============ Edit mode ============ */
.edit-banner {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--accent-soft);
  border: 1px solid rgba(224, 185, 138, 0.25);
  border-radius: var(--r-md);
  padding: 9px 12px;
  font-size: 13px;
  color: var(--accent);
  margin-top: 12px;
}

.drag-handle {
  position: absolute;
  top: 9px;
  right: 9px;
  color: var(--text-dim);
  opacity: 0.6;
  touch-action: none;
  cursor: grab;
  padding: 3px;
}

.dragging {
  opacity: 0.9;
  z-index: 200;
  box-shadow: 0 12px 28px rgba(0, 0, 0, 0.55);
  cursor: grabbing;
}

.remove-badge {
  position: absolute;
  top: -6px;
  left: -6px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: var(--danger);
  color: #1a0f0f;
  display: grid;
  place-items: center;
  z-index: 3;
}

/* ============ Popup ============ */
#popup-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  display: none;
  z-index: 50;
  align-items: flex-end;
  justify-content: center;
}
#popup-overlay.active { display: flex; }

#popup {
  width: 100%;
  max-width: 760px;
  max-height: 90vh;
  background: #121214;
  border: 1px solid var(--border);
  border-radius: 20px 20px 0 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  animation: sheet-up 0.22s ease;
}

@keyframes sheet-up { from { transform: translateY(14px); opacity: 0.6; } }

@media (min-width: 740px) {
  #popup-overlay { align-items: center; }
  #popup { border-radius: 20px; max-height: 82vh; }
}

.popup-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 15px 16px 12px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.popup-title {
  font-size: 17px;
  font-weight: 650;
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.popup-title-input {
  flex: 1;
  min-width: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-sm);
  color: var(--text);
  font-size: 16px;
  font-weight: 650;
  padding: 6px 10px;
  outline: none;
}

.popup-close {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  background: var(--surface-2);
  color: var(--text-dim);
  display: grid;
  place-items: center;
  flex-shrink: 0;
}

#popup-body {
  overflow-y: auto;
  padding: 14px 16px calc(env(safe-area-inset-bottom) + 22px);
}

.popup-section-label {
  font-size: 11.5px;
  font-weight: 650;
  letter-spacing: 0.07em;
  text-transform: uppercase;
  color: var(--text-dim);
  margin: 16px 2px 8px;
}
.popup-section-label:first-child { margin-top: 2px; }

/* ============ Entity tiles ============ */
.entity-tile {
  position: relative;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  text-align: left;
  min-height: 78px;
}

.entity-tile.active {
  border-color: rgba(224, 185, 138, 0.45);
}

.entity-tile-body {
  padding: 10px 12px 8px;
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 1px;
}

.entity-tile-head {
  display: flex;
  align-items: flex-start;
  gap: 7px;
}

.entity-tile-head svg { color: var(--text-dim); flex-shrink: 0; margin-top: 1px; }
.entity-tile.active .entity-tile-head svg { color: var(--accent); }

.entity-tile-name {
  font-size: 13.5px;
  font-weight: 600;
  line-height: 1.3;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.entity-tile-time {
  font-size: 11.5px;
  color: var(--text-dim);
  margin-top: 2px;
}

.entity-tile-state {
  padding: 7px 12px;
  font-size: 12.5px;
  color: var(--text-dim);
  background: var(--surface-2);
  border-top: 1px solid var(--border);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.entity-tile.active .entity-tile-state {
  background: var(--accent);
  border-top-color: var(--accent);
  color: var(--accent-text);
  font-weight: 600;
}

.btn-add-wide {
  grid-column: 1 / -1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  padding: 12px;
  border-radius: var(--r-md);
  border: 1px dashed rgba(255, 255, 255, 0.12);
  color: var(--text-dim);
  font-size: 13.5px;
}

/* ============ Sheets (pickers) ============ */
.sheet-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: none;
  z-index: 70;
  align-items: flex-end;
  justify-content: center;
}
.sheet-overlay.active { display: flex; }

.sheet {
  width: 100%;
  max-width: 620px;
  max-height: 86vh;
  background: #121214;
  border: 1px solid var(--border);
  border-radius: 20px 20px 0 0;
  padding: 15px 16px calc(env(safe-area-inset-bottom) + 18px);
  display: flex;
  flex-direction: column;
  gap: 10px;
  overflow: hidden;
}

@media (min-width: 740px) {
  .sheet-overlay { align-items: center; }
  .sheet { border-radius: 20px; max-height: 76vh; }
}

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

.sheet-title { font-size: 16px; font-weight: 650; }

.filter-row { display: flex; gap: 8px; }

.filter-row select {
  flex: 1;
  min-width: 0;
  padding: 9px 10px;
  border-radius: var(--r-sm);
  background: var(--surface);
  border: 1px solid var(--border);
  font-size: 13px;
  outline: none;
}

.icon-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(46px, 1fr));
  gap: 7px;
  overflow-y: auto;
}

.icon-grid-item {
  aspect-ratio: 1;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-sm);
  display: grid;
  place-items: center;
  color: var(--text);
}
.icon-grid-item:active { background: var(--surface-2); }

.entity-list {
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.entity-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 10px;
  border-radius: var(--r-md);
  background: var(--surface);
  border: 1px solid transparent;
  text-align: left;
  width: 100%;
}

.entity-row.added {
  background: var(--accent-soft);
  border-color: rgba(224, 185, 138, 0.3);
}

.entity-row-icon {
  width: 30px;
  height: 30px;
  border-radius: 9px;
  background: var(--surface-2);
  display: grid;
  place-items: center;
  flex-shrink: 0;
  color: var(--text-dim);
}

.entity-row-text { flex: 1; min-width: 0; }

.entity-row-name {
  font-size: 13.5px;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.entity-row-meta {
  font-size: 11px;
  color: var(--text-dim);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.entity-row-check { color: var(--accent); flex-shrink: 0; }

.entity-list-empty,
.loading {
  text-align: center;
  color: var(--text-dim);
  font-size: 13px;
  padding: 28px 0;
}

.loading { padding: 70px 0; }

/* ============ Entity detail ============ */
.detail-toprow {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  margin-bottom: 4px;
}

.detail-name { font-size: 16px; font-weight: 650; }

.detail-section-label {
  font-size: 11.5px;
  font-weight: 650;
  letter-spacing: 0.07em;
  text-transform: uppercase;
  color: var(--text-dim);
  margin: 18px 0 9px;
}

.slider-row { display: flex; align-items: center; gap: 13px; }

.slider-row input[type="range"] {
  flex: 1;
  min-width: 0;
  accent-color: var(--accent);
  height: 26px;
}

.slider-value {
  font-size: 13px;
  color: var(--text-dim);
  width: 44px;
  text-align: right;
  flex-shrink: 0;
}

.toggle-switch {
  width: 48px;
  height: 29px;
  border-radius: 999px;
  background: var(--surface-3);
  position: relative;
  flex-shrink: 0;
  transition: background 0.18s ease;
}

.toggle-switch span {
  position: absolute;
  top: 3px;
  left: 3px;
  width: 23px;
  height: 23px;
  border-radius: 50%;
  background: var(--text-dim);
  transition: transform 0.18s ease, background 0.18s ease;
}

.toggle-switch.on { background: rgba(224, 185, 138, 0.3); }
.toggle-switch.on span { transform: translateX(19px); background: var(--accent); }

.color-grid { display: flex; flex-wrap: wrap; gap: 9px; }

.color-swatch {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: 2px solid transparent;
  flex-shrink: 0;
}
.color-swatch.selected { border-color: var(--text); }

.color-swatch-custom {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: none;
  background: none;
  padding: 0;
  flex-shrink: 0;
  cursor: pointer;
}

.stepper-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 22px;
}

.stepper-btn {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: var(--surface);
  border: 1px solid var(--border);
  font-size: 21px;
  display: grid;
  place-items: center;
}

.stepper-value {
  font-size: 24px;
  font-weight: 650;
  min-width: 96px;
  text-align: center;
}

.mode-row { display: flex; flex-wrap: wrap; gap: 7px; }

.mode-btn {
  padding: 9px 14px;
  border-radius: var(--r-sm);
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text-dim);
  font-size: 13px;
}

.mode-btn.active {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-text);
  font-weight: 600;
}

.mode-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

/* ---- Camera ---- */
.camera-frame {
  width: 100%;
  aspect-ratio: 16 / 9;
  background: #000;
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  overflow: hidden;
  display: grid;
  place-items: center;
  margin-top: 4px;
}

.camera-frame img {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.camera-offline {
  flex-direction: column;
  gap: 9px;
  color: var(--text-dim);
  font-size: 13px;
  background: var(--surface);
}

.camera-offline svg { opacity: 0.5; }

.detail-hint {
  font-size: 12px;
  color: var(--text-dim);
  margin-top: 8px;
}

/* ---- Info list ---- */
.info-list {
  display: flex;
  flex-direction: column;
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  overflow: hidden;
}

.info-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  padding: 10px 12px;
  font-size: 13px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.info-row:last-child { border-bottom: none; }
.info-row span { color: var(--text-dim); flex-shrink: 0; }
.info-row strong {
  font-weight: 600;
  text-align: right;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
}
