:root {
  color-scheme: dark;
  --bg: #0c0f16;
  --surface: #141925;
  --surface-2: #1b2232;
  --border: #263044;
  --text: #e6ecf7;
  --text-dim: #8b98b0;
  --primary: #3b82f6;
  --primary-hover: #2563eb;
  --accent: #06b6d4;
  --ok: #10b981;
  --warn: #f59e0b;
  --err: #ef4444;

  --btn-hover: #232c40;
  --btn-hover-border: #3a4966;
  --accent-bg: rgba(6, 182, 212, 0.16);
  --accent-bg-hover: rgba(6, 182, 212, 0.28);
  --accent-text: #67e8f9;
  --ok-text: #6ee7b7;
  --warn-text: #fcd34d;
  --err-text: #fca5a5;
  --info-text: #93c5fd;
  --code-text: #93c5fd;

  --stage-bg: radial-gradient(circle at center, #131926 0%, #0b0e15 100%);
  --key-bg: #1e2638;
  --key-top: #263046;
  --key-border: #36435e;
  --key-hover-border: #5b729c;
  --key-shadow: #0a0d14;
  --key-inset: rgba(255, 255, 255, 0.07);
  --key-tested-bg: linear-gradient(180deg, #1b3233 0%, #152529 100%);
  --key-tested-border: rgba(16, 185, 129, 0.6);
  --key-hold-text: #93c5fd;
  --key-hold-bg: rgba(15, 23, 42, 0.55);

  --layer-active-text: #ffffff;
  --layer-num-bg: rgba(255, 255, 255, 0.08);
  --modal-backdrop: rgba(0, 0, 0, 0.7);
}

:root[data-theme="light"] {
  color-scheme: light;
  --bg: #eef2f7;
  --surface: #ffffff;
  --surface-2: #f4f7fb;
  --border: #d5deeb;
  --text: #111827;
  --text-dim: #526075;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --accent: #0891b2;
  --ok: #059669;
  --warn: #d97706;
  --err: #dc2626;

  --btn-hover: #e7eef8;
  --btn-hover-border: #b6c5dc;
  --accent-bg: rgba(8, 145, 178, 0.12);
  --accent-bg-hover: rgba(8, 145, 178, 0.2);
  --accent-text: #0e7490;
  --ok-text: #047857;
  --warn-text: #b45309;
  --err-text: #b91c1c;
  --info-text: #1d4ed8;
  --code-text: #1d4ed8;

  --stage-bg: radial-gradient(circle at center, #e6edf7 0%, #dae3f0 100%);
  --key-bg: #eef2f8;
  --key-top: #ffffff;
  --key-border: #c5d1e2;
  --key-hover-border: #64748b;
  --key-shadow: #b6c2d4;
  --key-inset: rgba(255, 255, 255, 0.85);
  --key-tested-bg: linear-gradient(180deg, #ecfdf5 0%, #d1fae5 100%);
  --key-tested-border: rgba(5, 150, 105, 0.65);
  --key-hold-text: #1d4ed8;
  --key-hold-bg: rgba(37, 99, 235, 0.12);

  --layer-active-text: #1d4ed8;
  --layer-num-bg: rgba(15, 23, 42, 0.08);
  --modal-backdrop: rgba(15, 23, 42, 0.45);
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "PingFang SC",
    "Microsoft YaHei", sans-serif;
  height: 100%;
  transition: background 0.2s ease, color 0.2s ease;
}

.hidden {
  display: none !important;
}

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

.app-shell {
  display: flex;
  flex-direction: column;
  height: 100vh;
  max-width: 1480px;
  margin: 0 auto;
  padding: 14px 18px;
  gap: 12px;
  overflow: hidden;
}

@media (max-width: 1020px) {
  .app-shell {
    height: auto;
    min-height: 100vh;
    overflow: visible;
  }
}

/* Topbar */
.topbar {
  display: flex;
  flex-direction: column;
  gap: 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px 16px;
}

.topbar-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  width: 100%;
}

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

.brand-logo-link {
  display: flex;
  align-items: center;
  text-decoration: none;
  background: #ffffff;
  padding: 3px 6px;
  border-radius: 7px;
  border: 1px solid rgba(0, 0, 0, 0.1);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12);
  transition: transform 0.15s ease, box-shadow 0.15s ease;
  flex-shrink: 0;
}

.brand-logo-link:hover {
  transform: translateY(-1px);
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.2);
}

.brand-logo-img {
  height: 28px;
  width: auto;
  display: block;
  object-fit: contain;
}

.store-corner {
  display: flex;
  align-items: center;
  margin-left: auto;
  flex-shrink: 0;
}

.store-icon {
  display: block;
  flex-shrink: 0;
  color: #ffffff;
}

.brand h1 {
  margin: 0;
  font-size: 18px;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.brand-sub {
  margin: 2px 0 0;
  font-size: 12px;
  color: var(--text-dim);
}

.topbar-controls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: flex-start;
  gap: 10px 14px;
  width: 100%;
  padding-top: 10px;
  border-top: 1px solid var(--border);
}

.devices-wrap,
.lock-controls,
.pref-controls {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
}

.pref-controls {
  margin-left: auto;
}

.devices-list {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.device-pill {
  font-size: 12px;
  padding: 4px 10px;
  border-radius: 999px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text-dim);
}

.device-pill.is-connected {
  color: var(--ok-text);
  border-color: rgba(16, 185, 129, 0.45);
  background: rgba(16, 185, 129, 0.12);
}

.hid-badge {
  font-size: 12px;
  font-weight: 600;
  padding: 4px 10px;
  border-radius: 999px;
  border: 1px solid rgba(16, 185, 129, 0.45);
  background: rgba(16, 185, 129, 0.16);
  color: var(--ok-text);
  white-space: nowrap;
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

.hid-badge::before {
  content: "";
  display: inline-block;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: #10b981;
  box-shadow: 0 0 6px #10b981;
}

.lock-badge {
  font-size: 12px;
  font-weight: 600;
  padding: 4px 10px;
  border-radius: 999px;
  border: 1px solid transparent;
  min-width: 158px;
  text-align: center;
  white-space: nowrap;
}

.lock-badge.is-locked {
  background: var(--accent-bg);
  color: var(--accent-text);
  border-color: rgba(6, 182, 212, 0.5);
}

.lock-badge.is-active {
  background: rgba(16, 185, 129, 0.14);
  color: var(--ok-text);
  border-color: rgba(16, 185, 129, 0.45);
}

.lock-badge.is-paused {
  background: rgba(245, 158, 11, 0.14);
  color: var(--warn-text);
  border-color: rgba(245, 158, 11, 0.45);
}

.lock-badge.is-off {
  background: rgba(239, 68, 68, 0.12);
  color: var(--err-text);
  border-color: rgba(239, 68, 68, 0.4);
}

/* Buttons & Inputs */
.btn {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  border-radius: 8px;
  padding: 8px 14px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, transform 0.08s;
  white-space: nowrap;
}

.btn:hover:not(:disabled) {
  background: var(--btn-hover);
  border-color: var(--btn-hover-border);
}

.btn:active:not(:disabled) {
  transform: translateY(1px);
}

.btn:disabled {
  opacity: 0.6;
  cursor: wait;
}

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

.btn-primary:hover:not(:disabled) {
  background: var(--primary-hover);
  border-color: var(--primary-hover);
}

.btn-accent {
  background: var(--accent-bg);
  border-color: rgba(6, 182, 212, 0.5);
  color: var(--accent-text);
}

.btn-accent:hover:not(:disabled) {
  background: var(--accent-bg-hover);
}

.btn.btn-store {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  background: linear-gradient(135deg, #2563eb 0%, #4f46e5 100%);
  border: 1px solid rgba(147, 197, 253, 0.45);
  color: #ffffff;
  font-weight: 600;
  text-decoration: none;
  box-shadow: 0 2px 8px rgba(37, 99, 235, 0.35);
  transition: transform 0.15s ease, box-shadow 0.15s ease, background 0.15s ease;
  white-space: nowrap;
}

.btn.btn-store:hover:not(:disabled) {
  background: linear-gradient(135deg, #3b82f6 0%, #6366f1 100%);
  border-color: rgba(191, 219, 254, 0.75);
  color: #ffffff;
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(59, 130, 246, 0.45);
}

:root[data-theme="light"] .btn.btn-store {
  background: linear-gradient(135deg, #1d4ed8 0%, #1e40af 100%);
  border-color: #1e3a8a;
  color: #ffffff;
  box-shadow: 0 2px 6px rgba(29, 78, 216, 0.28);
}

:root[data-theme="light"] .btn.btn-store:hover:not(:disabled) {
  background: linear-gradient(135deg, #2563eb 0%, #1d4ed8 100%);
  border-color: #1d4ed8;
  color: #ffffff;
  box-shadow: 0 4px 10px rgba(29, 78, 216, 0.38);
}

.btn-ghost {
  background: transparent;
  border-color: transparent;
  color: var(--text-dim);
}

.btn-ghost:hover:not(:disabled) {
  background: var(--surface-2);
  color: var(--text);
}

.btn-sm {
  padding: 5px 10px;
  font-size: 12px;
}

.btn-xs {
  padding: 3px 8px;
  font-size: 11px;
}

.switch-label,
.check-label {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--text);
  cursor: pointer;
  user-select: none;
}

/* Loader Bar */
.loader-bar {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

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

.url-input,
.token-input,
.select-input {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  padding: 8px 12px;
  font-size: 13px;
  outline: none;
}

.url-input {
  flex: 1 1 380px;
}

.token-input {
  width: 100%;
  max-width: 420px;
}

.url-input:focus,
.token-input:focus,
.select-input:focus {
  border-color: var(--primary);
}

.preset-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
}

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

.preset-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.preset-chip {
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text-dim);
  border-radius: 999px;
  padding: 3px 10px;
  font-size: 12px;
  cursor: pointer;
}

.preset-chip:hover {
  color: var(--text);
  border-color: var(--primary);
}

.selector-controls {
  margin-left: auto;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 12px;
}

.branch-select-wrap,
.keymap-select-wrap {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--text-dim);
}

.branch-select-wrap select,
.keymap-select-wrap select {
  padding: 4px 8px;
  font-size: 12px;
  max-width: 200px;
}

.advanced-panel {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding-top: 8px;
  border-top: 1px solid var(--border);
}

.adv-item {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.file-btn {
  display: inline-flex;
  align-items: center;
}

.status-banner {
  padding: 8px 12px;
  border-radius: 8px;
  font-size: 12.5px;
  border: 1px solid transparent;
}

.status-banner.is-info {
  background: rgba(59, 130, 246, 0.12);
  border-color: rgba(59, 130, 246, 0.35);
  color: var(--info-text);
}

.status-banner.is-success {
  background: rgba(16, 185, 129, 0.12);
  border-color: rgba(16, 185, 129, 0.35);
  color: var(--ok-text);
}

.status-banner.is-warn {
  background: rgba(245, 158, 11, 0.12);
  border-color: rgba(245, 158, 11, 0.35);
  color: var(--warn-text);
}

.status-banner.is-error {
  background: rgba(239, 68, 68, 0.12);
  border-color: rgba(239, 68, 68, 0.35);
  color: var(--err-text);
}

/* Workspace Grid */
.workspace {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 310px;
  gap: 12px;
  flex: 1;
  min-height: 0;
  overflow: hidden;
}

@media (max-width: 1020px) {
  .workspace {
    grid-template-columns: 1fr;
    overflow: visible;
  }
}

.stage-panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  height: 100%;
  min-height: 360px;
  overflow: hidden;
}

.stage-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  min-height: 32px;
  flex-shrink: 0;
}

.stage-title-wrap {
  display: flex;
  align-items: center;
  gap: 8px;
}

.stage-title-badge {
  font-size: 12.5px;
  font-weight: 700;
  padding: 4px 10px;
  border-radius: 8px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text);
}

.stage-options {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 12px;
}

.meta-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  flex-shrink: 0;
}

.meta-pill {
  font-size: 12px;
  color: var(--text-dim);
  background: var(--bg);
  border: 1px solid var(--border);
  padding: 3px 10px;
  border-radius: 6px;
  font-variant-numeric: tabular-nums;
}

.meta-pill.is-highlight {
  color: var(--ok-text);
  border-color: rgba(16, 185, 129, 0.35);
  min-width: 148px;
  text-align: center;
}

/* Keyboard Canvas */
.keyboard-stage {
  flex: 1;
  min-height: 290px;
  background: var(--stage-bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  display: flex;
  align-items: safe center;
  justify-content: safe center;
  padding: 20px;
  overflow: auto;
  position: relative;
}

.keyboard-stage.is-dragover {
  border-color: var(--accent);
  box-shadow: inset 0 0 0 2px rgba(6, 182, 212, 0.35);
}

.keyboard-canvas {
  position: relative;
  flex-shrink: 0;
  margin: auto;
}

.keycap {
  position: absolute;
  padding: 0;
  border-radius: 8px;
  background: linear-gradient(180deg, var(--key-top) 0%, var(--key-bg) 100%);
  border: 1px solid var(--key-border);
  box-shadow: 0 4px 0 var(--key-shadow), inset 0 1px 0 var(--key-inset);
  color: var(--text);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  user-select: none;
  transition: transform 0.06s ease, box-shadow 0.08s ease, background 0.08s ease,
    border-color 0.1s ease;
}

.keycap:hover {
  border-color: var(--key-hover-border);
}

.keycap.is-trans {
  opacity: 0.65;
  border-style: dashed;
}

.keycap.is-trans .keycap-tap {
  color: var(--text-dim);
  font-style: italic;
}

.keycap.is-none {
  opacity: 0.35;
  border-style: dotted;
}

.keycap.is-selected {
  outline: 2px solid rgba(59, 130, 246, 0.75);
  outline-offset: 1px;
}

/* Tested coverage state */
.keycap.is-tested {
  border-color: var(--key-tested-border);
  background: var(--key-tested-bg);
}

.keycap-tested-dot {
  position: absolute;
  top: 4px;
  right: 4px;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--ok);
  opacity: 0;
  transform: scale(0.5);
  transition: opacity 0.12s, transform 0.12s;
}

.keycap.is-tested .keycap-tested-dot {
  opacity: 1;
  transform: scale(1);
}

/* Live Active Pressed State */
.keycap.is-active {
  background: linear-gradient(180deg, #06b6d4 0%, #0284c7 100%);
  border-color: #67e8f9;
  color: #ffffff;
  box-shadow: 0 1px 0 var(--key-shadow), 0 0 18px rgba(6, 182, 212, 0.7);
  z-index: 5;
}

.keycap.is-hold-active {
  background: linear-gradient(180deg, #f59e0b 0%, #d97706 100%);
  border-color: #fde68a;
  color: #111827;
  box-shadow: 0 1px 0 var(--key-shadow), 0 0 16px rgba(245, 158, 11, 0.65);
  z-index: 4;
}

.keycap-idx {
  position: absolute;
  top: 3px;
  left: 4px;
  font-size: 9px;
  color: var(--text-dim);
  line-height: 1;
}

.keycap-tap {
  font-size: 13px;
  font-weight: 700;
  line-height: 1.1;
  text-align: center;
  padding: 0 3px;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.keycap-tap.is-small {
  font-size: 10px;
}

.keycap-hold {
  position: absolute;
  bottom: 3px;
  right: 4px;
  font-size: 9.5px;
  font-weight: 600;
  color: var(--key-hold-text);
  background: var(--key-hold-bg);
  padding: 0 3px;
  border-radius: 3px;
  line-height: 1.2;
}

.keycap.is-active .keycap-hold,
.keycap.is-hold-active .keycap-hold {
  color: #fff;
  background: rgba(0, 0, 0, 0.3);
}

/* Stage Footer */
.stage-footer {
  display: flex;
  flex-direction: column;
  gap: 6px;
  flex-shrink: 0;
}

.mods-wrap {
  display: flex;
  align-items: center;
  gap: 8px;
  height: 28px;
  min-height: 28px;
  max-height: 28px;
  overflow: hidden;
}

.mods-title {
  font-size: 12px;
  color: var(--text-dim);
  white-space: nowrap;
  flex-shrink: 0;
}

.active-mods-bar {
  display: flex;
  flex-wrap: nowrap;
  align-items: center;
  gap: 6px;
  height: 26px;
  overflow: hidden;
}

.mod-pill {
  font-size: 11.5px;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 6px;
  background: var(--accent-bg);
  border: 1px solid rgba(6, 182, 212, 0.5);
  color: var(--accent-text);
  white-space: nowrap;
  flex-shrink: 0;
}

.lock-hint {
  font-size: 11.5px;
  color: var(--text-dim);
  line-height: 1.45;
}

/* Sidebar */
.side-panel {
  display: flex;
  flex-direction: column;
  gap: 12px;
  height: 100%;
  min-height: 0;
  overflow: hidden;
}

.side-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  flex-shrink: 0;
}

.side-card h3 {
  margin: 0;
  font-size: 14px;
  font-weight: 700;
}

.side-card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.inspector-card {
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  gap: 6px;
  font-size: 12.5px;
  height: 148px;
  overflow: hidden;
}

.insp-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  height: 22px;
  flex-shrink: 0;
}

.insp-row span {
  color: var(--text-dim);
  white-space: nowrap;
}

.insp-row strong {
  max-width: 175px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}

.insp-row code {
  background: var(--bg);
  border: 1px solid var(--border);
  padding: 2px 6px;
  border-radius: 5px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 11.5px;
  color: var(--code-text);
  max-width: 175px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.insp-sep {
  border: none;
  border-top: 1px solid var(--border);
  margin: 2px 0;
  flex-shrink: 0;
}

.tag {
  font-size: 11px;
  font-weight: 600;
  padding: 2px 7px;
  border-radius: 999px;
}

.tag-ok {
  background: rgba(16, 185, 129, 0.15);
  color: var(--ok-text);
}

.tag-warn {
  background: rgba(245, 158, 11, 0.15);
  color: var(--warn-text);
}

.side-log-card {
  flex: 1;
  min-height: 180px;
  overflow: hidden;
}

.event-log-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
  flex: 1;
  min-height: 0;
  overflow-y: auto;
}

.log-item {
  display: grid;
  grid-template-columns: 56px minmax(0, 1fr) 68px;
  gap: 8px;
  align-items: center;
  height: 26px;
  flex-shrink: 0;
  padding: 0 8px;
  border-radius: 6px;
  background: var(--bg);
  font-size: 11.5px;
}

.log-item.is-matched {
  border-left: 2px solid var(--accent);
}

.log-time {
  color: var(--text-dim);
  font-family: ui-monospace, monospace;
  font-variant-numeric: tabular-nums;
}

.log-key {
  font-weight: 700;
  color: var(--text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.log-match {
  color: var(--ok-text);
  font-family: ui-monospace, monospace;
  text-align: right;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Modal */
.modal {
  position: fixed;
  inset: 0;
  background: var(--modal-backdrop);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 50;
}

.modal-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  width: 100%;
  max-width: 680px;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.modal-head h3 {
  margin: 0;
  font-size: 16px;
}

.raw-textarea {
  width: 100%;
  min-height: 280px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  padding: 10px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12px;
  resize: vertical;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
}
