@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;600;700&family=Roboto+Mono:wght@500&display=swap');

:root {
  /* Deep Blue/Purple Gradient */
  --bg-gradient: linear-gradient(135deg, #1e1e2f 0%, #2d2d44 100%);

  /* Glassmorphism Variables */
  --glass-bg: rgba(255, 255, 255, 0.05);
  --glass-border: rgba(255, 255, 255, 0.1);
  --glass-shadow: 0 8px 32px 0 rgba(0, 0, 0, 0.5);
  --glass-blur: blur(12px);

  /* Text Colors */
  --text-main: #ffffff;
  --text-sub: #8b8b9e;

  /* Neon Accents */
  --accent-primary: #00d2ff;
  /* Cyan */
  --accent-secondary: #ff007a;
  /* Pink/Magenta */
  --accent-glow: 0 0 20px rgba(0, 210, 255, 0.6);

  /* Keys */
  --key-white: rgba(255, 255, 255, 0.9);
  --key-black: #0a0a12;
  --key-active-lh: #ff007a;
  /* Left Hand Pink */
  --key-active-rh: #00d2ff;
  /* Right Hand Cyan */
}

* {
  box-sizing: border-box;
}

body {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-height: 100dvh;
  height: 100dvh;
  margin: 0;
  font-family: 'Inter', sans-serif;
  background: var(--bg-gradient);
  color: var(--text-main);
  overflow: hidden;
}

#app {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
}

/* Header */
header {
  width: 100%;
  padding: 1.5rem 3rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
  /* Subtle glass header */
  background: linear-gradient(to bottom, rgba(0, 0, 0, 0.4), transparent);
}

h1 {
  margin: 0;
  font-size: 1.8rem;
  font-weight: 700;
  letter-spacing: -0.5px;
  background: linear-gradient(90deg, #fff, #a0a0b0);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
}

.header-controls a {
  color: var(--text-sub);
  text-decoration: none;
  margin-left: 1rem;
  font-size: 0.9rem;
  transition: color 0.3s, text-shadow 0.3s;
}

.header-controls a:hover {
  color: var(--accent-primary);
  text-shadow: 0 0 10px rgba(0, 210, 255, 0.4);
}

/* Main Container */
#main-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
  height: 100%;
  max-width: none;
  margin-top: 0;
  flex-grow: 1;
}

/* Controls Bar (Pill Shape) */
#control-bar {
  display: flex;
  gap: 1rem;
  margin-bottom: 3rem;
  background: var(--glass-bg);
  backdrop-filter: var(--glass-blur);
  border: 1px solid var(--glass-border);
  padding: 0.8rem 1.5rem;
  border-radius: 100px;
  /* Pill shape */
  box-shadow: var(--glass-shadow);
  align-items: center;
  flex-wrap: nowrap;
  /* Force single line on desktop */
  justify-content: center;
  max-width: 100%;
  overflow-x: auto;
  /* Allow scroll if needed on small desktops */
}

.control-group {
  display: flex;
  align-items: center;
  gap: 0.8rem;
  white-space: nowrap;
  /* Prevent wrapping inside groups */
}

/* Settings Panel (Desktop: Inline, Mobile: Modal) */
#settings-panel {
  display: flex;
  gap: 1.5rem;
  align-items: center;
}

/* Tier 1 & 2 Controls */
#primary-controls,
#secondary-controls {
  display: flex;
  gap: 1.5rem;
  align-items: center;
}

/* Mobile Settings Button (Hidden on Desktop) */
#mobile-settings-btn {
  display: none;
  background: none;
  border: none;
  color: var(--text-sub);
  font-size: 1.5rem;
  cursor: pointer;
}

#mobile-close-settings-btn {
  display: none;
  /* Only for mobile modal */
}

label {
  color: var(--text-sub);
  font-size: 0.85rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 1px;
}

select,
input[type='number'] {
  background: rgba(0, 0, 0, 0.3);
  border: 1px solid var(--glass-border);
  color: white;
  padding: 0.4rem 0.8rem;
  border-radius: 20px;
  font-family: inherit;
  font-size: 0.9rem;
  transition: border-color 0.3s;
}

select:focus,
input:focus {
  outline: none;
  border-color: var(--accent-primary);
}

button {
  font-family: 'Inter', sans-serif;
  font-weight: 600;
  font-size: 0.9rem;
  transition: all 0.2s;
}

button:hover:not(:disabled) {
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
}

button:active:not(:disabled) {
  transform: translateY(0);
}

/* Chord Display Area */
#chord-display {
  display: flex;
  justify-content: space-between;
  width: 100%;
  margin-bottom: 3rem;
  gap: 2rem;
}

.chord-box {
  flex: 1;
  background: var(--glass-bg);
  backdrop-filter: var(--glass-blur);
  border: 1px solid var(--glass-border);
  border-radius: 24px;
  padding: 2.5rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  box-shadow: var(--glass-shadow);
  min-height: 220px;
  transition: transform 0.3s ease, box-shadow 0.3s ease;
  position: relative;
  overflow: hidden;
}

/* Subtle glow behind chord boxes */
.chord-box::before {
  content: '';
  position: absolute;
  top: -50%;
  left: -50%;
  width: 200%;
  height: 200%;
  background: radial-gradient(circle, rgba(255, 255, 255, 0.03) 0%, transparent 60%);
  pointer-events: none;
}

.chord-box:hover {
  transform: translateY(-5px);
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.6);
  border-color: rgba(255, 255, 255, 0.2);
}

.box-label {
  font-size: 0.9rem;
  color: var(--text-sub);
  margin-bottom: 1.5rem;
  text-transform: uppercase;
  letter-spacing: 3px;
  font-weight: 600;
}

.notes-display {
  font-family: 'Roboto Mono', monospace;
  font-size: 1.2rem;
  color: var(--text-sub);
  margin-bottom: 0.5rem;
  min-height: 1.8rem;
  opacity: 0.8;
}

.chord-name {
  font-size: 4.5rem;
  font-weight: 700;
  color: var(--text-main);
  text-shadow: 0 0 30px rgba(255, 255, 255, 0.15);
  min-height: 5.5rem;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1;
}

/* Virtual Keyboard */
#keyboard-area {
  width: 100%;
  height: 180px;
  background: rgba(10, 10, 18, 0.6);
  border-radius: 16px;
  position: relative;
  overflow: hidden;
  margin-bottom: 2rem;
  display: flex;
  justify-content: center;
  box-shadow: inset 0 0 20px rgba(0, 0, 0, 0.8);
  border-top: 4px solid #333;
}

/* Custom Scrollbar for Keyboard */
#keyboard-area::-webkit-scrollbar {
  height: 12px;
}

#keyboard-area::-webkit-scrollbar-track {
  background: rgba(255, 255, 255, 0.05);
  border-radius: 6px;
}

#keyboard-area::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.3);
  border-radius: 6px;
}

#keyboard-area::-webkit-scrollbar-thumb:hover {
  background: rgba(255, 255, 255, 0.5);
}

.key {
  position: relative;
  margin: 0 1px;
  border-radius: 0 0 6px 6px;
  cursor: pointer;
  transition: background-color 0.05s, box-shadow 0.05s, transform 0.05s;
}

.key.white {
  flex: 1;
  height: 100%;
  background: linear-gradient(to bottom, #eee 0%, #ccc 100%);
  z-index: 1;
  box-shadow: inset 0 -5px 10px rgba(0, 0, 0, 0.1);
  min-width: 10px;
}

.key.white:active {
  background: #bbb;
  transform: translateY(2px);
}

.key.black {
  width: calc(100% / var(--white-key-count) * 0.65);
  height: 60%;
  background: linear-gradient(to bottom, #222 0%, #000 100%);
  z-index: 2;
  margin-left: calc(100% / var(--white-key-count) * -0.325);
  margin-right: calc(100% / var(--white-key-count) * -0.325);
  box-shadow: 2px 2px 5px rgba(0, 0, 0, 0.5);
  border-radius: 0 0 4px 4px;
  flex-shrink: 0;
  /* Prevent black keys from shrinking */
}

.key.black:active {
  background: #111;
  transform: translateY(2px);
}

/* Active States (Neon Glow) */
.key.active-lh {
  background: var(--key-active-lh) !important;
  box-shadow: 0 0 20px var(--key-active-lh), 0 0 40px var(--key-active-lh) !important;
  z-index: 10;
}

.key.active-rh {
  background: var(--key-active-rh) !important;
  box-shadow: 0 0 20px var(--key-active-rh), 0 0 40px var(--key-active-rh) !important;
  z-index: 10;
}

/* Stats Modal */
#stats-modal {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.8);
  backdrop-filter: blur(5px);
  z-index: 1000;
  justify-content: center;
  align-items: center;
}

.stats-content {
  background: #1e1e2f;
  width: 90%;
  max-width: 600px;
  border-radius: 16px;
  border: 1px solid rgba(255, 255, 255, 0.1);
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.5);
  padding: 2rem;
  position: relative;
  color: #fff;
}

.stats-close-btn {
  position: absolute;
  top: 1rem;
  right: 1rem;
  background: none;
  border: none;
  color: #888;
  font-size: 2rem;
  cursor: pointer;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 1.5rem;
  margin-top: 2rem;
}

.stat-item {
  background: rgba(255, 255, 255, 0.05);
  border-radius: 10px;
  padding: 1.5rem;
  text-align: center;
  border: 1px solid rgba(255, 255, 255, 0.1);
}

.stat-label {
  font-size: 0.9rem;
  color: #aaa;
  margin-bottom: 0.5rem;
}

.stat-value {
  font-size: 2.5rem;
  font-weight: 700;
  margin-top: 0.5rem;
}

.heatmap-control {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: rgba(255, 255, 255, 0.05);
  padding: 1rem;
  border-radius: 8px;
  border: 1px solid rgba(255, 255, 255, 0.1);
}

/* The switch - the box around the slider */
.switch {
  position: relative;
  display: inline-block;
  width: 60px;
  height: 34px;
}

/* Hide default HTML checkbox */
.switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

/* The slider */
.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #ccc;
  -webkit-transition: .4s;
  transition: .4s;
}

.slider:before {
  position: absolute;
  content: "";
  height: 26px;
  width: 26px;
  left: 4px;
  bottom: 4px;
  background-color: white;
  -webkit-transition: .4s;
  transition: .4s;
}

input:checked+.slider:before {
  -webkit-transform: translateX(26px);
  -ms-transform: translateX(26px);
  transform: translateX(26px);
}

/* Rounded sliders */
.slider.round {
  border-radius: 34px;
}

.slider.round:before {
  border-radius: 50%;
}

/* Chord Rank Chart */
.chord-rank-item {
  display: flex;
  align-items: center;
  margin-bottom: 0.5rem;
  font-size: 0.9rem;
}

.chord-rank-name {
  flex: 0 0 100px;
  /* Fixed width for chord name */
  margin-right: 1rem;
  color: #fff;
}

.chord-rank-bar-container {
  flex-grow: 1;
  background: rgba(255, 255, 255, 0.05);
  border-radius: 4px;
  height: 18px;
  position: relative;
}

.chord-rank-bar {
  height: 100%;
  background: linear-gradient(to right, var(--accent-primary), var(--accent-secondary));
  border-radius: 4px;
  transition: width 0.5s ease-out;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding-right: 5px;
  box-sizing: border-box;
}

.chord-rank-count {
  color: #1e1e2f;
  font-weight: bold;
  font-size: 0.8rem;
  text-shadow: 0 0 2px rgba(0, 0, 0, 0.5);
}

/* --- Mobile/Tablet Responsive Design --- */
@media (max-width: 1023px) {

  /* Header */
  header {
    display: none;
  }

  /* Main Container */
  #main-container {
    width: 100%;
    height: 100dvh;
    margin: 0;
    backdrop-filter: none;
    justify-content: flex-start;
  }

  /* Shared Mobile Styles */
  #primary-controls,
  #secondary-controls {
    display: flex;
    gap: 0.5rem;
  }

  /* Settings Panel (Mobile Modal) */
  #settings-panel {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(20, 20, 30, 0.98);
    backdrop-filter: blur(10px);
    z-index: 2000;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    padding: 2rem;
    box-sizing: border-box;
  }

  #settings-panel.active {
    display: flex;
  }

  #settings-panel .control-group {
    flex-direction: column;
    align-items: center;
    width: 100%;
    margin-bottom: 1.5rem;
    border-left: none !important;
    padding-left: 0 !important;
  }

  #settings-panel select,
  #settings-panel input {
    width: 80%;
    font-size: 1.1rem;
    padding: 0.8rem;
    margin-top: 0.5rem;
    text-align: center;
  }

  #mobile-settings-btn {
    display: block;
    font-size: 1.5rem;
    background: none;
    border: none;
    color: var(--text-sub);
    cursor: pointer;
  }

  #mobile-close-settings-btn {
    display: block;
    position: absolute;
    top: 1.5rem;
    right: 1.5rem;
    background: none;
    border: none;
    color: #fff;
    font-size: 2.5rem;
    cursor: pointer;
    z-index: 2001;
  }

  .mobile-only-header {
    display: block !important;
    font-size: 1.5rem;
    color: var(--text-main);
  }

  /* HUD Chord Display */
  .chord-box {
    background: transparent;
    border: none;
    box-shadow: none;
    padding: 0;
    min-height: auto;
    backdrop-filter: none;
  }

  .chord-box::before {
    display: none;
  }

  .chord-box:hover {
    transform: none;
  }

  .box-label {
    display: none;
  }

  .chord-name {
    font-size: 3.5rem;
    text-shadow: 0 0 20px rgba(255, 255, 255, 0.3);
    min-height: auto;
    line-height: 1.2;
  }

  .notes-display {
    font-size: 1rem;
    margin-bottom: 0;
  }

  #global-arp-indicator {
    font-size: 0.7rem;
  }

  /* Mobile Performance Optimization */
  .chord-box,
  #control-bar,
  #settings-panel {
    backdrop-filter: none !important;
    background: rgba(20, 20, 30, 0.95) !important;
  }

  /* --- Portrait Mode --- */
  @media (orientation: portrait) {
    #app {
      flex-direction: column;
    }

    #keyboard-area {
      overflow-x: auto;
      overflow-y: hidden;
      -webkit-overflow-scrolling: touch;
      justify-content: flex-start;
      border-top: 1px solid var(--glass-border);
      display: flex;
    }

    /* Mobile Portrait (< 768px) */
    @media (max-width: 767px) {
      #control-bar {
        height: 10%;
        width: 100%;
        margin: 0;
        border-radius: 0;
        border: none;
        border-bottom: 1px solid var(--glass-border);
        padding: 0.5rem;
        justify-content: space-between;
      }

      #info-display {
        height: 40%;
        width: 100%;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
      }

      #chord-main {
        width: 100%;
        height: 100%;
        margin: 0;
        display: flex;
        align-items: center;
        justify-content: center;
      }

      #analysis-log {
        display: none;
      }

      #keyboard-area {
        height: 50%;
        width: 100%;
        margin: 0;
        border-radius: 0;
        flex-grow: 1;
        /* Ensure it fills remaining space */
      }

      .key.white {
        min-width: calc(100vw / 7) !important;
        flex: 0 0 calc(100vw / 7) !important;
        height: 100%;
      }

      .key.black {
        width: calc((100vw / 7) * 0.6) !important;
        margin-left: calc((100vw / 7) * -0.3) !important;
        margin-right: calc((100vw / 7) * -0.3) !important;
      }
    }

    /* Tablet Portrait (iPad) */
    @media (min-width: 768px) {
      #control-bar {
        height: auto;
        min-height: 80px;
        width: 100%;
        margin: 0;
        border-radius: 0;
        border-bottom: 1px solid var(--glass-border);
        padding: 1rem 2rem;
        justify-content: space-between;
        background: var(--glass-bg);
      }

      #info-display {
        height: 40%;
        width: 100%;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        padding: 2rem;
      }

      #chord-main {
        width: 100%;
        height: 100%;
        display: flex;
        align-items: center;
        justify-content: center;
      }

      .chord-box {
        background: var(--glass-bg);
        backdrop-filter: var(--glass-blur);
        border: 1px solid var(--glass-border);
        box-shadow: var(--glass-shadow);
        padding: 2rem;
        min-height: 200px;
        width: 80%;
      }

      .chord-name {
        font-size: 4rem;
      }

      #analysis-log {
        display: none;
      }

      #keyboard-area {
        height: 40%;
        /* Balanced height */
        width: 100%;
        margin: 0;
        border-radius: 0;
        flex-grow: 1;
        overflow-x: auto;
        border-top: 1px solid var(--glass-border);
        display: flex;
      }

      .key.white {
        min-width: calc(100vw / 14) !important;
        /* ~2 Octaves */
        flex: 0 0 calc(100vw / 14) !important;
        height: 100%;
      }

      .key.black {
        width: calc((100vw / 14) * 0.6) !important;
        margin-left: calc((100vw / 14) * -0.3) !important;
        margin-right: calc((100vw / 14) * -0.3) !important;
      }
    }
  }

  /* --- Landscape Mode --- */
  @media (orientation: landscape) {
    #control-bar {
      height: 15dvh;
      width: 100%;
      margin: 0;
      border-radius: 0;
      border: none;
      border-bottom: 1px solid var(--glass-border);
      padding: 0.5rem;
      justify-content: space-between;
    }

    #info-display {
      display: none;
    }

    #keyboard-area {
      height: 85dvh;
      width: 100%;
      margin: 0;
      border-radius: 0;
      overflow: hidden;
      border-top: 1px solid var(--glass-border);
      justify-content: flex-start;
    }

    #keyboard-area::-webkit-scrollbar {
      display: none;
    }

    .key.white {
      width: calc(100vw / 10) !important;
      min-width: calc(100vw / 10) !important;
      flex: 0 0 calc(100vw / 10) !important;
      height: 100%;
    }

    .key.black {
      width: calc((100vw / 10) * 0.6) !important;
      margin-left: calc((100vw / 10) * -0.3) !important;
      margin-right: calc((100vw / 10) * -0.3) !important;
      height: 60%;
    }

    #settings-panel {
      justify-content: flex-start;
      padding-top: 4rem;
      height: 15dvh !important;
      width: 100% !important;
      margin: 0 !important;
      border-radius: 0 !important;
      border: none !important;
      border-bottom: 1px solid var(--glass-border) !important;
      padding: 0.5rem !important;
      justify-content: space-between !important;
      margin-bottom: 0 !important;
    }
  }
}

/* --- Encyclopedia Styles (Global) --- */
.encyclopedia-group {
  margin-bottom: 2rem;
}

.encyclopedia-group h3 {
  color: var(--accent-primary);
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
  padding-bottom: 0.5rem;
  margin-bottom: 1rem;
  text-align: center;
  transition: all 0.3s ease;
  border: 1px solid transparent;
}

.encyclopedia-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(80px, 1fr));
  gap: 0.8rem;
}

.enc-card {
  background: rgba(255, 255, 255, 0.05);
  border-radius: 8px;
  padding: 0.8rem 0.5rem;
  text-align: center;
  transition: all 0.3s ease;
  border: 1px solid transparent;
  min-height: 3rem;
  font-size: 0.85rem;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.enc-card.locked {
  opacity: 0.3;
  filter: grayscale(1);
}

.enc-card.unlocked {
  background: rgba(0, 210, 255, 0.1);
  border-color: rgba(0, 210, 255, 0.3);
  box-shadow: 0 0 10px rgba(0, 210, 255, 0.1);
}

.enc-card.unlocked:hover {
  transform: translateY(-2px);
  box-shadow: 0 0 15px rgba(0, 210, 255, 0.3);
}

.enc-name {
  font-weight: bold;
  font-size: 0.9rem;
  margin-bottom: 0.3rem;
  color: #fff;
}

.enc-count {
  font-size: 0.7rem;
  color: #aaa;
}

/* Encyclopedia Modal Overlay */
.modal-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.8);
  backdrop-filter: blur(5px);
  z-index: 3000;
  justify-content: center;
  align-items: center;
}

#encyclopedia-modal .modal-content {
  background: var(--glass-bg);
  padding: 2rem;
  border-radius: 12px;
  width: 95%;
  max-width: 800px;
  max-height: 85vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* --- Desktop Overrides --- */
@media (min-width: 1024px) {
  header {
    display: flex;
  }

  #main-container {
    width: 90%;
    max-width: 1200px;
    height: auto;
    margin-top: 1rem;
    backdrop-filter: none;
    /* Reset */
  }

  #control-bar {
    height: auto;
    width: auto;
    justify-content: center !important;
    align-items: center !important;
    margin-bottom: 0 !important;
    gap: 0 !important;
  }

  #chord-main {
    width: 100% !important;
    height: 100% !important;
    margin: 0 !important;
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
  }

  #analysis-log {
    display: none !important;
  }

  #keyboard-area {
    height: 180px !important;
    width: 100% !important;
    margin: 0 !important;
    border-radius: 16px !important;
    flex-grow: 0 !important;
    overflow-y: hidden !important;
    margin-bottom: 2rem !important;
    border-top: 4px solid #333 !important;
  }
}