:root {
  --bg: #090d14;
  --surface: #121824;
  --surface-2: #192233;
  --border: #263248;
  --text: #eef2ff;
  --muted: #94a3b8;
  --primary: #6366f1;
  --primary-hover: #4f46e5;
  --success: #10b981;
  --warning: #f59e0b;
  --danger: #ef4444;
  --radius: 12px;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  line-height: 1.45;
}

.hidden {
  display: none !important;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

code {
  background: rgba(99, 102, 241, 0.16);
  padding: 2px 6px;
  border-radius: 6px;
  font-size: 0.9em;
}

/* DS icon */
.ds-icon {
  width: 42px;
  height: 46px;
  border-radius: 8px;
  background: linear-gradient(145deg, #2b354b, #1a2130);
  border: 1px solid #3c4b68;
  padding: 5px;
  display: inline-flex;
  flex-direction: column;
  gap: 5px;
  flex-shrink: 0;
}

.ds-icon.small {
  width: 30px;
  height: 34px;
  padding: 4px;
  gap: 4px;
}

.ds-screen {
  flex: 1;
  background: linear-gradient(135deg, #312e81, #1e1b4b);
  border-radius: 3px;
  border: 1px solid #4f46e5;
}

/* Buttons & inputs */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 40px;
  padding: 8px 14px;
  border-radius: 10px;
  border: 1px solid transparent;
  font-size: 0.92rem;
  font-weight: 600;
  text-decoration: none;
  cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease, transform 0.08s ease;
  user-select: none;
}

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

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

.btn-sm {
  min-height: 34px;
  padding: 5px 10px;
  font-size: 0.84rem;
}

.btn-full {
  width: 100%;
}

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

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

.btn-secondary {
  background: var(--surface-2);
  color: var(--text);
  border-color: var(--border);
}

.btn-secondary:hover:not(:disabled) {
  border-color: #475569;
  background: #202c41;
}

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

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

.btn-danger {
  background: rgba(239, 68, 68, 0.12);
  color: #fca5a5;
  border-color: rgba(239, 68, 68, 0.35);
}

.btn-danger:hover:not(:disabled) {
  background: rgba(239, 68, 68, 0.22);
}

/* Login */
.view-centered {
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: radial-gradient(circle at top, #171e30 0%, var(--bg) 65%);
}

.login-card {
  width: 100%;
  max-width: 400px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 28px;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.45);
}

.brand-badge {
  display: flex;
  align-items: center;
  gap: 14px;
  margin-bottom: 24px;
}

.brand-badge h1 {
  margin: 0;
  font-size: 1.4rem;
}

.subtitle {
  margin: 2px 0 0;
  font-size: 0.88rem;
  color: var(--muted);
}

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

.login-form label {
  font-size: 0.88rem;
  color: var(--muted);
}

.login-form input {
  width: 100%;
  min-height: 44px;
  padding: 10px 14px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 1rem;
}

.login-form input:focus {
  outline: 2px solid var(--primary);
  border-color: transparent;
}

.error-text {
  margin: 0;
  color: #fca5a5;
  font-size: 0.88rem;
}

/* Topbar & Library */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 20px;
  background: rgba(18, 24, 36, 0.92);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}

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

.topbar-title {
  display: block;
  font-size: 1.05rem;
}

.topbar-sub {
  display: block;
  font-size: 0.78rem;
  color: var(--muted);
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.library-main {
  max-width: 1120px;
  margin: 0 auto;
  padding: 20px;
}

.drop-zone {
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  padding: 20px;
  text-align: center;
  background: var(--surface);
  transition: border-color 0.15s ease, background 0.15s ease;
}

.drop-zone.drag-over {
  border-color: var(--primary);
  background: rgba(99, 102, 241, 0.08);
}

.drop-zone p {
  margin: 4px 0;
}

.muted {
  color: var(--muted);
  font-size: 0.88rem;
}

.upload-progress {
  margin-top: 14px;
  text-align: left;
}

.upload-progress-head {
  display: flex;
  justify-content: space-between;
  font-size: 0.86rem;
  margin-bottom: 6px;
}

.progress-track {
  width: 100%;
  height: 10px;
  background: var(--bg);
  border-radius: 999px;
  overflow: hidden;
  border: 1px solid var(--border);
}

.progress-bar {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--primary), #818cf8);
  transition: width 0.12s linear;
}

.library-header-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin: 22px 0 14px;
}

.library-header-row h2 {
  margin: 0;
  font-size: 1.15rem;
}

.pill {
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 0.8rem;
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--muted);
}

.empty-state {
  text-align: center;
  padding: 40px 20px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

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

.rom-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.rom-card-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 8px;
}

.rom-title {
  margin: 0;
  font-size: 1.05rem;
  word-break: break-word;
}

.rom-meta {
  font-size: 0.82rem;
  color: var(--muted);
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.badge-row {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.badge {
  font-size: 0.76rem;
  padding: 3px 8px;
  border-radius: 999px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--muted);
}

.badge.ok {
  color: #6ee7b7;
  border-color: rgba(16, 185, 129, 0.35);
  background: rgba(16, 185, 129, 0.1);
}

.rom-actions-primary {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.rom-actions-primary .btn-play {
  flex: 1;
}

.rom-actions-secondary {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  padding-top: 8px;
  border-top: 1px solid var(--border);
}

.core-select {
  min-height: 34px;
  padding: 4px 8px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  font-size: 0.84rem;
}

/* Player View */
.player-view {
  height: 100dvh;
  display: flex;
  flex-direction: column;
  background: #05070b;
  overflow: hidden;
}

.player-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 6px 10px;
  min-height: 48px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  flex-wrap: wrap;
}

.player-bar-left {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.player-title {
  font-weight: 600;
  font-size: 0.92rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 220px;
}

.player-bar-right {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
}

.sync-pill {
  font-size: 0.78rem;
  padding: 4px 8px;
  border-radius: 999px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--muted);
}

.sync-pill.saving {
  color: #fde68a;
  border-color: rgba(245, 158, 11, 0.4);
}

.sync-pill.saved {
  color: #6ee7b7;
  border-color: rgba(16, 185, 129, 0.4);
}

.sync-pill.error {
  color: #fca5a5;
  border-color: rgba(239, 68, 68, 0.4);
}

.emulator-mount {
  flex: 1;
  width: 100%;
  min-height: 0;
  position: relative;
  background: #05070b;
}

#game {
  width: 100%;
  height: 100%;
}

/* Toasts */
.toast-container {
  position: fixed;
  bottom: 16px;
  right: 16px;
  z-index: 9999;
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-width: min(360px, calc(100vw - 32px));
}

.toast {
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 10px 14px;
  border-radius: 10px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.4);
  font-size: 0.88rem;
}

.toast.error {
  border-color: rgba(239, 68, 68, 0.5);
}

.toast.ok {
  border-color: rgba(16, 185, 129, 0.5);
}

@media (max-width: 640px) {
  .topbar {
    padding: 10px 12px;
  }
  .topbar-sub {
    display: none;
  }
  .library-main {
    padding: 12px;
  }
  .player-title {
    max-width: 120px;
  }
  .player-bar {
    padding: 5px 8px;
  }
}
