:root {
  --primary: #007AFF;
  --primary-dark: #0056CC;
  --success: #34C759;
  --success-dark: #248A3D;
  --danger: #FF3B30;
  --danger-dark: #D70015;
  --warning: #FF9500;
  --bg: #F2F2F7;
  --card: rgba(255, 255, 255, 0.85);
  --card-solid: #FFFFFF;
  --text: #1C1C1E;
  --text-s: #3A3A3C;
  --text-t: #8E8E93;
  --border: rgba(0, 0, 0, 0.08);
  --border-s: rgba(0, 0, 0, 0.15);
  --radius: 16px;
  --radius-s: 12px;
  --radius-xs: 8px;
  --shadow: 0 2px 16px rgba(0,0,0,0.06);
  --shadow-lg: 0 8px 40px rgba(0,0,0,0.10);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --header-h: 56px;
  --bottom-h: 72px;
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}

/* ---- Header ---- */
.app-header {
  position: fixed; top: 0; left: 0; right: 0;
  height: var(--header-h);
  background: rgba(255,255,255,0.8);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  z-index: 100;
}

.app-header h1 {
  font-size: 18px;
  font-weight: 700;
  letter-spacing: -0.3px;
}

.header-actions { display: flex; gap: 8px; align-items: center; }

.header-user {
  font-size: 13px;
  color: var(--text-t);
}

.btn-sm {
  padding: 6px 14px;
  font-size: 13px;
  font-weight: 600;
  font-family: var(--font);
  border: none;
  border-radius: 20px;
  cursor: pointer;
  transition: all 0.15s;
}

.btn-sm:active { transform: scale(0.96); }

.btn-logout {
  background: rgba(255, 59, 48, 0.1);
  color: var(--danger);
}

/* ---- Main Content ---- */
.main-content {
  position: fixed;
  top: var(--header-h);
  bottom: var(--bottom-h);
  left: 0; right: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.tab-panel {
  display: none;
  padding: 20px;
  padding-bottom: 24px;
  max-width: 900px;
  margin: 0 auto;
  animation: fadeIn 0.25s ease;
}

.tab-panel.active { display: block; }

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ---- Bottom Nav ---- */
.bottom-nav {
  position: fixed; bottom: 0; left: 0; right: 0;
  height: var(--bottom-h);
  background: rgba(255,255,255,0.85);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-around;
  padding-bottom: env(safe-area-inset-bottom, 0);
  z-index: 100;
}

.nav-item {
  display: flex; flex-direction: column; align-items: center;
  gap: 2px;
  padding: 6px 6px;
  cursor: pointer;
  border: none;
  background: none;
  font-family: var(--font);
  color: var(--text-t);
  font-size: 9px;
  font-weight: 500;
  transition: color 0.2s;
}

.nav-item.active { color: var(--primary); }

.nav-icon {
  width: 22px; height: 22px;
  display: flex; align-items: center; justify-content: center;
  font-size: 18px;
}

/* ---- Cards ---- */
.card {
  background: var(--card-solid);
  border-radius: var(--radius);
  padding: 24px;
  margin-bottom: 16px;
  box-shadow: var(--shadow);
}

.card-title {
  font-size: 20px;
  font-weight: 700;
  margin-bottom: 16px;
  letter-spacing: -0.3px;
}

.card-subtitle {
  font-size: 13px;
  color: var(--text-t);
  margin-bottom: 16px;
}

/* ---- Form Elements ---- */
.form-group {
  margin-bottom: 16px;
}

.form-label {
  display: block;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-s);
  margin-bottom: 6px;
}

.form-input, .form-textarea {
  width: 100%;
  padding: 12px 14px;
  font-size: 16px;
  font-family: var(--font);
  border: 1.5px solid var(--border);
  border-radius: var(--radius-xs);
  background: var(--bg);
  color: var(--text);
  outline: none;
  transition: border-color 0.2s, box-shadow 0.2s;
}

.form-input:focus, .form-textarea:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(0,122,255,0.12);
}

.form-textarea { resize: vertical; min-height: 60px; }

/* ---- Buttons ---- */
.btn {
  padding: 12px 24px;
  font-size: 15px;
  font-weight: 600;
  font-family: var(--font);
  border: none;
  border-radius: var(--radius-s);
  cursor: pointer;
  transition: all 0.15s;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.btn:active { transform: scale(0.97); }

.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); }

.btn-success { background: var(--success); color: #fff; }
.btn-success:hover { background: var(--success-dark); }

.btn-danger { background: rgba(255,59,48,0.1); color: var(--danger); }
.btn-danger:hover { background: rgba(255,59,48,0.2); }

.btn-ghost {
  background: var(--bg);
  color: var(--text-s);
}

.btn-full { width: 100%; justify-content: center; }

.btn:disabled { opacity: 0.5; cursor: not-allowed; }

/* ---- Upload Area ---- */
.upload-area {
  border: 2px dashed var(--border-s);
  border-radius: var(--radius-s);
  padding: 32px 16px;
  text-align: center;
  cursor: pointer;
  transition: all 0.2s;
  background: var(--bg);
}

.upload-area:hover {
  border-color: var(--primary);
  background: rgba(0,122,255,0.03);
}

.upload-area.has-file {
  border-style: solid;
  border-color: var(--success);
}

.upload-text {
  font-size: 14px;
  color: var(--text-t);
}

.upload-text strong {
  color: var(--primary);
}

.upload-preview {
  max-width: 100%;
  max-height: 200px;
  border-radius: var(--radius-xs);
  margin-top: 12px;
  object-fit: contain;
}

/* ---- Image Preview ---- */
.img-preview {
  width: 100%;
  border-radius: var(--radius-xs);
  object-fit: contain;
  max-height: 300px;
  background: var(--bg);
}

/* ---- Map Editor ---- */
.map-editor-container {
  position: relative;
  width: 100%;
  border-radius: var(--radius-xs);
  overflow: hidden;
  background: var(--bg);
  touch-action: none;
}

.map-editor-container img {
  width: 100%;
  display: block;
  user-select: none;
  -webkit-user-drag: none;
}

.station-marker-editor {
  position: absolute;
  width: 28px; height: 28px;
  background: var(--primary);
  border: 2.5px solid #fff;
  border-radius: 50%;
  transform: translate(-50%, -50%);
  cursor: grab;
  box-shadow: 0 2px 8px rgba(0,0,0,0.25);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  font-weight: 700;
  color: #fff;
  z-index: 10;
  transition: box-shadow 0.15s;
}

.station-marker-editor:active { cursor: grabbing; }
.station-marker-editor.selected {
  background: var(--warning);
  box-shadow: 0 0 0 4px rgba(255,149,0,0.3), 0 2px 8px rgba(0,0,0,0.25);
}

/* Mobile action buttons */
.map-action-buttons {
  animation: fadeIn 0.15s ease;
}

.map-action-buttons .action-btn {
  transition: background 0.15s;
}

.map-action-buttons .action-btn:active {
  background: #F2F2F7;
}

/* Touch device optimizations for map editor */
@media (pointer: coarse) {
  .station-marker-editor {
    width: 36px;
    height: 36px;
    font-size: 13px;
  }
  
  .map-editor-container {
    touch-action: pan-y pinch-zoom;
  }
}

/* ---- Station List ---- */
.station-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px;
  border-radius: var(--radius-xs);
  border: 1.5px solid var(--border);
  margin-bottom: 8px;
  cursor: pointer;
  transition: all 0.15s;
}

.station-item:hover { background: var(--bg); }
.station-item.selected {
  border-color: var(--primary);
  background: rgba(0,122,255,0.05);
}

.station-num {
  width: 28px; height: 28px;
  background: var(--primary);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-size: 13px;
  font-weight: 700;
  flex-shrink: 0;
}

.station-info { flex: 1; min-width: 0; }
.station-name { font-size: 15px; font-weight: 600; }
.station-meta { font-size: 12px; color: var(--text-t); margin-top: 2px; }

/* ---- Station Edit Panel ---- */
.edit-panel {
  background: var(--bg);
  border-radius: var(--radius-s);
  padding: 20px;
  margin-top: 12px;
  animation: fadeIn 0.2s ease;
}

.edit-panel .row {
  display: flex;
  gap: 12px;
}

.edit-panel .row .form-group { flex: 1; }

/* ---- Slider ---- */
.range-group {
  display: flex;
  align-items: center;
  gap: 12px;
}

.range-group input[type=range] {
  flex: 1;
  -webkit-appearance: none;
  height: 6px;
  background: var(--border-s);
  border-radius: 3px;
  outline: none;
}

.range-group input[type=range]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 22px; height: 22px;
  background: var(--primary);
  border-radius: 50%;
  cursor: pointer;
  box-shadow: 0 1px 4px rgba(0,0,0,0.2);
}

.range-value {
  font-size: 14px;
  font-weight: 600;
  color: var(--primary);
  min-width: 50px;
  text-align: right;
}

/* ---- Admin Table ---- */
.admin-table {
  width: 100%;
  border-collapse: collapse;
}

.admin-table th, .admin-table td {
  text-align: left;
  padding: 12px;
  border-bottom: 1px solid var(--border);
  font-size: 14px;
}

.admin-table th {
  font-weight: 600;
  color: var(--text-t);
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.admin-table td .actions {
  display: flex;
  gap: 6px;
}

/* ---- Build Bar ---- */
.build-bar {
  position: fixed;
  bottom: var(--bottom-h);
  left: 0; right: 0;
  padding: 12px 20px;
  background: rgba(255,255,255,0.9);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  z-index: 99;
}

.build-status {
  font-size: 13px;
  color: var(--text-t);
}

/* ---- Toast ---- */
.toast {
  position: fixed;
  top: calc(var(--header-h) + 12px);
  left: 50%;
  transform: translateX(-50%) translateY(-100px);
  background: var(--text);
  color: #fff;
  padding: 12px 24px;
  border-radius: 24px;
  font-size: 14px;
  font-weight: 500;
  z-index: 200;
  transition: transform 0.35s cubic-bezier(0.16, 1, 0.3, 1);
  box-shadow: var(--shadow-lg);
}

.toast.show { transform: translateX(-50%) translateY(0); }
.toast.success { background: var(--success); }
.toast.error { background: var(--danger); }

/* ---- Modal ---- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  backdrop-filter: blur(4px);
  z-index: 150;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.modal-overlay.active { display: flex; }

.modal {
  background: var(--card-solid);
  border-radius: var(--radius);
  padding: 28px;
  width: 100%;
  max-width: 440px;
  box-shadow: var(--shadow-lg);
  animation: modalIn 0.3s cubic-bezier(0.16, 1, 0.3, 1);
}

@keyframes modalIn {
  from { opacity: 0; transform: scale(0.9); }
  to { opacity: 1; transform: scale(1); }
}

.modal-title {
  font-size: 18px;
  font-weight: 700;
  margin-bottom: 20px;
}

.modal-actions {
  display: flex;
  gap: 8px;
  margin-top: 20px;
  justify-content: flex-end;
}

/* ---- Responsive: Desktop ---- */
@media (min-width: 768px) {
  .bottom-nav {
    top: var(--header-h);
    bottom: auto;
    left: 0;
    width: 220px;
    height: auto;
    flex-direction: column;
    justify-content: flex-start;
    padding: 16px 8px;
    gap: 4px;
    border-top: none;
    border-right: 1px solid var(--border);
  }

  .nav-item {
    flex-direction: row;
    width: 100%;
    padding: 12px 16px;
    border-radius: var(--radius-xs);
    font-size: 14px;
    gap: 10px;
    justify-content: flex-start;
  }

  .nav-item.active { background: rgba(0,122,255,0.08); }

  .main-content {
    left: 220px;
    bottom: 0;
  }

  .build-bar {
    left: 220px;
    bottom: 0;
  }

  :root { --bottom-h: 56px; }
}

/* ---- Spinner ---- */
.spinner-inline {
  display: inline-block;
  width: 16px; height: 16px;
  border: 2px solid rgba(0,0,0,0.1);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.6s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

/* Audio status indicator */
.audio-status {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 12px;
  padding: 3px 8px;
  border-radius: 10px;
  background: rgba(52,199,89,0.1);
  color: var(--success);
}

.audio-status.empty {
  background: rgba(142,142,147,0.1);
  color: var(--text-t);
}

/* ---- Audio List ---- */
.audio-list {
  margin-bottom: 12px;
}

.audio-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-radius: var(--radius-xs);
  border: 1.5px solid var(--border);
  margin-bottom: 6px;
  cursor: pointer;
  transition: all 0.15s;
  font-size: 13px;
}

.audio-item:hover { background: rgba(0,0,0,0.02); }
.audio-item.selected {
  border-color: var(--primary);
  background: rgba(0,122,255,0.05);
}

.audio-order {
  width: 22px; height: 22px;
  background: var(--text-t);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-size: 11px;
  font-weight: 700;
  flex-shrink: 0;
}

.audio-item.selected .audio-order { background: var(--primary); }

.audio-info { flex: 1; }

.audio-questions {
  font-size: 11px;
  color: var(--text-t);
  background: var(--bg);
  padding: 3px 8px;
  border-radius: 10px;
}

.badge {
  display: inline-block;
  font-size: 10px;
  padding: 2px 6px;
  border-radius: 8px;
  background: rgba(0,122,255,0.1);
  color: var(--primary);
  margin-left: 6px;
}

/* ---- Section Divider ---- */
.section-divider {
  height: 1px;
  background: var(--border);
  margin: 16px 0;
}

/* ---- Audio Editor ---- */
.audio-editor {
  animation: fadeIn 0.2s ease;
}

.audio-editor-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}

.form-label.small {
  font-size: 12px;
  margin-bottom: 4px;
}

/* ---- Question List ---- */
.question-list {
  margin-bottom: 12px;
}

.question-item {
  padding: 10px 12px;
  border-radius: var(--radius-xs);
  border: 1px solid var(--border);
  margin-bottom: 6px;
  background: #fff;
}

.question-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 13px;
  font-weight: 500;
}

.question-actions {
  display: flex;
  gap: 4px;
}

.question-options {
  margin-top: 6px;
  font-size: 12px;
  color: var(--text-t);
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.question-options span {
  padding: 2px 6px;
  background: var(--bg);
  border-radius: 4px;
}

.question-options span.correct {
  background: rgba(52,199,89,0.15);
  color: var(--success);
  font-weight: 600;
}

/* ---- Empty Hint ---- */
.empty-hint {
  font-size: 13px;
  color: var(--text-t);
  text-align: center;
  padding: 12px;
}

/* ---- Question Modal ---- */
.option-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
}

.option-row input[type="radio"] {
  width: 18px;
  height: 18px;
  cursor: pointer;
}

.option-row .option-input {
  flex: 1;
}

.option-row .btn-sm {
  padding: 4px 8px;
  font-size: 12px;
}

/* ---- Question Type Selector ---- */
.question-type-selector {
  display: flex;
  gap: 16px;
}

.type-option {
  display: flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
  font-size: 14px;
}

.type-option input[type="radio"] {
  width: 16px;
  height: 16px;
  cursor: pointer;
}

/* ---- Keyword Input ---- */
.keyword-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
}

.keyword-row .keyword-input {
  flex: 1;
}

.keyword-row .btn-sm {
  padding: 4px 8px;
  font-size: 12px;
}

/* ---- Question Type Badge ---- */
.question-type-badge {
  display: inline-block;
  font-size: 10px;
  padding: 2px 6px;
  border-radius: 6px;
  background: rgba(0,122,255,0.1);
  color: var(--primary);
  margin-right: 4px;
}

.question-type-badge.short-answer {
  background: rgba(255,149,0,0.1);
  color: var(--warning);
}

.question-type-badge.file-upload {
  background: rgba(52,199,89,0.1);
  color: var(--success);
}

/* ---- Keywords Hint ---- */
.keywords-hint {
  font-style: italic;
  color: var(--warning);
  background: rgba(255,149,0,0.08) !important;
}

/* ---- Status Badges ---- */
.status-badge {
  display: inline-block;
  font-size: 11px;
  font-weight: 600;
  padding: 3px 10px;
  border-radius: 12px;
  text-transform: capitalize;
}

.badge-success {
  background: rgba(52,199,89,0.12);
  color: var(--success);
}

.badge-danger {
  background: rgba(255,59,48,0.1);
  color: var(--danger);
}

.badge-warning {
  background: rgba(255,149,0,0.12);
  color: var(--warning);
}

/* ---- Toggle Switch ---- */
.toggle-switch {
  position: relative;
  display: inline-block;
  width: 48px;
  height: 28px;
  flex-shrink: 0;
}

.toggle-switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.toggle-slider {
  position: absolute;
  inset: 0;
  background: #ccc;
  border-radius: 28px;
  cursor: pointer;
  transition: background 0.3s;
}

.toggle-slider::before {
  content: '';
  position: absolute;
  width: 22px;
  height: 22px;
  left: 3px;
  bottom: 3px;
  background: #fff;
  border-radius: 50%;
  transition: transform 0.3s;
  box-shadow: 0 1px 3px rgba(0,0,0,0.2);
}

.toggle-switch input:checked + .toggle-slider {
  background: var(--primary);
}

.toggle-switch input:checked + .toggle-slider::before {
  transform: translateX(20px);
}

/* ---- Statistics Tab ---- */
.stats-overview {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
}

.stat-card {
  background: linear-gradient(135deg, var(--primary) 0%, var(--primary-dark) 100%);
  border-radius: var(--radius-s);
  padding: 20px 16px;
  text-align: center;
  color: #fff;
}

.stat-number {
  font-size: 32px;
  font-weight: 700;
  line-height: 1.2;
}

.stat-label {
  font-size: 12px;
  opacity: 0.9;
  margin-top: 4px;
}

.stats-station {
  margin-bottom: 24px;
  padding-bottom: 20px;
  border-bottom: 1px solid var(--border);
}

.stats-station:last-child {
  margin-bottom: 0;
  padding-bottom: 0;
  border-bottom: none;
}

.stats-station-name {
  font-size: 18px;
  font-weight: 600;
  color: var(--text);
  margin-bottom: 12px;
}

.stats-audio {
  margin-bottom: 16px;
  padding-left: 12px;
  border-left: 3px solid var(--primary);
}

.stats-audio-title {
  font-size: 14px;
  font-weight: 500;
  color: var(--text-s);
  margin-bottom: 12px;
}

.stats-question {
  background: var(--bg);
  border-radius: var(--radius-xs);
  padding: 12px;
  margin-bottom: 10px;
}

.stats-question:last-child {
  margin-bottom: 0;
}

.stats-question-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 12px;
  margin-bottom: 8px;
}

.stats-question-text {
  font-size: 14px;
  color: var(--text);
  flex: 1;
}

.stats-question-type {
  font-size: 11px;
  background: var(--text-t);
  color: #fff;
  padding: 2px 8px;
  border-radius: 10px;
  white-space: nowrap;
}

.stats-question-data {
  display: flex;
  gap: 16px;
  align-items: center;
}

.stats-count {
  font-size: 13px;
  color: var(--text-s);
}

.stats-accuracy {
  font-size: 13px;
  font-weight: 600;
  padding: 2px 10px;
  border-radius: 12px;
}

.stats-accuracy.high {
  background: rgba(52, 199, 89, 0.15);
  color: var(--success-dark);
}

.stats-accuracy.medium {
  background: rgba(255, 149, 0, 0.15);
  color: #CC7700;
}

.stats-accuracy.low {
  background: rgba(255, 59, 48, 0.15);
  color: var(--danger);
}

.stats-recent {
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px dashed var(--border);
}

.stats-recent-title {
  font-size: 12px;
  color: var(--text-t);
  margin-bottom: 8px;
}

.stats-recent-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.stats-answer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 12px;
  padding: 6px 10px;
  border-radius: 6px;
  background: var(--card-solid);
}

.stats-answer.correct {
  border-left: 3px solid var(--success);
}

.stats-answer.wrong {
  border-left: 3px solid var(--danger);
}

.stats-answer-text {
  color: var(--text-s);
  word-break: break-all;
  max-width: 70%;
}

.stats-answer-time {
  color: var(--text-t);
  font-size: 11px;
  white-space: nowrap;
}

.stats-answer.file-upload-answer {
  flex-direction: column;
  align-items: flex-start;
  gap: 6px;
  border-left: 3px solid var(--primary);
}

.stats-answer-preview {
  width: 100%;
}

.stats-answer-preview img {
  display: block;
  border-radius: 6px;
}

.stats-answer-preview audio {
  display: block;
}

@media (max-width: 480px) {
  .stats-overview {
    grid-template-columns: repeat(3, 1fr);
    gap: 8px;
  }
  
  .stat-card {
    padding: 14px 8px;
  }
  
  .stat-number {
    font-size: 24px;
  }
  
  .stat-label {
    font-size: 10px;
  }
  
  .stats-question-header {
    flex-direction: column;
    gap: 6px;
  }
  
  .stats-question-data {
    flex-wrap: wrap;
    gap: 8px;
  }
}
