:root {
  --bg: #060606;
  --surface: #0e0e0e;
  --surface-hover: #161616;
  --surface-2: #161616;
  --surface-3: #1e1e1e;
  --surface-border: #1a1a1a;
  --text: #d4d4d4;
  --text-dim: #666;
  --text-muted: #444;
  --red: #c0392b;
  --red-light: #e74c3c;
  --red-glow: rgba(192, 57, 43, 0.4);
  --red-bg: rgba(192, 57, 43, 0.12);
  --green: #22c55e;
  --green-glow: rgba(34, 197, 94, 0.35);
  --amber: #eab308;
  --blue: #3b82f6;
  --blue-glow: rgba(59, 130, 246, 0.3);
  --bar-height: 48px;
  --safe-top: env(safe-area-inset-top, 0px);
}

* { margin: 0; padding: 0; box-sizing: border-box; }
*::-webkit-scrollbar { display: none; }
* { scrollbar-width: none; }

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
}

/* ---- TOP BAR ---- */
.top-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
  padding-top: env(safe-area-inset-top, 0px);
  min-height: var(--bar-height);
  background: var(--surface);
  border-bottom: 1px solid var(--surface-border);
  flex-shrink: 0;
}

.top-left {
  display: flex;
  align-items: center;
  gap: 10px;
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--green);
  box-shadow: 0 0 8px var(--green-glow);
  flex-shrink: 0;
  animation: dot-pulse 3s ease-in-out infinite;
}

@keyframes dot-pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.5; }
}

.status-dot.disconnected {
  background: var(--red);
  box-shadow: 0 0 8px var(--red-glow);
}

.top-title {
  font-size: 15px;
  font-weight: 700;
  color: var(--text);
  letter-spacing: -0.3px;
}

.top-title-sub {
  font-size: 12px;
  font-weight: 500;
  color: var(--text-dim);
  letter-spacing: 0;
}

.top-gear {
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  border-radius: 50%;
  transition: color 0.2s, background 0.2s;
  -webkit-tap-highlight-color: transparent;
}

.top-gear svg {
  width: 20px;
  height: 20px;
}

.top-gear:active {
  color: var(--text-dim);
  background: var(--surface-2);
}

/* ---- MAIN CONTENT ---- */
.main-content {
  flex: 1;
  overflow: hidden;
  position: relative;
}

.screen {
  position: absolute;
  inset: 0;
  overflow-y: auto;
  display: none;
}

.screen.active {
  display: flex;
  flex-direction: column;
}

.screen::-webkit-scrollbar { display: none; }
.screen { scrollbar-width: none; }

/* ---- KEYPAD ---- */
#screenKeypad {
  justify-content: center;
  padding-bottom: 15px;
  overflow: hidden;
}

.keypad-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
  padding: 14px;
}

/* ---- KEYPAD BUTTON ---- */
.keypad-btn {
  position: relative;
  aspect-ratio: 0.95;
  background: var(--surface-2);
  border: 1px solid var(--surface-border);
  border-radius: 14px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  cursor: pointer;
  transition: transform 0.12s, border-color 0.2s, background 0.2s, opacity 0.2s, filter 0.2s;
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  overflow: hidden;
  opacity: 0.45;
  filter: saturate(0.3);
}

.keypad-btn img {
  pointer-events: none;
  -webkit-user-drag: none;
  user-drag: none;
}

.keypad-btn::before {
  content: '';
  position: absolute;
  top: 0;
  left: 10%;
  right: 10%;
  height: 1px;
  background: linear-gradient(90deg, transparent, rgba(255,255,255,0.04), transparent);
  pointer-events: none;
}

.keypad-btn .btn-label {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-dim);
  text-align: center;
  padding: 0 6px;
  line-height: 1.3;
  transition: color 0.2s;
  z-index: 1;
}


.keypad-btn .btn-hold-bar {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  border-radius: 14px;
  pointer-events: none;
  opacity: 0.15;
  z-index: 0;
}

.keypad-btn .btn-fill-bar {
  position: absolute;
  inset: 0;
  border-radius: 14px;
  pointer-events: none;
  opacity: 0.15;
  z-index: 0;
}

/* ---- LABEL IMAGE ON BUTTON ---- */
.keypad-btn .btn-label-img-wrap {
  width: 62%;
  aspect-ratio: 1;
  border: 2px solid var(--btn-color, var(--surface-border));
  border-radius: 8px;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1;
  transition: border-color 0.2s, box-shadow 0.2s;
}

.keypad-btn .btn-label-img-wrap img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;
}

.keypad-btn.active .btn-label-img-wrap {
  border-color: var(--btn-color, var(--red));
  box-shadow: 0 0 12px var(--btn-glow, var(--red-glow));
}

/* ---- ACTIVE STATE ---- */
.keypad-btn.active {
  opacity: 1;
  filter: saturate(1);
  border-color: var(--btn-color, var(--red));
  background: var(--btn-bg, var(--red-bg));
  box-shadow:
    0 0 24px var(--btn-glow, var(--red-glow)),
    inset 0 0 24px var(--btn-glow-inner, rgba(192, 57, 43, 0.06));
}

.keypad-btn.active .btn-label {
  color: var(--btn-color-light, var(--red-light));
}


.keypad-btn.fading {
  opacity: 0.45;
  filter: saturate(0.3);
  border-color: var(--surface-border);
  background: var(--surface-2);
  box-shadow: none;
  transition: all 0.25s ease-out;
}

.keypad-btn.fading .btn-fill-bar {
  opacity: 0;
  transition: opacity 0.25s ease-out;
}

.keypad-btn:active {
  transform: scale(0.95);
}

@keyframes pulse-glow {
  0%, 100% {
    box-shadow:
      0 0 24px var(--btn-glow, var(--red-glow)),
      inset 0 0 24px var(--btn-glow-inner, rgba(192, 57, 43, 0.06));
  }
  50% {
    box-shadow:
      0 0 36px var(--btn-glow, var(--red-glow)),
      0 0 60px var(--btn-glow-outer, rgba(192, 57, 43, 0.12)),
      inset 0 0 24px var(--btn-glow-inner, rgba(192, 57, 43, 0.06));
  }
}


/* ---- SETTINGS ---- */
.settings-body {
  padding: 12px 16px;
  padding-bottom: calc(16px + 15px);
  overflow-y: auto;
  flex: 1;
}

.setting-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 0;
  margin-bottom: 8px;
}

.setting-label {
  font-size: 13px;
  color: var(--text-dim);
}

.setting-select {
  background: var(--surface-2);
  border: 1px solid var(--surface-border);
  color: var(--text);
  padding: 6px 10px;
  border-radius: 8px;
  font-size: 13px;
  font-family: inherit;
}

.settings-divider {
  height: 1px;
  background: var(--surface-border);
  margin: 16px 0;
}

.keypad-type-card {
  margin-bottom: 16px;
}

.keypad-type-label {
  font-size: 10px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.8px;
  color: var(--text-muted);
  margin-bottom: 8px;
}

.keypad-type-options {
  display: flex;
  gap: 10px;
}

.keypad-type-option {
  flex: 1;
  background: var(--surface-2);
  border: 1.5px solid var(--surface-border);
  border-radius: 12px;
  padding: 14px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  transition: border-color 0.2s;
}

.keypad-type-option.selected {
  border-color: var(--red);
  background: var(--red-bg);
}

.keypad-type-icon {
  width: 48px;
  height: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.keypad-type-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  grid-template-rows: repeat(3, 1fr);
  gap: 2px;
  width: 100%;
  height: 100%;
}

.keypad-type-grid span {
  background: var(--text-muted);
  border-radius: 1px;
}

.keypad-type-option.selected .keypad-type-grid span {
  background: var(--red);
}

.keypad-type-name {
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
}

.keypad-type-desc {
  font-size: 11px;
  color: var(--text-muted);
}

.settings-version {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 16px 0 4px;
  font-size: 11px;
  color: var(--text-muted);
}

.btn-config-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}

.btn-config-header-title {
  font-size: 10px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.8px;
  color: var(--text-muted);
}

.reorder-toggle {
  padding: 5px 12px;
  background: var(--surface-2);
  border: 1px solid var(--surface-border);
  border-radius: 6px;
  color: var(--text-dim);
  font-size: 11px;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  transition: all 0.15s;
}

.reorder-toggle.active {
  background: var(--red-bg);
  border-color: var(--red);
  color: var(--red-light);
}

/* ---- BUTTON CONFIG LIST ---- */
.btn-config-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.btn-config-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 14px;
  background: var(--surface-2);
  border: 1px solid var(--surface-border);
  border-radius: 12px;
  cursor: pointer;
  transition: border-color 0.2s, background 0.2s, opacity 0.2s;
  touch-action: pan-y;
  position: relative;
}

.btn-config-row:active {
  background: var(--surface-3);
}

.btn-config-row.hidden-btn {
  opacity: 0.4;
}

/* Dragging state */
.btn-config-row.dragging {
  opacity: 0.9;
  border-color: var(--red);
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.5);
  transition: none;
}

.btn-config-row.drag-over {
  border-color: var(--blue);
  background: rgba(59, 130, 246, 0.08);
}

/* 5x3 grid thumbnail — matches Haltech NSP layout */
.btn-config-grid {
  width: 60px;
  height: 36px;
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  grid-template-rows: repeat(3, 1fr);
  gap: 2px;
  flex-shrink: 0;
  padding: 3px;
  background: var(--surface-3);
  border: 1px solid var(--surface-border);
  border-radius: 5px;
}

.btn-config-grid .grid-cell {
  background: var(--surface-border);
  border-radius: 2px;
}

.btn-config-grid .grid-cell.highlight {
  background: var(--blue);
  box-shadow: 0 0 4px var(--blue-glow);
}

.btn-config-color {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  flex-shrink: 0;
  border: 2px solid rgba(255,255,255,0.08);
}

.btn-config-info {
  flex: 1;
  min-width: 0;
}

.btn-config-name {
  font-size: 16px;
  font-weight: 600;
  color: var(--text);
}

.btn-config-detail {
  font-size: 13px;
  color: var(--text-muted);
}

.btn-config-mode {
  font-size: 11px;
  font-weight: 500;
  color: var(--text-muted);
  flex-shrink: 0;
}

/* ---- EDIT MODAL ---- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0);
  backdrop-filter: blur(0px);
  -webkit-backdrop-filter: blur(0px);
  z-index: 100;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  pointer-events: none;
  transition: background 0.3s, backdrop-filter 0.3s, -webkit-backdrop-filter 0.3s;
}

.modal-overlay.active {
  background: rgba(0, 0, 0, 0.75);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  pointer-events: auto;
}

.modal-panel {
  background: var(--surface);
  border-top: 1px solid var(--surface-border);
  border-radius: 20px 20px 0 0;
  width: 100%;
  max-width: 420px;
  padding: 20px;
  padding-bottom: calc(20px + 15px);
  box-shadow: 0 -8px 40px rgba(0, 0, 0, 0.5);
  transform: translateY(100%);
  transition: transform 0.3s cubic-bezier(0.32, 0.72, 0, 1);
}

.modal-overlay.active .modal-panel {
  transform: translateY(0);
}

.modal-handle {
  width: 36px;
  height: 4px;
  background: var(--surface-3);
  border-radius: 2px;
  margin: 0 auto 14px;
}

.modal-header {
  margin-bottom: 16px;
}

.modal-title {
  font-size: 16px;
  font-weight: 700;
  color: var(--text);
  letter-spacing: -0.2px;
}

.modal-field {
  margin-bottom: 16px;
}

.modal-field-label {
  font-size: 10px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.8px;
  color: var(--text-muted);
  margin-bottom: 6px;
}

.modal-input {
  width: 100%;
  background: var(--surface-2);
  border: 1px solid var(--surface-border);
  color: var(--text);
  padding: 10px 14px;
  border-radius: 10px;
  font-size: 15px;
  font-family: inherit;
  transition: border-color 0.2s;
}

.modal-input:focus {
  outline: none;
  border-color: var(--red);
  box-shadow: 0 0 0 3px rgba(192, 57, 43, 0.15);
}

.modal-select {
  width: 100%;
  background: var(--surface-2);
  border: 1px solid var(--surface-border);
  color: var(--text);
  padding: 10px 14px;
  border-radius: 10px;
  font-size: 15px;
  font-family: inherit;
  transition: border-color 0.2s;
}

.modal-select:focus {
  outline: none;
  border-color: var(--red);
}

/* ---- COLOR PICKER ---- */
.color-picker {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  padding: 4px 0;
}

.color-swatch {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  cursor: pointer;
  border: 2px solid transparent;
  transition: transform 0.15s, border-color 0.15s, box-shadow 0.15s;
  position: relative;
}

.color-swatch:active {
  transform: scale(0.9);
}

.color-swatch.selected {
  border-color: var(--text);
  box-shadow: 0 0 0 2px var(--bg), 0 0 12px currentColor;
  transform: scale(1.1);
}


/* ---- LABEL PICKER ---- */
.label-picker-wrap {
  display: flex;
  align-items: center;
  gap: 10px;
}

.label-picker-preview {
  width: 52px;
  height: 52px;
  background: #000;
  border: 1.5px solid #333;
  border-radius: 10px;
  flex-shrink: 0;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
}

.label-preview-none {
  font-size: 10px;
  color: var(--text-muted);
}

.label-preview-img {
  width: 46px;
  height: 46px;
  object-fit: contain;
  display: block;
}

.label-preview-clear {
  position: absolute;
  top: -8px;
  right: -8px;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: var(--red);
  border: 2px solid var(--surface);
  color: #fff;
  font-size: 12px;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  z-index: 2;
}

.label-browse-btn {
  margin-left: auto;
  padding: 10px 16px;
  background: var(--surface-2);
  border: 1px solid var(--surface-border);
  border-radius: 10px;
  color: var(--text);
  font-size: 13px;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  flex-shrink: 0;
}

.label-browse-btn:active {
  background: var(--surface-3);
}

.label-picker-grid {
  margin-top: 10px;
  max-height: 240px;
  overflow-y: auto;
  padding: 8px;
  background: var(--surface-2);
  border-radius: 10px;
  border: 1px solid var(--surface-border);
  display: grid;
  grid-template-columns: repeat(auto-fill, 60px);
  justify-content: center;
  gap: 8px;
}

.label-picker-grid::-webkit-scrollbar { display: none; }
.label-picker-grid { scrollbar-width: none; }

.label-thumb {
  width: 60px;
  height: 60px;
  background: #000;
  border: 1.5px solid #333;
  border-radius: 8px;
  cursor: pointer;
  transition: border-color 0.15s, transform 0.1s;
  overflow: hidden;
}

.label-thumb img {
  width: 60px;
  height: 60px;
  object-fit: contain;
  display: block;
}

.label-thumb:active {
  transform: scale(0.93);
}

.label-thumb.selected {
  border-color: var(--red);
  box-shadow: 0 0 8px var(--red-glow);
}

/* ---- VISIBILITY TOGGLE ---- */
.visibility-toggle {
  display: flex;
  gap: 0;
  border-radius: 10px;
  overflow: hidden;
  border: 1px solid var(--surface-border);
}

.vis-btn {
  flex: 1;
  padding: 10px;
  background: var(--surface-2);
  border: none;
  color: var(--text-muted);
  font-size: 13px;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}

.vis-btn.active {
  background: var(--surface-3);
  color: var(--text);
}

.vis-btn:first-child {
  border-right: 1px solid var(--surface-border);
}

.modal-actions {
  display: flex;
  gap: 10px;
  margin-top: 20px;
}

.modal-btn {
  flex: 1;
  padding: 12px;
  border-radius: 10px;
  border: 1px solid var(--surface-border);
  background: var(--surface-2);
  color: var(--text);
  font-size: 14px;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  transition: background 0.15s, transform 0.1s;
}

.modal-btn:active {
  transform: scale(0.97);
}

.modal-btn.primary {
  background: var(--red);
  border-color: var(--red);
  color: #fff;
  box-shadow: 0 4px 16px rgba(192, 57, 43, 0.3);
}

.modal-btn.danger {
  color: var(--red);
}

/* ---- LANDSCAPE ---- */
@media (orientation: landscape) {
  .top-bar {
    display: none;
  }

  /* Show top bar on settings screen in landscape */
  body.on-settings .top-bar {
    display: flex;
  }

  body.on-settings .top-gear-float {
    display: none;
  }

  #screenKeypad {
    padding: env(safe-area-inset-top, 0px) env(safe-area-inset-right, 0px) env(safe-area-inset-bottom, 0px) env(safe-area-inset-left, 0px);
  }

  .keypad-grid {
    grid-template-columns: repeat(5, 1fr);
    gap: 8px;
    padding: 8px;
    height: 100%;
    align-content: stretch;
  }

  .keypad-btn {
    aspect-ratio: auto;
    border-radius: 10px;
  }

  .keypad-btn .btn-label-img-wrap {
    border-radius: 6px;
  }

  .keypad-btn .btn-fill-bar,
  .keypad-btn .btn-hold-bar {
    border-radius: 10px;
  }

  .keypad-btn.fading {
    border-radius: 10px;
  }

  /* Floating gear button in landscape */
  .top-gear-float {
    display: flex;
  }
}

/* Floating gear for landscape — hidden by default */
.top-gear-float {
  display: none;
  position: fixed;
  top: 8px;
  right: max(8px, env(safe-area-inset-right, 0px));
  width: 32px;
  height: 32px;
  align-items: center;
  justify-content: center;
  background: var(--surface);
  border: 1px solid var(--surface-border);
  border-radius: 50%;
  color: var(--text-muted);
  cursor: pointer;
  z-index: 50;
  opacity: 0.5;
  transition: opacity 0.2s;
  -webkit-tap-highlight-color: transparent;
}

.top-gear-float svg {
  width: 16px;
  height: 16px;
}

.top-gear-float:active {
  opacity: 1;
}
