* {
  box-sizing: border-box;
}

:root {
  --bg: #0f0f12;
  --surface: #1a1a20;
  --text: #e8e8ed;
  --text-muted: #8a8a99;
  --accent: #6366f1;
  --accent-dim: #4f46e5;
  --success: #22c55e;
  --warning: #eab308;
  --danger: #ef4444;
  --amber: #f59e0b;
  --excellent: #3b82f6;
  --safe-top: env(safe-area-inset-top, 0);
  --safe-bottom: env(safe-area-inset-bottom, 0);
}

html.light {
  --bg: #f8fafc;
  --surface: #ffffff;
  --text: #1e293b;
  --text-muted: #64748b;
  --accent: #4f46e5;
  --accent-dim: #4338ca;
}

html {
  -webkit-text-size-adjust: 100%;
}

body {
  font-family: "SF Pro Display", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  line-height: 1.5;
  padding: var(--safe-top) 0 var(--safe-bottom);
}

.container {
  max-width: 720px;
  margin: 0 auto;
  padding: 1rem;
}

@media (min-width: 640px) {
  .container {
    padding: 2rem 1.5rem;
  }
}

header {
  margin-bottom: 2rem;
}

.header-row {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  justify-content: space-between;
  gap: 0.75rem;
}

.header-row > div:first-child {
  flex: 1 1 auto;
  min-width: 0;
}

@media (max-width: 520px) {
  .header-row {
    flex-direction: column;
  }
  header h1 {
    white-space: normal;
  }
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-shrink: 0;
}

.btn-icon {
  background: transparent;
  border: 1px solid rgba(255,255,255,0.2);
  padding: 0.4rem 0.6rem;
  border-radius: 8px;
  font-size: 1.1rem;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.btn-icon:hover {
  background: rgba(255,255,255,0.08);
}

html.light .btn-icon {
  border-color: rgba(0,0,0,0.15);
}

html.light .btn-icon:hover {
  background: rgba(0,0,0,0.05);
}

.btn-docs {
  background: transparent;
  color: var(--accent);
  border: 1px solid rgba(99, 102, 241, 0.4);
  padding: 0.4rem 0.75rem;
  border-radius: 8px;
  font-size: 0.85rem;
  cursor: pointer;
  white-space: nowrap;
  flex-shrink: 0;
  -webkit-tap-highlight-color: transparent;
}

.btn-docs:hover {
  background: rgba(99, 102, 241, 0.15);
}

header h1 {
  font-size: clamp(1.1rem, 2vw, 1.5rem);
  font-weight: 600;
  margin: 0 0 0.25rem 0;
  line-height: 1.2;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
}

.subtitle {
  color: var(--text-muted);
  font-size: 0.8rem;
  margin: 0;
  line-height: 1.4;
}

@media (min-width: 640px) {
  .subtitle {
    font-size: 0.9rem;
  }
}

.test-controls {
  background: var(--surface);
  border-radius: 12px;
  padding: 1.5rem;
  margin-bottom: 1.5rem;
}

.control-row {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  margin-bottom: 1rem;
}

.control-row:last-of-type {
  margin-bottom: 1.25rem;
}

@media (min-width: 640px) {
  .control-row {
    flex-direction: row;
    align-items: center;
    gap: 1rem;
  }
}

.control-row label {
  font-size: 0.9rem;
}

@media (min-width: 640px) {
  .control-row label {
    min-width: 120px;
  }
}

.input-with-presets {
  flex: 1;
  position: relative;
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.presets-dropdown {
  position: relative;
}

.presets-trigger {
  background: transparent;
  border: 1px solid rgba(255,255,255,0.2);
  color: var(--accent);
  font-size: 0.8rem;
  padding: 0.35rem 0.6rem;
  border-radius: 6px;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.presets-trigger:hover {
  background: rgba(99, 102, 241, 0.15);
  border-color: var(--accent);
}

html.light .presets-trigger {
  border-color: rgba(0,0,0,0.2);
}

html.light .presets-trigger:hover {
  background: rgba(79, 70, 229, 0.1);
  border-color: var(--accent);
}

html.light .presets-menu {
  background: var(--surface);
  border-color: rgba(0,0,0,0.15);
}

.presets-menu {
  position: absolute;
  top: 100%;
  left: 0;
  margin-top: 0.25rem;
  padding: 0.35rem 0;
  background: var(--surface);
  border: 1px solid rgba(255,255,255,0.15);
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.4);
  list-style: none;
  min-width: 200px;
  z-index: 50;
}

.presets-menu.hidden {
  display: none;
}

.presets-menu li {
  padding: 0.5rem 0.75rem;
  cursor: pointer;
  font-size: 0.9rem;
}

.presets-menu li:hover {
  background: rgba(99, 102, 241, 0.2);
}

.input-with-datalist {
  flex: 1;
  position: relative;
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
}

.datalist-hint {
  font-size: 0.7rem;
  color: var(--text-muted);
  opacity: 0.7;
}

.control-with-hint {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.probe-hint {
  font-size: 0.75rem;
  opacity: 0.6;
  color: var(--text-muted);
}

.control-row input {
  flex: 1;
  padding: 0.6rem 0.75rem;
  min-height: 44px;
  border: 1px solid rgba(255,255,255,0.1);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
}

@media (min-width: 640px) {
  .control-row input {
    padding: 0.5rem 0.75rem;
    min-height: auto;
    font-size: 0.95rem;
  }
}

.btn-primary {
  background: var(--accent);
  color: white;
  border: none;
  padding: 0.75rem 1.5rem;
  min-height: 48px;
  border-radius: 8px;
  font-size: 1rem;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s;
  -webkit-tap-highlight-color: transparent;
}

.btn-primary:hover {
  background: var(--accent-dim);
}

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

.progress-container {
  margin-top: 1rem;
}

.progress-container.hidden {
  display: none;
}

.btn-secondary {
  background: rgba(99, 102, 241, 0.2);
  color: var(--accent);
  border: 1px solid var(--accent);
  padding: 0.6rem 1rem;
  min-height: 44px;
  border-radius: 8px;
  font-size: 0.9rem;
  cursor: pointer;
  transition: background 0.15s;
  -webkit-tap-highlight-color: transparent;
}

.btn-secondary:hover {
  background: rgba(99, 102, 241, 0.3);
}

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

.status {
  background: var(--surface);
  border-radius: 12px;
  padding: 1.5rem;
  margin-bottom: 1.5rem;
}

.status.hidden {
  display: none;
}

.progress-bar {
  height: 6px;
  background: rgba(255,255,255,0.1);
  border-radius: 3px;
  overflow: hidden;
  margin-top: 1rem;
}

.progress-fill {
  height: 100%;
  background: var(--accent);
  width: 0%;
  transition: width 0.15s ease-out;
}

.results {
  background: var(--surface);
  border-radius: 12px;
  padding: 1.5rem;
}

.results.hidden {
  display: none;
}

.results h2 {
  font-size: 1.1rem;
  font-weight: 600;
  margin: 0 0 1rem 0;
}

.results h2:not(:first-child) {
  margin-top: 1.5rem;
}

.latency-plot-container {
  position: relative;
  margin-bottom: 1rem;
  overflow: visible;
  width: 100%;
  min-height: 180px;
}

.latency-plot-canvas-wrap {
  border-radius: 8px;
  overflow: hidden;
  background: rgba(0, 0, 0, 0.2);
}

.latency-plot-container canvas {
  display: block;
  width: 100%;
  height: 180px;
}

.chart-tooltip {
  position: absolute;
  padding: 0.4rem 0.6rem;
  background: var(--surface);
  border: 1px solid rgba(255,255,255,0.2);
  border-radius: 6px;
  font-size: 0.85rem;
  font-weight: 500;
  white-space: nowrap;
  box-shadow: 0 2px 8px rgba(0,0,0,0.3);
  z-index: 10;
  pointer-events: none;
}

.chart-tooltip.hidden {
  display: none;
}

.speedtest-results {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  margin-top: 1rem;
}

.speedtest-results.hidden {
  display: none;
}

.speedtest-cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 1rem;
}

.speedtest-card {
  padding: 1rem;
  border: 1px solid rgba(255,255,255,0.1);
  border-radius: 8px;
  background: rgba(0,0,0,0.15);
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

html.light .speedtest-card {
  border-color: rgba(0,0,0,0.08);
  background: rgba(0,0,0,0.03);
}

.speedtest-label {
  font-size: 0.8rem;
  color: var(--text-muted);
  text-transform: uppercase;
}

.speedtest-value {
  font-size: 1.5rem;
  font-weight: 700;
}

.speedtest-unit {
  font-size: 0.8rem;
  color: var(--text-muted);
}

.speedtest-bar {
  height: 8px;
  background: rgba(255,255,255,0.1);
  border-radius: 4px;
  overflow: hidden;
  margin-top: 0.25rem;
}

.speedtest-bar-fill {
  height: 100%;
  background: var(--accent);
  width: 0%;
  transition: width 0.5s ease-out;
}

.speedtest-timestamp {
  font-size: 0.8rem;
  color: var(--text-muted);
}

.speedtest-row {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.speedtest-row .label {
  min-width: 80px;
  font-size: 0.9rem;
  color: var(--text-muted);
}

.speedtest-row span:last-child {
  font-size: 1.1rem;
  font-weight: 600;
}

.loss-warning {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  background: rgba(245, 158, 11, 0.2);
  border: 1px solid var(--amber);
  border-radius: 8px;
  padding: 1rem;
  margin-bottom: 1rem;
}

.loss-warning.hidden {
  display: none;
}

.loss-warning-text {
  flex: 1;
  color: var(--text);
  font-size: 0.9rem;
}

.loss-warning-close {
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 1.25rem;
  cursor: pointer;
  padding: 0.25rem;
  line-height: 1;
}

.loss-warning-close:hover {
  color: var(--text);
}

.qoo-summary {
  background: var(--surface);
  border-radius: 12px;
  padding: 1.5rem;
  margin-bottom: 1rem;
}

.qoo-summary.hidden {
  display: none;
}

.qoo-summary-inner {
  text-align: center;
  padding: 1rem;
  border-radius: 12px;
}

.qoo-summary-inner.poor {
  background: rgba(239, 68, 68, 0.25);
  border-left: 4px solid #ef4444;
}
.qoo-summary-inner.fair {
  background: rgba(245, 158, 11, 0.25);
  border-left: 4px solid #f59e0b;
}
.qoo-summary-inner.good {
  background: rgba(34, 197, 94, 0.25);
  border-left: 4px solid #22c55e;
}
.qoo-summary-inner.excellent {
  background: rgba(59, 130, 246, 0.25);
  border-left: 4px solid #3b82f6;
}

.qoo-overall-score {
  display: block;
  font-size: 2.5rem;
  font-weight: 700;
  margin-bottom: 0.25rem;
}

.qoo-overall-grade {
  display: inline-block;
  font-size: 1rem;
  font-weight: 600;
  padding: 0.25rem 0.75rem;
  border-radius: 999px;
  margin-bottom: 0.5rem;
}

.qoo-summary-inner.poor .qoo-overall-grade { background: var(--danger); color: white; }
.qoo-summary-inner.fair .qoo-overall-grade { background: var(--amber); color: #1f2937; }
.qoo-summary-inner.good .qoo-overall-grade { background: var(--success); color: white; }
.qoo-summary-inner.excellent .qoo-overall-grade { background: var(--excellent); color: white; }

.qoo-verdict {
  margin: 0 0 1rem 0;
  font-size: 1rem;
  color: var(--text);
}

.qoo-summary-inner .btn-copy {
  align-self: center;
}

.btn-copy.hidden {
  display: none;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 0.75rem;
  margin-bottom: 1rem;
}

@media (min-width: 480px) {
  .stats-grid {
    grid-template-columns: repeat(3, 1fr);
    gap: 1rem;
  }
}

.stat-card {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  padding: 1rem;
  border: 1px solid rgba(255,255,255,0.1);
  border-radius: 8px;
  background: rgba(0,0,0,0.15);
}

.stat-card-loss-warning {
  border-color: rgba(239, 68, 68, 0.6);
  background: rgba(239, 68, 68, 0.12);
}

html.light .stat-card {
  border-color: rgba(0,0,0,0.08);
  background: rgba(0,0,0,0.03);
}

html.light .stat-card-loss-warning {
  border-color: rgba(239, 68, 68, 0.5);
  background: rgba(239, 68, 68, 0.08);
}

.stat-icon {
  font-size: 0.9rem;
  color: var(--text-muted);
  margin-bottom: 0.15rem;
}

.stat-icon-blank {
  opacity: 0;
  user-select: none;
}

.stat-label {
  font-size: 0.7rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.stat-value {
  font-size: 1.35rem;
  font-weight: 700;
}

.stat-unit {
  font-size: 0.75rem;
  color: var(--text-muted);
}

.stat {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.stat .label {
  font-size: 0.8rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.stat span:last-child {
  font-size: 1.25rem;
  font-weight: 600;
}

.method-note {
  font-size: 0.85rem;
  color: var(--text-muted);
  margin: -0.5rem 0 1rem 0;
}

.qoo-scores {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

@media (max-width: 600px) {
  .qoo-scores {
    gap: 0.5rem;
  }
}

.qoo-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-wrap: wrap;
}

@media (min-width: 640px) {
  .qoo-row {
    gap: 1rem;
  }
}

@media (max-width: 600px) {
  .test-controls .control-row {
    flex-direction: column;
  }
  .test-controls .control-row input,
  .test-controls .control-row .control-with-hint {
    width: 100%;
  }
  .stats-grid {
    grid-template-columns: 1fr;
  }
  .qoo-row {
    display: grid;
    grid-template-columns: 1fr auto;
    grid-template-rows: auto auto;
    gap: 0.5rem 1rem;
    padding: 0.75rem;
    background: rgba(0,0,0,0.15);
    border-radius: 8px;
    border: 1px solid rgba(255,255,255,0.06);
    align-items: center;
  }
  .qoo-row .app-class {
    min-width: 0;
    grid-column: 1;
    grid-row: 1;
  }
  .qoo-row .qoo-score-cell {
    min-width: 0;
    grid-column: 2;
    grid-row: 1;
    justify-content: flex-end;
  }
  .qoo-row .qoo-bar {
    grid-column: 1 / -1;
    grid-row: 2;
    width: 100%;
    min-width: 0;
  }
  html.light .qoo-row {
    background: rgba(0,0,0,0.04);
    border-color: rgba(0,0,0,0.08);
  }
  .latency-plot-container {
    min-height: 160px;
  }
  .latency-plot-container canvas {
    height: 160px;
  }
}

@media (max-width: 400px) {
  .stats-grid {
    grid-template-columns: 1fr;
  }
}

.qoo-row .app-class {
  font-weight: 500;
  min-width: 100px;
  font-size: 0.9rem;
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
}

@media (min-width: 640px) {
  .qoo-row .app-class {
    min-width: 140px;
    font-size: 1rem;
  }
}

.qoo-tooltip-btn {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  border: 1px solid var(--text-muted);
  background: transparent;
  color: var(--text-muted);
  font-size: 0.7rem;
  font-weight: 600;
  cursor: help;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  line-height: 1;
  position: relative;
  flex-shrink: 0;
}

.qoo-tooltip-btn:hover {
  background: rgba(99, 102, 241, 0.2);
  color: var(--accent);
  border-color: var(--accent);
}

.qoo-tooltip-popover {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  bottom: 100%;
  margin-bottom: 6px;
  padding: 0.6rem 0.8rem;
  background: var(--surface);
  border: 1px solid rgba(255,255,255,0.2);
  border-radius: 8px;
  font-size: 0.8rem;
  color: var(--text);
  max-width: 280px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.4);
  z-index: 100;
  white-space: normal;
  line-height: 1.4;
}

.qoo-bar {
  flex: 1;
  min-width: 80px;
  height: 22px;
  background: rgba(255,255,255,0.08);
  border-radius: 999px;
  overflow: hidden;
  display: flex;
}

.qoo-bar-fill {
  height: 100%;
  min-width: 0;
  border-radius: 999px;
  transition: width 0.6s ease-out;
}

.qoo-bar-fill.poor {
  background: linear-gradient(90deg, #f87171, #ef4444);
}
.qoo-bar-fill.fair {
  background: linear-gradient(90deg, #fbbf24, #f59e0b);
}
.qoo-bar-fill.good {
  background: linear-gradient(90deg, #4ade80, #22c55e);
}
.qoo-bar-fill.excellent {
  background: linear-gradient(90deg, #60a5fa, #3b82f6);
}

.qoo-score-cell {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  min-width: 120px;
  justify-content: flex-end;
}

.qoo-score {
  font-weight: 600;
  min-width: 48px;
  text-align: right;
}

.qoo-grade-badge {
  font-size: 0.7rem;
  font-weight: 600;
  padding: 0.2rem 0.5rem;
  border-radius: 999px;
  color: white;
}

.qoo-grade-badge.poor { background: #ef4444; }
.qoo-grade-badge.fair { background: #f59e0b; color: #1f2937; }
.qoo-grade-badge.good { background: #22c55e; }
.qoo-grade-badge.excellent { background: #3b82f6; }

.error {
  background: rgba(239, 68, 68, 0.15);
  border: 1px solid var(--danger);
  border-radius: 12px;
  padding: 1rem;
  color: var(--danger);
}

.error.hidden {
  display: none;
}

footer {
  margin-top: 2rem;
  padding-top: 1.5rem;
  border-top: 1px solid rgba(255,255,255,0.08);
  font-size: 0.85rem;
  color: var(--text-muted);
}

footer p {
  margin: 0 0 0.5rem 0;
}

.dev-info {
  margin-top: 0.5rem;
}

.dev-info summary {
  cursor: pointer;
  color: var(--accent);
  font-size: 0.85rem;
}

.dev-info summary:hover {
  text-decoration: underline;
}

.dev-info pre {
  margin: 0.5rem 0 0 0;
  padding: 0.75rem;
  background: rgba(0,0,0,0.2);
  border-radius: 8px;
  font-size: 0.8rem;
  overflow-x: auto;
}

html.light .dev-info pre {
  background: rgba(0,0,0,0.06);
}

footer code {
  background: rgba(255,255,255,0.1);
  padding: 0.15rem 0.4rem;
  border-radius: 4px;
  font-size: 0.8rem;
}

html.light footer code {
  background: rgba(0,0,0,0.08);
}

/* Docs modal */
.modal {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}

.modal.hidden {
  display: none;
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.7);
  backdrop-filter: blur(4px);
}

.modal-panel {
  position: relative;
  background: var(--surface);
  border-radius: 12px;
  max-width: 720px;
  max-height: 90vh;
  width: 100%;
  display: flex;
  flex-direction: column;
  box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.5);
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 1.25rem;
  border-bottom: 1px solid rgba(255,255,255,0.08);
  flex-shrink: 0;
}

.modal-header h2 {
  margin: 0;
  font-size: 1.1rem;
  font-weight: 600;
}

.modal-close {
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 1.5rem;
  line-height: 1;
  cursor: pointer;
  padding: 0.25rem;
  -webkit-tap-highlight-color: transparent;
}

.modal-close:hover {
  color: var(--text);
}

.modal-body {
  overflow-y: auto;
  padding: 1.25rem;
  -webkit-overflow-scrolling: touch;
}

.modal-body h1 { font-size: 1.25rem; margin: 0 0 0.75rem 0; }
.modal-body h2 { font-size: 1.1rem; margin: 1.25rem 0 0.5rem 0; }
.modal-body h3 { font-size: 1rem; margin: 1rem 0 0.5rem 0; }
.modal-body p { margin: 0 0 0.75rem 0; }
.modal-body ul, .modal-body ol { margin: 0 0 0.75rem 0; padding-left: 1.5rem; }
.modal-body li { margin-bottom: 0.25rem; }
.modal-body code {
  background: rgba(255,255,255,0.1);
  padding: 0.1rem 0.35rem;
  border-radius: 4px;
  font-size: 0.88em;
}
.modal-body pre {
  background: rgba(0, 0, 0, 0.3);
  padding: 1rem;
  border-radius: 8px;
  overflow-x: auto;
  margin: 0.75rem 0;
}
.modal-body pre code {
  background: none;
  padding: 0;
}
.modal-body table {
  width: 100%;
  border-collapse: collapse;
  margin: 0.75rem 0;
  font-size: 0.9rem;
}
.modal-body th, .modal-body td {
  border: 1px solid rgba(255,255,255,0.15);
  padding: 0.5rem 0.75rem;
  text-align: left;
}
.modal-body th {
  background: rgba(0, 0, 0, 0.2);
  font-weight: 600;
}
.modal-body hr {
  border: none;
  border-top: 1px solid rgba(255,255,255,0.1);
  margin: 1.5rem 0;
}
.modal-body a {
  color: var(--accent);
  text-decoration: none;
}
.modal-body a:hover {
  text-decoration: underline;
}

.docs-loading, .docs-error {
  color: var(--text-muted);
  font-style: italic;
}

.docs-error {
  color: var(--danger);
}
