:root {
  --bg: #05070d;
  --panel-bg: rgba(12, 17, 28, 0.82);
  --panel-border: rgba(90, 130, 200, 0.18);
  --accent: #4ea3ff;
  --accent-warm: #ff9d4e;
  --text: #dce6f5;
  --text-dim: #8a99b5;
  --good: #57d977;
  --warn: #ffcf5c;
  --bad: #ff5c5c;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  font-size: 13px;
}

#canvas-container {
  position: fixed;
  inset: 0;
  z-index: 0;
}
canvas { display: block; }

/* ---------- Top bar ---------- */
#topbar {
  position: fixed;
  top: 0; left: 0; right: 0;
  height: 44px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  background: linear-gradient(to bottom, rgba(5,7,13,0.9), rgba(5,7,13,0));
  z-index: 10;
  pointer-events: none;
}
.brand { font-weight: 600; letter-spacing: 0.3px; font-size: 15px; }
.brand span { color: var(--text); }
#readouts {
  display: flex;
  gap: 18px;
  font-variant-numeric: tabular-nums;
}
#readouts .ro { text-align: right; }
#readouts .ro .label { font-size: 10px; color: var(--text-dim); text-transform: uppercase; letter-spacing: 0.5px; }
#readouts .ro .val { font-size: 14px; font-weight: 600; }

/* ---------- Side panels ---------- */
aside {
  position: fixed;
  top: 50px;
  bottom: 58px;
  width: 320px;
  background: var(--panel-bg);
  border: 1px solid var(--panel-border);
  border-radius: 10px;
  backdrop-filter: blur(10px);
  z-index: 9;
  overflow-y: auto;
  padding: 10px 14px;
}
#panel { left: 12px; }
#charts-panel { right: 12px; width: 300px; }

aside::-webkit-scrollbar { width: 8px; }
aside::-webkit-scrollbar-thumb { background: rgba(120,150,200,0.25); border-radius: 4px; }

.section {
  margin-bottom: 6px;
  border-bottom: 1px solid rgba(90,130,200,0.10);
  padding-bottom: 8px;
}
.section > .section-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  cursor: pointer;
  user-select: none;
  padding: 8px 2px 6px;
  font-weight: 600;
  font-size: 12px;
  letter-spacing: 0.6px;
  text-transform: uppercase;
  color: var(--accent);
}
.section > .section-head .chevron { color: var(--text-dim); transition: transform 0.15s; }
.section.collapsed > .section-head .chevron { transform: rotate(-90deg); }
.section.collapsed > .section-body { display: none; }

/* ---------- Sliders ---------- */
.control { margin: 9px 0; }
.control .row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 3px;
}
.control .name { color: var(--text); }
.control .value { color: var(--accent); font-variant-numeric: tabular-nums; font-weight: 600; }
.control input[type=range] {
  width: 100%;
  -webkit-appearance: none;
  appearance: none;
  height: 4px;
  border-radius: 2px;
  background: rgba(120,150,200,0.25);
  outline: none;
}
.control input[type=range]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 14px; height: 14px;
  border-radius: 50%;
  background: var(--accent);
  cursor: pointer;
  border: 2px solid #0b1018;
  box-shadow: 0 0 6px rgba(78,163,255,0.6);
}
.control input[type=range]::-moz-range-thumb {
  width: 14px; height: 14px; border-radius: 50%;
  background: var(--accent); cursor: pointer; border: 2px solid #0b1018;
}
.control .effects { font-size: 10.5px; color: var(--text-dim); margin-top: 2px; line-height: 1.35; }

.btn-row { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 4px; }
button.sim-btn {
  flex: 1 1 auto;
  min-width: 80px;
  background: rgba(78,163,255,0.12);
  border: 1px solid rgba(78,163,255,0.35);
  color: var(--text);
  padding: 7px 10px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 12px;
  transition: background 0.12s;
}
button.sim-btn:hover { background: rgba(78,163,255,0.28); }
button.sim-btn.warm { background: rgba(255,157,78,0.12); border-color: rgba(255,157,78,0.4); }
button.sim-btn.warm:hover { background: rgba(255,157,78,0.3); }
button.sim-btn.reset { background: rgba(255,92,92,0.1); border-color: rgba(255,92,92,0.35); }

/* ---------- Time bar ---------- */
#timebar {
  position: fixed;
  bottom: 0; left: 0; right: 0;
  height: 50px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  background: linear-gradient(to top, rgba(5,7,13,0.92), rgba(5,7,13,0));
  z-index: 10;
}
.time-btn {
  background: rgba(20,28,44,0.8);
  border: 1px solid var(--panel-border);
  color: var(--text-dim);
  padding: 6px 12px;
  border-radius: 6px;
  cursor: pointer;
  font-variant-numeric: tabular-nums;
  font-size: 12px;
}
.time-btn.active { color: #fff; border-color: var(--accent); background: rgba(78,163,255,0.25); }
.time-sep { width: 1px; height: 22px; background: var(--panel-border); margin: 0 6px; }
#sim-clock { color: var(--text-dim); margin-left: 14px; font-variant-numeric: tabular-nums; min-width: 220px; }

/* ---------- Charts ---------- */
.charts-title { color: var(--accent); font-weight: 600; font-size: 12px; letter-spacing: 0.6px; padding: 4px 2px 8px; }
.chart {
  margin-bottom: 10px;
}
.chart .chart-head { display: flex; justify-content: space-between; font-size: 11px; margin-bottom: 2px; }
.chart .chart-head .c-name { color: var(--text-dim); }
.chart .chart-head .c-val { font-weight: 600; font-variant-numeric: tabular-nums; }
.chart canvas { width: 100%; height: 46px; display: block; border-radius: 4px; background: rgba(8,12,20,0.6); }

/* ---------- Loader ---------- */
#loader {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 18px;
  background: var(--bg);
  color: var(--text-dim);
  transition: opacity 0.6s;
}
#loader.hidden { opacity: 0; pointer-events: none; }
.spinner {
  width: 48px; height: 48px;
  border: 3px solid rgba(78,163,255,0.2);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.hint {
  position: fixed;
  bottom: 56px; left: 50%;
  transform: translateX(-50%);
  color: var(--text-dim);
  font-size: 11px;
  z-index: 8;
  pointer-events: none;
  opacity: 0.7;
}

.badge { font-size: 10px; padding: 1px 6px; border-radius: 8px; }
.badge.good { background: rgba(87,217,119,0.15); color: var(--good); }
.badge.warn { background: rgba(255,207,92,0.15); color: var(--warn); }
.badge.bad { background: rgba(255,92,92,0.15); color: var(--bad); }

/* ===========================================================================
   RESPONSIVE — tablet & mobile
   Trên màn nhỏ: 2 panel biến thành ngăn kéo (drawer) trượt ra, có nút bật/tắt.
   =========================================================================== */

/* Nút toggle + backdrop: ẩn mặc định (chỉ hiện ở màn nhỏ) */
.panel-toggle { display: none; }
#panel-backdrop { display: none; }

@media (max-width: 900px) {
  html, body { font-size: 13px; }

  /* ----- Top bar: cho phép xuống dòng, thu gọn ----- */
  #topbar {
    height: auto;
    min-height: 44px;
    flex-wrap: wrap;
    row-gap: 2px;
    padding: 6px 10px;
  }
  .brand { font-size: 13px; }
  /* chừa chỗ cho nút toggle bên trái */
  .brand { padding-left: 4px; }
  #readouts { gap: 12px; flex-wrap: wrap; justify-content: flex-end; }
  #readouts .ro .val { font-size: 12px; }
  #readouts .ro .label { font-size: 9px; }

  /* ----- Panels -> drawer trượt từ 2 cạnh ----- */
  aside {
    top: 0;
    bottom: 0;
    width: min(86vw, 360px);
    border-radius: 0;
    padding-top: 54px;
    z-index: 30;
    transition: transform 0.25s ease;
    -webkit-overflow-scrolling: touch;
  }
  #panel {
    left: 0;
    border-left: none;
    transform: translateX(-102%);
  }
  #panel.open { transform: translateX(0); }
  #charts-panel {
    right: 0;
    width: min(86vw, 360px);
    border-right: none;
    transform: translateX(102%);
  }
  #charts-panel.open { transform: translateX(0); }

  /* Lớp nền mờ khi mở drawer */
  #panel-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.45);
    z-index: 29;
  }
  #panel-backdrop.show { display: block; }

  /* ----- Nút nổi bật/tắt panel ----- */
  .panel-toggle {
    position: fixed;
    bottom: 64px;
    z-index: 31;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 46px;
    height: 46px;
    border-radius: 50%;
    background: var(--panel-bg);
    border: 1px solid var(--panel-border);
    color: var(--text);
    font-size: 20px;
    line-height: 1;
    cursor: pointer;
    backdrop-filter: blur(10px);
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.4);
  }
  #toggle-panel { left: 12px; }
  #toggle-charts { right: 12px; }

  /* ----- Time bar: cho xuống dòng ----- */
  #timebar {
    height: auto;
    min-height: 50px;
    flex-wrap: wrap;
    padding: 6px 8px;
    gap: 6px;
  }
  #sim-clock {
    min-width: 0;
    margin-left: 0;
    flex-basis: 100%;
    text-align: center;
    font-size: 11px;
  }

  /* ----- Hint: tránh đè nút toggle ----- */
  .hint { bottom: 116px; font-size: 10px; }
}

/* ----- Điện thoại nhỏ ----- */
@media (max-width: 480px) {
  html, body { font-size: 12px; }
  .brand span { display: none; }            /* chỉ giữ icon 🌍 cho gọn */
  #readouts { gap: 8px; }
  #readouts .ro:nth-child(n+4) { display: none; } /* bớt số liệu khi quá chật */
  .time-btn { padding: 5px 9px; font-size: 11px; }
  .panel-toggle { width: 42px; height: 42px; font-size: 18px; bottom: 60px; }
}

/* ----- Màn ngang & thấp (điện thoại nằm ngang) ----- */
@media (max-height: 430px) and (max-width: 900px) {
  aside { padding-top: 46px; }
  .panel-toggle { bottom: 56px; }
}
