:root {
  --bg: #0d1117;
  --bg2: #161b22;
  --bg3: #21262d;
  --border: #30363d;
  --text: #e6edf3;
  --muted: #8b949e;
  --accent: #58a6ff;
  --green: #3fb950;
  --red: #f85149;
  --amber: #d29922;
  --purple: #bc8cff;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: "Segoe UI", system-ui, sans-serif;
  min-height: 100vh;
}

.hidden { display: none !important; }
.spacer { flex: 1; }

#topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 20px;
  background: var(--bg2);
  border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; font-size: 18px; }
#whoami { color: var(--muted); }

.view { padding: 24px; max-width: 1100px; margin: 0 auto; }

/* AUTH */
#view-auth { display: flex; justify-content: center; align-items: center; min-height: calc(100vh - 60px); }
.auth-card {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 32px;
  width: 100%;
  max-width: 400px;
}
.auth-card h1 { font-size: 26px; margin-bottom: 6px; }
.subtitle { color: var(--muted); margin-bottom: 20px; font-size: 14px; }

.tabs { display: flex; gap: 8px; margin-bottom: 16px; }
.tab {
  flex: 1;
  padding: 8px;
  background: var(--bg3);
  color: var(--muted);
  border: 1px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
}
.tab.active { color: var(--text); border-color: var(--accent); }

form label { display: block; margin-bottom: 12px; font-size: 14px; color: var(--muted); }
input {
  width: 100%;
  margin-top: 4px;
  padding: 10px 12px;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 14px;
}
input:focus { outline: none; border-color: var(--accent); }

.btn {
  padding: 10px 16px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg3);
  color: var(--text);
  font-size: 14px;
  cursor: pointer;
  transition: filter 0.15s;
}
.btn:hover { filter: brightness(1.2); }
.btn.primary { background: var(--accent); color: #0d1117; font-weight: 600; border-color: transparent; }
.btn.secondary { background: var(--bg3); color: var(--text); }
.btn.ghost { background: transparent; }
.btn.small { padding: 5px 10px; font-size: 13px; }
.btn.full { width: 100%; }
.btn.toggle.on { background: var(--amber); color: #0d1117; font-weight: 700; border-color: transparent; }
.btn.danger { background: var(--red); color: #fff; border-color: transparent; font-weight: 600; }

.error { color: var(--red); font-size: 13px; margin-bottom: 10px; }

/* LOBBY */
.lobby-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 16px; }
.create-room { display: flex; flex-direction: column; gap: 8px; margin-bottom: 20px; }
.join-by-code { display: flex; gap: 8px; }
.join-by-code input { flex: 1; font-family: "Courier New", monospace; font-size: 18px; letter-spacing: 4px; text-align: center; }
.code-badge {
  display: inline-block;
  background: var(--bg3);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 1px 6px;
  margin-left: 8px;
  font-family: "Courier New", monospace;
  font-size: 13px;
  letter-spacing: 2px;
  color: var(--accent);
  cursor: pointer;
  vertical-align: middle;
}
.code-badge:hover { background: var(--accent); color: var(--bg); }

.rooms-list { list-style: none; display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 12px; }
.rooms-list li {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 16px;
  display: flex;
  align-items: center;
  gap: 10px;
}
.room-name { font-weight: 600; }
.room-meta { color: var(--muted); font-size: 12px; }

.badge {
  font-size: 11px;
  font-weight: 700;
  padding: 3px 8px;
  border-radius: 999px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}
.badge.leader { background: var(--purple); color: #0d1117; }
.badge.live { background: var(--red); color: #fff; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: currentColor; display: inline-block; margin-right: 4px; }

.empty { color: var(--muted); padding: 24px; text-align: center; grid-column: 1 / -1; }

/* STAGE */
.stage-head { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-bottom: 16px; }
.presence { color: var(--muted); font-size: 13px; }

.ping-indicator {
  font-size: 12px;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--bg3);
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
.ping-indicator.ping-good { color: var(--green); background: rgba(63, 185, 80, 0.15); }
.ping-indicator.ping-ok { color: var(--amber); background: rgba(210, 153, 34, 0.15); }
.ping-indicator.ping-bad { color: var(--red); background: rgba(248, 81, 73, 0.15); }

.panels { display: grid; grid-template-columns: 2fr 1.5fr 1.5fr; gap: 12px; margin-bottom: 16px; }
.panel {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 16px;
}
.panel-label { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: 1px; margin-bottom: 6px; }

.chord-big { font-size: 56px; font-weight: 800; line-height: 1.1; color: var(--accent); min-height: 62px; transition: color 0.1s; }
.bass-tonic { font-size: 22px; font-weight: 700; color: var(--amber); }
.chord-roman {
  font-size: 20px;
  font-weight: 700;
  color: var(--purple);
  min-height: 26px;
}
.chord-roman:empty { min-height: 0; }
.key-big { font-size: 28px; font-weight: 700; line-height: 1.2; min-height: 34px; }

.actions-panel { display: flex; flex-direction: column; gap: 10px; }
.midi-inputs { color: var(--muted); font-size: 12px; line-height: 1.5; }
.midi-inputs.midi-error { color: var(--amber); }
.midi-hint { color: var(--muted); font-size: 11px; line-height: 1.5; }
.midi-hint b { color: var(--text); }

/* IOS BANNER */
.ios-banner {
  background: rgba(255, 176, 32, 0.12);
  border: 1px solid var(--amber);
  border-radius: 10px;
  color: var(--amber);
  padding: 10px 14px;
  margin: 0 0 14px;
  font-size: 13px;
  line-height: 1.5;
}
.ios-banner b { color: var(--text); }

/* HELP MODAL */
.modal.help-modal { width: min(720px, 94vw); max-width: 720px; }
.help-md { font-size: 13px; line-height: 1.6; color: var(--text); }
.help-md h2 { font-size: 16px; margin: 18px 0 8px; color: var(--accent, #4af); border-bottom: 1px solid var(--border); padding-bottom: 4px; }
.help-md h3 { font-size: 14px; margin: 14px 0 6px; }
.help-md p { margin: 8px 0; }
.help-md ul, .help-md ol { margin: 8px 0; padding-left: 22px; }
.help-md li { margin: 4px 0; }
.help-md table { width: 100%; border-collapse: collapse; margin: 10px 0; font-size: 12px; }
.help-md th, .help-md td { border: 1px solid var(--border); padding: 6px 8px; text-align: left; vertical-align: top; }
.help-md th { background: var(--bg2); }
.help-md code { background: var(--bg2); border: 1px solid var(--border); border-radius: 4px; padding: 1px 5px; font-size: 11px; }
.help-md blockquote { margin: 10px 0; padding: 8px 12px; border-left: 3px solid var(--accent, #4af); background: var(--bg2); border-radius: 0 6px 6px 0; }
.help-md hr { border: none; border-top: 1px solid var(--border); margin: 14px 0; }

/* KEYBOARD */
.keyboard-wrap {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 18px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  touch-action: pan-x;
}
.keyboard { position: relative; height: 150px; user-select: none; min-width: 900px; }
.key.white {
  position: absolute;
  top: 0;
  bottom: 0;
  background: linear-gradient(#f8f9fa, #dee2e6);
  border: 1px solid #999;
  border-radius: 0 0 6px 6px;
  transition: background 0.05s;
}
.key.black {
  position: absolute;
  top: 0;
  height: 60%;
  background: linear-gradient(#333, #000);
  border-radius: 0 0 5px 5px;
  z-index: 2;
  box-shadow: 0 3px 5px rgba(0,0,0,0.5);
}
.key .key-label {
  position: absolute;
  bottom: 6px;
  left: 0;
  right: 0;
  text-align: center;
  font-size: 11px;
  color: #666;
  pointer-events: none;
}
.key.on.white { background: linear-gradient(#7ee787, var(--green)); }
.key.on.black { background: linear-gradient(var(--green), #1a7f37); }
.key.root.hint-white { background: linear-gradient(#ffd766, var(--amber)); }
.key.root.hint-black { background: linear-gradient(var(--amber), #9e6a03); }
.key.ghost { opacity: 0.25; }

/* INSTRUMENT SELECTOR */
.instrument-bar {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 12px;
}
.instrument-bar .panel-label { margin-bottom: 0; }
.instrument-selector { display: flex; gap: 6px; }
.instrument-btn.active { background: var(--accent); color: #0d1117; font-weight: 700; border-color: transparent; }

/* FRETBOARD */
.fretboard { min-width: 900px; user-select: none; }
.fb-wrap { display: flex; flex-direction: column; }
.fb-row { display: flex; align-items: center; }
.fb-label {
  width: 28px;
  flex-shrink: 0;
  text-align: center;
  font-size: 13px;
  font-weight: 700;
  color: var(--muted);
}
.fb-str-name { color: var(--text); font-size: 14px; }
.fb-nums { margin-bottom: 2px; }
.fb-num {
  flex: 1;
  text-align: center;
  font-size: 11px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}
.fb-string-row {
  position: relative;
  height: 40px;
}
.fb-string-row::before {
  content: "";
  position: absolute;
  top: 50%;
  left: 28px;
  right: 0;
  height: 2px;
  background: #555;
  z-index: 0;
  pointer-events: none;
}
.fb-cell {
  flex: 1;
  height: 100%;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  border-right: 1px solid #333;
  z-index: 1;
}
.fb-cell.fb-nut {
  border-left: 3px solid #aaa;
}
.fb-dot {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  background: rgba(255,255,255,0.04);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  font-weight: 700;
  z-index: 2;
  transition: background 0.05s, color 0.05s;
  pointer-events: none;
}
.fb-cell.active .fb-dot { background: var(--green); color: #0d1117; }
.fb-cell.root .fb-dot { background: var(--amber); color: #0d1117; }
.fb-cell.ghost .fb-dot { background: rgba(255,255,255,0.08); color: var(--muted); }
.fb-markers { height: 20px; }
.fb-marker {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
}
.fb-inlay {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: #333;
}

/* PROGRESSIONS */
.prog-wrap { margin-top: 20px; background: var(--bg2); border: 1px solid var(--border); border-radius: 12px; padding: 16px; }
.prog-log { list-style: none; margin-top: 8px; max-height: 180px; overflow-y: auto; }
.prog-log li {
  font-family: ui-monospace, monospace;
  font-size: 15px;
  padding: 6px 10px;
  border-left: 3px solid var(--amber);
  background: var(--bg3);
  border-radius: 0 6px 6px 0;
  margin-bottom: 6px;
}

/* PREDIÇÃO */
.predict-wrap { margin-top: 12px; background: var(--bg2); border: 1px solid var(--border); border-radius: 12px; padding: 16px; }
.predict-head { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-bottom: 8px; }
.predict-head .panel-label { margin-bottom: 0; }
.pred-confidence {
  font-size: 11px;
  font-weight: 700;
  padding: 2px 8px;
  border-radius: 999px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}
.pred-confidence.high { color: var(--green); background: rgba(63, 185, 80, 0.15); }
.pred-confidence.mid { color: var(--amber); background: rgba(210, 153, 34, 0.15); }
.pred-confidence.low { color: var(--muted); background: var(--bg3); }
.pred-note { color: var(--muted); font-size: 12px; }
.pred-suggestions { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 8px; }
.pred-chip {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 6px 10px;
}
.pred-roman { color: var(--purple); font-weight: 700; font-size: 16px; grid-row: span 1; }
.pred-name { font-weight: 700; font-size: 18px; }
.pred-pct { color: var(--accent); font-weight: 600; font-size: 13px; }
.pred-bar { grid-column: 1 / -1; height: 4px; border-radius: 2px; background: var(--bg3); overflow: hidden; }
.pred-bar-fill { height: 100%; background: var(--accent); transition: width 0.25s; }
.pred-empty { color: var(--muted); font-size: 13px; padding: 6px 0; }
.pred-metrics {
  margin-top: 10px;
  padding: 8px 10px;
  border-radius: 8px;
  background: var(--bg3);
  color: var(--muted);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
}

/* PREVISÃO MELÓDICA / IMPROVISO */
.note-predict-wrap { border-color: rgba(163, 113, 247, 0.35); }
.note-suggestions { grid-template-columns: repeat(auto-fit, minmax(145px, 1fr)); }
.note-chip { grid-template-columns: auto 1fr auto; }
.note-role {
  font-size: 9px;
  font-weight: 700;
  padding: 2px 6px;
  border-radius: 999px;
  text-transform: uppercase;
  letter-spacing: 0.4px;
  white-space: nowrap;
}
.note-role.chord { color: var(--green); background: rgba(63, 185, 80, 0.15); }
.note-role.scale { color: var(--accent); background: rgba(88, 166, 255, 0.15); }
.note-role.chromatic { color: var(--amber); background: rgba(210, 153, 34, 0.15); }
.note-role.outside { color: var(--muted); background: var(--bg3); }
.note-interval { grid-column: 1 / -1; color: var(--muted); font-size: 11px; }

/* HISTÓRICO DO MODELO */
.history-wrap { border-color: rgba(210, 153, 34, 0.35); }
.history-stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
  gap: 8px;
}
.history-card {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
}
.history-card-title {
  color: var(--muted);
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}
.history-card-main {
  display: flex;
  align-items: baseline;
  gap: 6px;
  margin: 4px 0 6px;
  font-variant-numeric: tabular-nums;
}
.history-acc { font-size: 24px; font-weight: 800; color: var(--green); }
.history-acc.low { color: var(--red, #f85149); }
.history-acc.mid { color: var(--amber); }
.history-card-sub { color: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.history-bars { display: flex; flex-direction: column; gap: 4px; margin-top: 6px; }
.history-bar-row { display: flex; align-items: center; gap: 6px; font-size: 10px; color: var(--muted); }
.history-bar-row span:first-child { width: 34px; text-transform: uppercase; letter-spacing: 0.3px; }
.history-bar-track { flex: 1; height: 5px; border-radius: 3px; background: var(--bg3); overflow: hidden; }
.history-bar-hit { height: 100%; background: var(--green); transition: width 0.25s; }
.history-bar-miss { height: 100%; background: var(--red, #f85149); transition: width 0.25s; }
.history-list {
  list-style: none;
  margin: 10px 0 0;
  padding: 0;
  max-height: 260px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.history-item {
  display: grid;
  grid-template-columns: auto auto 1fr auto;
  align-items: center;
  gap: 8px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-left: 3px solid var(--red, #f85149);
  border-radius: 8px;
  padding: 6px 9px;
  font-size: 12px;
  font-variant-numeric: tabular-nums;
}
.history-item.hit { border-left-color: var(--green); }
.history-item.hit3 { border-left-color: var(--amber); }
.history-item.octave { border-left-color: rgba(210, 153, 34, 0.55); }
.history-kind {
  font-size: 9px;
  font-weight: 700;
  padding: 2px 6px;
  border-radius: 999px;
  text-transform: uppercase;
  letter-spacing: 0.4px;
  background: var(--bg3);
  color: var(--muted);
}
.history-kind.chord { color: var(--accent); background: rgba(88, 166, 255, 0.15); }
.history-kind.note { color: var(--purple); background: rgba(163, 113, 247, 0.15); }
.history-verdict { font-weight: 700; font-size: 11px; color: var(--red, #f85149); }
.history-item.hit .history-verdict { color: var(--green); }
.history-item.hit3 .history-verdict { color: var(--amber); }
.history-item.octave .history-verdict { color: var(--amber); opacity: 0.75; }
.history-detail { color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.history-detail b { color: var(--text); font-weight: 700; }
.history-time { color: var(--muted); font-size: 10px; }
.history-empty { color: var(--muted); font-size: 13px; padding: 6px 0; }

/* TOAST */
.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--bg3);
  border: 1px solid var(--amber);
  color: var(--text);
  padding: 12px 20px;
  border-radius: 10px;
  font-size: 14px;
  box-shadow: 0 8px 30px rgba(0,0,0,0.6);
  z-index: 100;
  max-width: 90vw;
}

/* MODAL */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(1, 4, 9, 0.75);
  backdrop-filter: blur(3px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
  animation: fadeIn 0.15s ease-out;
}
@keyframes fadeIn { from { opacity: 0; } }
.modal {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 24px;
  width: min(420px, 92vw);
  max-height: 85vh;
  overflow-y: auto;
  box-shadow: 0 20px 60px rgba(0,0,0,0.7);
}
.modal h3 { font-size: 18px; margin-bottom: 14px; }
.modal-body { display: flex; flex-direction: column; gap: 12px; }
.modal-body p { color: var(--muted); font-size: 14px; line-height: 1.5; }
.modal-body label { margin-bottom: 0; }
.check-row { display: flex; align-items: center; gap: 8px; font-size: 14px; color: var(--text); cursor: pointer; }
.check-row input { width: auto; margin: 0; accent-color: var(--accent); }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; }
.field-hint { color: var(--muted); font-size: 12px; }

@media (max-width: 800px) {
  .view { padding: 16px 12px; }

  #topbar { padding: 8px 12px; gap: 8px; }
  .brand { font-size: 15px; }
  #whoami { font-size: 12px; }

  .lobby-head { flex-wrap: wrap; gap: 8px; }
  .rooms-list { grid-template-columns: 1fr; }

  .stage-head { gap: 6px; }
  .stage-head h2 { font-size: 16px; }
  .presence { font-size: 11px; display: block; width: 100%; order: 10; }
  .ping-indicator { font-size: 11px; }
  #btnCloseRoom, #btnLeave { font-size: 12px; padding: 4px 8px; }

  .panels { grid-template-columns: 1fr; gap: 8px; }
  .panel { padding: 12px; }
  .chord-big { font-size: 40px; min-height: 46px; }
  .bass-tonic { font-size: 18px; }
  .chord-roman { font-size: 16px; min-height: 22px; }
  .key-big { font-size: 22px; min-height: 28px; }

  .instrument-bar { flex-wrap: wrap; gap: 8px; }
  .instrument-selector { gap: 4px; }
  .instrument-btn { font-size: 12px !important; padding: 4px 8px !important; }

  .keyboard-wrap { padding: 10px; overflow-x: auto; -webkit-overflow-scrolling: touch; }
.keyboard { min-width: 600px; height: 120px; }

.fretboard { min-width: 600px; overflow-x: auto; }
  .fb-dot { width: 24px; height: 24px; font-size: 10px; }
  .fb-string-row { height: 32px; }
  .fb-label { width: 22px; font-size: 11px; }
  .fb-string-row::before { left: 22px; }

  .prog-wrap { padding: 12px; }
  .prog-log li { font-size: 13px; }

  .predict-wrap { padding: 12px; }
  .pred-suggestions { grid-template-columns: 1fr; }
  .pred-name { font-size: 16px; }

  .history-item { grid-template-columns: auto auto 1fr; }
  .history-detail { grid-column: 1 / -1; grid-row: 2; white-space: normal; }
  .history-time { grid-column: 3; grid-row: 1; text-align: right; }

  .modal { width: min(360px, 94vw); padding: 20px; }
  .modal h3 { font-size: 16px; }
}

@media (max-width: 480px) {
  .brand { font-size: 14px; }
  .auth-card { padding: 20px; }
  .auth-card h1 { font-size: 22px; }

  .chord-big { font-size: 34px; min-height: 40px; }
  .key-big { font-size: 18px; min-height: 24px; }

  .keyboard { min-width: 480px; height: 100px; }
  .fretboard { min-width: 480px; }
}
