@font-face {
  font-family: 'combo';
  src: url('Fonts/Inter.ttf') format('truetype');
}

@font-face {
  font-family: 'mono';
  src: url('Fonts/ShareTechMono-Regular.ttf') format('truetype');
}

:root {
  --bg: #111214;
  --surface: #191a1d;
  --panel: #222327;
  --panel-raised: #2b2c31;
  --line: #3d3f46;
  --line-strong: #575a63;
  --text: #f4f4f5;
  --dim: #a8abb3;
  --accent: #ff4f8b;
  --accent-soft: rgba(255, 79, 139, .16);
  --success: #35b86b;
  --warning: #e4ad45;
  --error: #f05d62;
  --tap: #ff4f8b;
  --slide: #45b8d8;
  --hold: #f2cb4f;
  --touch: #54d79d;
  --brk: #f19a45;
  --mine: #8a8d96;
  --play: #ff4f8b;
}

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

[hidden] {
  display: none !important;
}

html,
body {
  width: 100%;
  min-width: 320px;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: 'combo', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans TC', sans-serif;
}

body {
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  padding: 16px;
}

button,
input,
select {
  font: inherit;
}

button,
label,
input[type='range'] {
  touch-action: manipulation;
}

button:focus-visible,
input:focus-visible,
select:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.eyebrow {
  display: block;
  color: var(--accent);
  font-size: 10px;
  font-weight: 800;
  line-height: 1.2;
}

.btn-primary,
.btn-secondary {
  min-height: 42px;
  border: 1px solid transparent;
  border-radius: 6px;
  padding: 9px 14px;
  color: #fff;
  font-weight: 750;
  cursor: pointer;
  transition: background-color .12s ease, border-color .12s ease, opacity .12s ease;
}

.btn-primary {
  background: var(--success);
  border-color: var(--success);
}

.btn-primary:hover:not(:disabled) {
  background: #2fa35f;
  border-color: #2fa35f;
}

.btn-secondary {
  background: transparent;
  border-color: var(--line-strong);
  color: var(--text);
}

.btn-secondary:hover:not(:disabled) {
  background: var(--panel-raised);
}

button:disabled {
  cursor: not-allowed;
  opacity: .42;
}

/* Chart picker */
.picker-overlay {
  position: fixed;
  inset: 0;
  z-index: 120;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: rgba(10, 10, 12, .94);
}

.picker-panel {
  width: min(460px, 100%);
  max-height: min(660px, calc(100vh - 48px));
  overflow: visible;
  padding: 22px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 18px 50px rgba(0, 0, 0, .48);
}

.picker-heading {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 20px;
}

.picker-mark {
  display: grid;
  place-items: center;
  flex: 0 0 40px;
  height: 40px;
  border-radius: 6px;
  background: var(--accent);
  color: #fff;
  font-size: 21px;
  font-weight: 800;
}

.picker-heading h2 {
  margin-top: 3px;
  font-size: 18px;
  line-height: 1.25;
}

.picker-heading p {
  margin-top: 4px;
  color: var(--dim);
  font-size: 12px;
  line-height: 1.4;
}

.picker-form {
  display: grid;
  gap: 10px;
}

.picker-form > label,
.picker-fieldset legend,
.layout-fieldset legend {
  color: #d9dade;
  font-size: 12px;
  font-weight: 700;
}

.chart-search-wrap {
  position: relative;
}

#chartSearch,
#chartDifficulty {
  width: 100%;
  height: 42px;
  border: 1px solid var(--line-strong);
  border-radius: 6px;
  outline: none;
  background: #151619;
  color: var(--text);
  padding: 0 12px;
  font-size: 14px;
}

#chartSearch:focus,
#chartDifficulty:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent-soft);
}

#chartSearch::placeholder {
  color: #7f828b;
}

.chart-results {
  position: absolute;
  top: calc(100% + 4px);
  right: 0;
  left: 0;
  z-index: 130;
  max-height: 218px;
  overflow-y: auto;
  background: var(--panel);
  border: 1px solid var(--line-strong);
  border-radius: 6px;
  box-shadow: 0 12px 28px rgba(0, 0, 0, .48);
}

.chart-result {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  width: 100%;
  border: 0;
  border-bottom: 1px solid var(--line);
  background: transparent;
  color: var(--text);
  padding: 10px 12px;
  text-align: left;
  font-size: 13px;
  line-height: 1.35;
  cursor: pointer;
}

.chart-result:last-child {
  border-bottom: 0;
}

.chart-result > span:first-child {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chart-result:hover,
.chart-result:focus-visible {
  background: var(--panel-raised);
  outline: none;
}

.chart-result-types {
  flex: 0 0 auto;
  color: var(--accent);
  font-size: 10px;
  font-weight: 800;
}

.chart-results-empty {
  padding: 12px;
  color: var(--dim);
  font-size: 12px;
}

.picker-fieldset,
.layout-fieldset {
  min-width: 0;
  border: 0;
}

.picker-fieldset legend,
.layout-fieldset legend {
  margin-bottom: 7px;
}

.type-segments,
.layout-segments {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  overflow: hidden;
  border: 1px solid var(--line-strong);
  border-radius: 6px;
  background: #17181b;
}

.type-segments {
  height: 40px;
}

.type-segments label,
.layout-segments label {
  position: relative;
  min-width: 0;
  cursor: pointer;
}

.type-segments label + label,
.layout-segments label + label {
  border-left: 1px solid var(--line-strong);
}

.type-segments input,
.layout-segments input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.type-segments span,
.layout-segments span {
  display: grid;
  place-items: center;
  height: 100%;
  min-width: 0;
  color: var(--dim);
  font-size: 13px;
  font-weight: 700;
  text-align: center;
}

.type-segments input:checked + span,
.layout-segments input:checked + span {
  background: var(--panel-raised);
  color: #fff;
  box-shadow: inset 0 -2px var(--accent);
}

.type-segments input:focus-visible + span,
.layout-segments input:focus-visible + span {
  outline: 2px solid var(--accent);
  outline-offset: -2px;
}

.picker-error {
  min-height: 18px;
  color: var(--error);
  font-size: 12px;
  line-height: 1.4;
}

.picker-retry {
  width: 100%;
}

/* Main player */
.container {
  display: flex;
  flex-direction: column;
  gap: 14px;
  width: min(1040px, 100%);
  height: min(720px, calc(100vh - 32px));
  min-height: 520px;
  overflow: hidden;
  padding: 18px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 16px 40px rgba(0, 0, 0, .34);
}

.app-header {
  position: relative;
  flex: 0 0 auto;
  width: 100%;
  min-height: 52px;
  padding: 0 54px 10px;
  border-bottom: 1px solid var(--line);
  text-align: center;
}

.title-group {
  min-width: 0;
}

.app-header h1 {
  overflow: hidden;
  margin-top: 3px;
  color: #fff;
  font-size: 18px;
  font-weight: 750;
  line-height: 1.3;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.status-connecting,
.status-ready,
.status-error {
  margin-top: 3px;
  font-size: 11px;
}

.status-connecting {
  color: var(--warning);
}

.status-ready {
  color: var(--success);
}

.status-error {
  color: var(--error);
}

.app-header .sub {
  display: none;
}

#settingsMenu {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 30;
}

.icon-button {
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--panel);
  color: var(--text);
  font-size: 17px;
  cursor: pointer;
}

.icon-button:hover:not(:disabled) {
  background: var(--panel-raised);
  border-color: var(--accent);
}

.btn-retry {
  min-height: 34px;
  margin: 7px auto 0;
  padding: 5px 12px;
  font-size: 12px;
}

.main-content {
  display: grid;
  grid-template-columns: minmax(400px, 1.08fr) minmax(360px, .92fr);
  gap: 20px;
  flex: 1;
  min-height: 0;
  width: 100%;
}

.left-panel,
.right-panel {
  min-width: 0;
  min-height: 0;
}

.left-panel {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}

#hud {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  flex: 0 0 auto;
  width: 100%;
  color: var(--dim);
  font-size: 11px;
  font-variant-numeric: tabular-nums;
}

#hud b {
  color: var(--text);
  font-weight: 700;
}

.player-row {
  display: flex;
  align-items: stretch;
  justify-content: center;
  gap: 8px;
  flex: 1;
  width: 100%;
  min-height: 0;
}

#stage {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  flex: 1 1 auto;
  min-width: 0;
  min-height: 0;
}

#chartCanvas {
  display: block;
  width: auto;
  max-width: 100%;
  height: 100%;
  max-height: 100%;
  aspect-ratio: 1 / 1;
  background: #0e0f11;
  border: 1px solid var(--line);
  border-radius: 8px;
}

.nav-col {
  display: flex;
  flex: 0 0 56px;
  flex-direction: column;
  gap: 6px;
  width: 56px;
}

.nav-col button {
  flex: 1;
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--panel);
  color: var(--text);
  padding: 7px 2px;
  font-size: 11px;
  text-align: center;
  white-space: nowrap;
  cursor: pointer;
  transition: background-color .12s ease, border-color .12s ease;
}

.nav-col button:hover:not(:disabled) {
  background: var(--panel-raised);
  border-color: var(--line-strong);
}

.nav-col .btn-play {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  font-size: 14px;
  font-weight: 800;
}

.nav-col .btn-play:hover:not(:disabled) {
  background: #e9447d;
  border-color: #e9447d;
}

.right-panel {
  display: flex;
  flex-direction: column;
  gap: 8px;
  overflow-y: auto;
  scrollbar-color: var(--line-strong) transparent;
}

#timeline {
  width: 100%;
  min-width: 0;
}

#densityWrap {
  position: relative;
  cursor: pointer;
}

#densityCanvas {
  display: block;
  width: 100%;
  height: 50px;
  border-bottom: 1px solid var(--line);
  border-radius: 4px;
  background: #151619;
}

#timeLabels,
#measureTicks {
  display: flex;
  justify-content: space-between;
  color: var(--dim);
  font-size: 9px;
  font-variant-numeric: tabular-nums;
}

#timeLabels {
  margin: 3px 2px 4px;
}

#measureTicks {
  margin-top: 1px;
}

#timeLabels span,
#measureTicks span {
  flex: 1;
  text-align: center;
}

#timeLabels span:first-child,
#measureTicks span:first-child {
  text-align: left;
}

#timeLabels span:last-child,
#measureTicks span:last-child {
  text-align: right;
}

#measureSlider,
.speedbox input[type='range'] {
  appearance: none;
  background: transparent;
  cursor: pointer;
}

#measureSlider {
  display: block;
  width: 100%;
  height: 22px;
}

#measureSlider::-webkit-slider-runnable-track,
.speedbox input[type='range']::-webkit-slider-runnable-track {
  height: 5px;
  border-radius: 3px;
  background: #383a40;
}

#measureSlider::-moz-range-track,
.speedbox input[type='range']::-moz-range-track {
  height: 5px;
  border-radius: 3px;
  background: #383a40;
}

#measureSlider::-webkit-slider-thumb,
.speedbox input[type='range']::-webkit-slider-thumb {
  appearance: none;
  width: 16px;
  height: 16px;
  margin-top: -5px;
  border: 3px solid var(--accent);
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 4px rgba(0, 0, 0, .5);
}

#measureSlider::-moz-range-thumb,
.speedbox input[type='range']::-moz-range-thumb {
  width: 12px;
  height: 12px;
  border: 3px solid var(--accent);
  border-radius: 50%;
  background: #fff;
}

#measureSlider.active::-webkit-slider-thumb {
  box-shadow: 0 0 0 3px var(--accent-soft), 0 1px 4px rgba(0, 0, 0, .5);
}

#rangePanel {
  margin-top: 8px;
  padding-top: 12px;
  border-top: 1px solid var(--line);
}

#rangeHeader {
  display: grid;
  grid-template-columns: auto auto 1fr;
  align-items: center;
  gap: 7px;
  margin-bottom: 10px;
  color: var(--dim);
  font-size: 11px;
}

.range-title {
  color: var(--text);
  font-size: 12px;
  font-weight: 800;
}

.limit-badge {
  border: 1px solid #655431;
  border-radius: 999px;
  padding: 2px 6px;
  color: var(--warning);
  font-size: 9px;
  font-weight: 750;
  white-space: nowrap;
}

#rangeLabel {
  min-width: 0;
  color: #e5b8ca;
  font-weight: 700;
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.range-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 25px;
}

.range-row-label {
  flex: 0 0 38px;
  color: var(--dim);
  font-size: 9px;
  line-height: 1.1;
  text-align: right;
}

.range-track {
  position: relative;
  flex: 1;
  min-width: 0;
  height: 22px;
}

.range-track-bg {
  position: absolute;
  top: 50%;
  right: 7px;
  left: 7px;
  height: 5px;
  transform: translateY(-50%);
  border-radius: 3px;
  background: #383a40;
}

.range-track input {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 22px;
  margin: 0;
  appearance: none;
  background: transparent;
  pointer-events: none;
  touch-action: none;
}

.range-track input::-webkit-slider-runnable-track,
.range-track input::-moz-range-track {
  height: 5px;
  margin-top: 8.5px;
  background: transparent;
}

.range-track input::-webkit-slider-thumb {
  appearance: none;
  width: 16px;
  height: 16px;
  margin-top: -5.5px;
  border: 3px solid var(--mine);
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 4px rgba(0, 0, 0, .5);
  cursor: ew-resize;
  pointer-events: auto;
}

.range-track input::-moz-range-thumb {
  width: 12px;
  height: 12px;
  border: 3px solid var(--mine);
  border-radius: 50%;
  background: #fff;
  cursor: ew-resize;
  pointer-events: auto;
}

.range-track input.active::-webkit-slider-thumb {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft), 0 1px 4px rgba(0, 0, 0, .5);
}

.range-track input.active::-moz-range-thumb {
  border-color: var(--accent);
}

.range-tip {
  position: absolute;
  bottom: 100%;
  z-index: 5;
  max-width: 104px;
  overflow: hidden;
  margin-bottom: 5px;
  padding: 2px 5px;
  transform: translateX(-50%);
  border: 1px solid var(--line);
  border-radius: 4px;
  background: var(--panel);
  color: var(--text);
  box-shadow: 0 2px 6px rgba(0, 0, 0, .35);
  font-family: 'mono', monospace;
  font-size: 9px;
  line-height: 1.35;
  text-overflow: ellipsis;
  white-space: nowrap;
  pointer-events: none;
}

.range-tip::after {
  position: absolute;
  top: 100%;
  left: 50%;
  border: 4px solid transparent;
  border-top-color: var(--line);
  content: '';
  transform: translateX(-50%);
}

#rangeEnds,
#rangeActions {
  display: flex;
  gap: 6px;
  margin-top: 8px;
}

#rangeEnds {
  justify-content: space-between;
}

#rangeActions button {
  flex: 1;
}

#rangeEnds button,
#rangeActions button {
  min-height: 34px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--panel);
  color: var(--text);
  padding: 5px 8px;
  font-size: 10px;
  cursor: pointer;
}

#rangeEnds button:hover:not(:disabled),
#rangeActions button:hover:not(:disabled) {
  background: var(--panel-raised);
  border-color: var(--line-strong);
}

.layout-fieldset {
  margin-top: 10px;
}

.layout-segments {
  min-height: 54px;
}

.layout-segments span {
  align-content: center;
  gap: 1px;
  padding: 5px;
}

.layout-segments b {
  font-size: 11px;
}

.layout-segments small {
  overflow: hidden;
  max-width: 100%;
  font-size: 9px;
  font-weight: 550;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.btn-export {
  width: 100%;
  margin-top: 10px;
  font-size: 12px;
}

#legend {
  display: flex;
  justify-content: center;
  gap: 10px;
  flex-wrap: wrap;
  color: var(--dim);
  font-size: 9px;
}

#legend i {
  display: inline-block;
  width: 7px;
  height: 7px;
  margin-right: 4px;
  border-radius: 2px;
}

.app-footer {
  flex: 0 0 auto;
  width: 100%;
  min-height: 18px;
}

.message {
  color: var(--dim);
  font-size: 11px;
  font-weight: 600;
  line-height: 1.4;
  text-align: center;
}

.message.success {
  color: var(--success);
}

.message.error {
  color: var(--error);
}

.message.info {
  color: var(--warning);
}

/* Settings popover */
#settingsPanel {
  position: fixed;
  z-index: 60;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 10px;
  width: max-content;
  min-width: 260px;
  max-width: calc(100vw - 24px);
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  box-shadow: 0 12px 30px rgba(0, 0, 0, .5);
}

.speedbox {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--dim);
  font-size: 11px;
}

.speedbox > span:first-child,
.btn-sfx-mode {
  min-width: 104px;
}

.speedbox input[type='range'] {
  flex: 1;
  width: 90px;
  height: 24px;
  touch-action: none;
}

.speedbox #speedVal,
.speedbox #hsVal,
.speedbox #sfxVal {
  min-width: 36px;
  color: var(--text);
  font-variant-numeric: tabular-nums;
}

.btn-sfx-mode {
  min-height: 32px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--panel-raised);
  color: var(--text);
  padding: 5px 7px;
  font-size: 10px;
  cursor: pointer;
}

.sfx-off #sfxSlider,
.sfx-off #sfxVal {
  display: none;
}

/* Confirmation dialog */
.modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 140;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(0, 0, 0, .72);
}

.modal-box {
  display: flex;
  flex-direction: column;
  gap: 9px;
  width: min(460px, 100%);
  max-height: 80vh;
  padding: 18px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  box-shadow: 0 18px 48px rgba(0, 0, 0, .52);
}

.modal-box h3 {
  font-size: 16px;
}

.modal-meta {
  color: var(--dim);
  font-size: 12px;
  line-height: 1.45;
}

.modal-simai-text {
  flex: 1;
  min-height: 0;
  overflow: auto;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #151619;
  color: var(--text);
  font-family: 'mono', monospace;
  font-size: 11px;
  line-height: 1.5;
  white-space: pre-wrap;
  word-break: break-word;
}

.modal-actions {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}

@media (max-width: 860px) {
  body {
    display: block;
    overflow-x: hidden;
    overflow-y: auto;
    padding: 76px 12px max(12px, env(safe-area-inset-bottom, 0px));
  }

  .platform-mobile body {
    padding-top: calc(env(safe-area-inset-top, 24px) + 54px);
  }

  .picker-overlay {
    align-items: flex-start;
    overflow-y: auto;
    padding: calc(env(safe-area-inset-top, 24px) + 60px) 14px max(14px, env(safe-area-inset-bottom, 0px));
  }

  .picker-panel {
    max-height: none;
    padding: 18px;
  }

  .container {
    width: min(540px, 100%);
    height: auto;
    min-height: 0;
    max-height: none;
    margin: 0 auto;
    overflow: visible;
    padding: 14px;
  }

  .main-content {
    display: flex;
    flex-direction: column;
    gap: 16px;
  }

  .player-row {
    flex: none;
    height: min(380px, calc(100vw - 52px));
  }

  .right-panel {
    overflow: visible;
  }

  .nav-col {
    flex-basis: 48px;
    width: 48px;
  }

  #legend,
  #timeLabels,
  #measureTicks {
    display: none;
  }

  #densityCanvas {
    height: 38px;
  }

  #settingsPanel {
    min-width: min(290px, calc(100vw - 24px));
  }

  .range-track,
  .range-track input {
    height: 30px;
  }

  .range-track input::-webkit-slider-runnable-track,
  .range-track input::-moz-range-track {
    margin-top: 12.5px;
  }

  .range-track input::-webkit-slider-thumb {
    width: 20px;
    height: 20px;
    margin-top: -7.5px;
  }

  .range-track input::-moz-range-thumb {
    width: 16px;
    height: 16px;
  }

  #measureSlider {
    height: 32px;
  }

  #measureSlider::-webkit-slider-thumb {
    width: 22px;
    height: 22px;
    margin-top: -8px;
  }

  #rangeEnds button,
  #rangeActions button {
    min-height: 40px;
    font-size: 11px;
  }

  .btn-export {
    min-height: 44px;
  }
}

@media (max-width: 480px) {
  .app-header {
    padding-right: 44px;
    padding-left: 44px;
  }

  .app-header h1 {
    font-size: 15px;
  }

  .icon-button {
    width: 38px;
    height: 38px;
  }

  #hud {
    gap: 4px;
    font-size: 9px;
  }

  .nav-col {
    flex-basis: 43px;
    width: 43px;
  }

  .nav-col button {
    font-size: 10px;
  }

  #rangeHeader {
    grid-template-columns: auto 1fr;
  }

  #rangeLabel {
    grid-column: 1 / -1;
    text-align: left;
  }

  .range-row-label {
    flex-basis: 32px;
  }

  .layout-segments b {
    font-size: 10px;
  }

  .layout-segments small {
    font-size: 8px;
  }
}

@media (max-height: 620px) and (min-width: 861px) {
  .container {
    min-height: 0;
  }

  #densityCanvas {
    height: 36px;
  }

  .range-row {
    margin-top: 21px;
  }

  .layout-segments {
    min-height: 48px;
  }
}
