/*
  METCON Generator
  Copyright (C) 2026 Stéphane Fosse

  Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

  3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

  SPDX short identifier: BSD-3-Clause
  Full license text: https://opensource.org/license/bsd-3-clause
*/

  @import url('https://fonts.googleapis.com/css2?family=Bebas+Neue&family=DM+Sans:wght@300;400;500;600&family=DM+Mono:wght@400;500&display=swap');

  :root {
    --bg: #080808;
    --bg-surface: #111111;
    --bg-surface-2: #181818;
    --bg-hover: #1f1f1f;
    --text: #ffffff;
    --text-muted: #e9e9e9;
    --text-dim: #e9e9e9;
    --text-secondary: #888888;
    --accent: #f59e0b;
    --accent-hover: #fbbf24;
    --accent-dim: rgba(245,158,11,0.12);
    --accent-dim2: rgba(245,158,11,0.06);
    --border: #222;
    --border-accent: rgba(245,158,11,0.3);
    --red: #ef4444;
    --green: #22c55e;
    --blue: #3b82f6;
    --radius: 8px;
    --radius-sm: 5px;
    --transition: 0.18s ease;
  }

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

  body {
    background: var(--bg);
    color: var(--text);
    font-family: 'DM Sans', sans-serif;
    font-size: 15px;
    line-height: 1.5;
    min-height: 100vh;
    padding-bottom: 60px;
  }

  /* Subtle grain overlay */
  body::before {
    content: '';
    position: fixed;
    inset: 0;
    background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 256 256' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='noise'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='4' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23noise)' opacity='0.04'/%3E%3C/svg%3E");
    pointer-events: none;
    z-index: 0;
    opacity: 0.5;
  }

  #app {
    position: relative;
    z-index: 1;
    max-width: 480px;
    margin: 0 auto;
    padding-left: max(16px, env(safe-area-inset-left));
    padding-right: max(16px, env(safe-area-inset-right));
    padding-bottom: max(60px, env(safe-area-inset-bottom));
  }

  /* ── HEADER ── */
  header {
    padding: 40px 0 28px;
    border-bottom: 1px solid var(--border);
    margin-bottom: 28px;
  }

  .header-eyebrow {
    font-family: 'DM Mono', monospace;
    font-size: 10px;
    letter-spacing: 0.2em;
    text-transform: uppercase;
    color: var(--accent);
    margin-bottom: 6px;
  }

  h1 {
    font-family: 'Bebas Neue', sans-serif;
    font-size: clamp(52px, 14vw, 72px);
    line-height: 0.9;
    letter-spacing: 0.01em;
    color: var(--text);
    text-transform: uppercase;
    display: flex;
    flex-direction: column;
  }

  h1 .h1-main { color: var(--text); }
  h1 .h1-accent { color: var(--accent); }

  .header-sub {
    margin-top: 10px;
    font-size: 13px;
    color: var(--text-secondary);
    font-weight: 300;
    letter-spacing: 0.01em;
  }

  /* ── SECTIONS ── */
  .section {
    margin-bottom: 20px;
  }

  .section-label {
    font-family: 'DM Mono', monospace;
    font-size: 10px;
    letter-spacing: 0.15em;
    text-transform: uppercase;
    color: var(--text-secondary);
    margin-bottom: 10px;
    display: flex;
    align-items: center;
    gap: 8px;
  }

  .section-label::after {
    content: '';
    flex: 1;
    height: 1px;
    background: var(--border);
  }

  /* ── TOGGLE GROUP ── */
  .toggle-group {
    display: flex;
    gap: 4px;
    background: var(--bg-surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 4px;
  }

  .toggle-btn {
    flex: 1;
    min-height: 44px;
    background: transparent;
    border: none;
    border-radius: var(--radius-sm);
    color: var(--text-muted);
    font-family: 'DM Mono', monospace;
    font-size: 12px;
    font-weight: 500;
    letter-spacing: 0.08em;
    cursor: pointer;
    transition: background var(--transition), color var(--transition);
    text-transform: uppercase;
  }

  .toggle-btn:hover { background: var(--bg-hover); color: var(--text-dim); }

  .toggle-btn.active {
    background: var(--accent);
    color: #000;
    font-weight: 600;
  }

  /* ── SLIDER ROW ── */
  .slider-row {
    display: flex;
    align-items: center;
    gap: 12px;
    background: var(--bg-surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 12px 16px;
  }

  .slider-row label {
    font-size: 13px;
    color: var(--text-dim);
    white-space: nowrap;
    min-width: 60px;
  }

  .slider-row input[type=range] {
    flex: 1;
    min-width: 0;
    -webkit-appearance: none;
    appearance: none;
    height: 4px;
    background: var(--border);
    border-radius: 2px;
    outline: none;
    cursor: pointer;
  }

  .slider-row input[type=range]::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 18px; height: 18px;
    background: var(--accent);
    border-radius: 50%;
    cursor: pointer;
    transition: transform var(--transition), box-shadow var(--transition);
  }

  .slider-row input[type=range]::-webkit-slider-thumb:hover {
    transform: scale(1.2);
    box-shadow: 0 0 0 6px var(--accent-dim);
  }

  .slider-val {
    font-family: 'DM Mono', monospace;
    font-size: 14px;
    font-weight: 500;
    color: var(--accent);
    min-width: 48px;
    flex-shrink: 0;
    white-space: nowrap;
    text-align: right;
  }

  /* ── COLLAPSIBLE OPTIONS ── */
  .options-toggle {
    width: 100%;
    min-height: 44px;
    background: var(--bg-surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    color: var(--text-dim);
    font-family: 'DM Sans', sans-serif;
    font-size: 13px;
    font-weight: 500;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 16px;
    transition: border-color var(--transition), color var(--transition);
    margin-bottom: 0;
  }

  .options-toggle:hover {
    border-color: var(--accent);
    color: var(--text);
  }

  .options-toggle .arrow {
    font-size: 12px;
    transition: transform var(--transition);
    color: var(--text-secondary);
  }

  .options-toggle.open .arrow { transform: rotate(90deg); }

  .options-panel {
    background: var(--bg-surface);
    border: 1px solid var(--border);
    border-top: none;
    border-radius: 0 0 var(--radius) var(--radius);
    padding: 16px;
    display: none;
    flex-direction: column;
    gap: 16px;
    margin-top: -1px;
  }

  .options-panel.open { display: flex; }

  /* ── OPTION ROW ── */
  .opt-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
  }

  .opt-label {
    font-size: 13px;
    color: var(--text-dim);
  }

  /* ── TOGGLE SWITCH ── */
  .switch {
    position: relative;
    width: 44px;
    height: 24px;
    flex-shrink: 0;
  }

  .switch input { opacity: 0; width: 0; height: 0; }

  .switch-track {
    position: absolute;
    inset: 0;
    background: var(--border);
    border-radius: 12px;
    cursor: pointer;
    transition: background var(--transition);
  }

  .switch-track::after {
    content: '';
    position: absolute;
    width: 18px; height: 18px;
    background: var(--text-secondary);
    border-radius: 50%;
    top: 3px; left: 3px;
    transition: transform var(--transition), background var(--transition);
  }

  .switch input:checked + .switch-track { background: var(--accent-dim); border: 1px solid var(--accent); }
  .switch input:checked + .switch-track::after { transform: translateX(20px); background: var(--accent); }

  /* ── MINI TOGGLE GROUP ── */
  .mini-toggle {
    display: flex;
    gap: 3px;
    background: var(--bg-surface-2);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 3px;
  }

  .mini-btn {
    min-height: 32px;
    padding: 0 12px;
    background: transparent;
    border: none;
    border-radius: 4px;
    color: var(--text-muted);
    font-family: 'DM Mono', monospace;
    font-size: 11px;
    font-weight: 500;
    letter-spacing: 0.06em;
    text-transform: uppercase;
    cursor: pointer;
    transition: background var(--transition), color var(--transition);
    white-space: nowrap;
  }

  .mini-btn:hover { background: var(--bg-hover); color: var(--text-dim); }
  .mini-btn.active { background: var(--accent-dim); color: var(--accent); border: 1px solid var(--border-accent); }

  /* ── EQUIPMENT GRID ── */
  .equipment-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 6px;
  }

  @media (min-width: 360px) {
    .equipment-grid { grid-template-columns: 1fr 1fr 1fr; }
  }

  .equip-item {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 10px;
    background: var(--bg-surface-2);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: border-color var(--transition), background var(--transition);
    user-select: none;
  }

  .equip-item:hover { background: var(--bg-hover); }

  .equip-item input[type=checkbox] { display: none; }

  .equip-check {
    width: 14px; height: 14px;
    border: 1px solid var(--border);
    border-radius: 3px;
    background: transparent;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background var(--transition), border-color var(--transition);
    font-size: 9px;
    color: #000;
  }

  .equip-item input:checked ~ .equip-check {
    background: var(--accent);
    border-color: var(--accent);
  }

  .equip-item input:checked ~ .equip-check::after { content: '✓'; }

  .equip-text {
    font-size: 12px;
    color: var(--text-dim);
    line-height: 1.2;
    transition: color var(--transition);
  }

  .equip-item input:checked ~ .equip-text { color: var(--text); }

  /* ── EQUIP SELECT ALL ── */
  .equip-actions {
    display: flex;
    gap: 8px;
    margin-bottom: 8px;
  }

  .link-btn {
    background: none;
    border: none;
    color: var(--accent);
    font-size: 12px;
    font-family: 'DM Mono', monospace;
    cursor: pointer;
    padding: 0;
    text-decoration: underline;
    text-underline-offset: 2px;
    opacity: 0.8;
    transition: opacity var(--transition);
  }

  .link-btn:hover { opacity: 1; }

  /* ── CONDITIONAL PANELS ── */
  .cond-panel {
    display: none;
    flex-direction: column;
    gap: 10px;
  }

  .cond-panel.visible { display: flex; }

  /* ── ACTIONS ── */
  #actions {
    display: flex;
    gap: 10px;
    margin: 24px 0;
  }

  .btn-generate {
    flex: 1;
    min-height: 52px;
    background: var(--accent);
    color: #000;
    border: none;
    border-radius: var(--radius);
    font-family: 'Bebas Neue', sans-serif;
    font-size: 22px;
    letter-spacing: 0.08em;
    cursor: pointer;
    transition: background var(--transition), transform var(--transition), box-shadow var(--transition);
    position: relative;
    overflow: hidden;
  }

  .btn-generate::after {
    content: '';
    position: absolute;
    inset: 0;
    background: linear-gradient(135deg, rgba(255,255,255,0.12) 0%, transparent 60%);
    pointer-events: none;
  }

  .btn-generate:hover {
    background: var(--accent-hover);
    transform: translateY(-1px);
    box-shadow: 0 8px 24px rgba(245,158,11,0.25);
  }

  .btn-generate:active { transform: translateY(0); }

  .btn-regen {
    min-height: 52px;
    min-width: 52px;
    background: var(--bg-surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    color: var(--text-muted);
    font-size: 20px;
    cursor: pointer;
    transition: border-color var(--transition), color var(--transition), transform var(--transition);
    display: none;
    align-items: center;
    justify-content: center;
  }

  .btn-regen:hover { border-color: var(--accent); color: var(--accent); transform: rotate(90deg); }
  .btn-regen.visible { display: flex; }

  /* ── RESULT ── */
  #result {
    animation: fadeIn 0.3s ease;
  }

  @keyframes fadeIn { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: translateY(0); } }

  .result-empty {
    text-align: center;
    padding: 40px 20px;
    color: var(--text-muted);
    font-size: 13px;
    border: 1px dashed var(--border);
    border-radius: var(--radius);
  }

  .result-empty .empty-icon {
    font-size: 32px;
    display: block;
    margin-bottom: 12px;
    opacity: 0.4;
  }

  .metcon-card {
    background: var(--bg-surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    overflow: hidden;
  }

  .metcon-header {
    padding: 20px;
    border-bottom: 1px solid var(--border);
    background: linear-gradient(135deg, var(--bg-surface-2), var(--bg-surface));
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 12px;
  }

  .metcon-title-block { flex: 1; }

  .format-badge {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 4px 10px;
    background: var(--accent);
    color: #000;
    border-radius: 4px;
    font-family: 'DM Mono', monospace;
    font-size: 11px;
    font-weight: 600;
    letter-spacing: 0.1em;
    text-transform: uppercase;
    margin-bottom: 8px;
  }

  .metcon-headline {
    font-family: 'Bebas Neue', sans-serif;
    font-size: 36px;
    line-height: 1;
    color: var(--text);
    letter-spacing: 0.02em;
  }

  .metcon-sub {
    font-size: 12px;
    color: var(--text-secondary);
    margin-top: 4px;
    font-family: 'DM Mono', monospace;
  }

  .time-display {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    flex-shrink: 0;
  }

  .time-number {
    font-family: 'Bebas Neue', sans-serif;
    font-size: 44px;
    line-height: 1;
    color: var(--accent);
  }

  .time-unit {
    font-family: 'DM Mono', monospace;
    font-size: 10px;
    letter-spacing: 0.15em;
    color: var(--text-secondary);
    text-transform: uppercase;
  }

  /* ── MOVEMENT LIST ── */
  .movement-list {
    padding: 4px 0;
  }

  .movement-item {
    display: flex;
    align-items: center;
    padding: 14px 20px;
    border-bottom: 1px solid var(--border);
    gap: 14px;
    transition: background var(--transition);
  }

  .movement-item:last-child { border-bottom: none; }
  .movement-item:hover { background: var(--bg-surface-2); }

  .mvt-num {
    font-family: 'DM Mono', monospace;
    font-size: 10px;
    color: var(--text-secondary);
    min-width: 18px;
    opacity: 0.5;
  }

  .mvt-qty {
    font-family: 'Bebas Neue', sans-serif;
    font-size: 26px;
    color: var(--accent);
    min-width: 52px;
    line-height: 1;
  }

  .mvt-info { flex: 1; }

  .mvt-name {
    font-size: 15px;
    font-weight: 500;
    color: var(--text);
    line-height: 1.2;
  }

  .mvt-rx {
    font-family: 'DM Mono', monospace;
    font-size: 11px;
    color: var(--text-muted);
    margin-top: 2px;
  }

  .mvt-cat {
    font-family: 'DM Mono', monospace;
    font-size: 9px;
    letter-spacing: 0.1em;
    text-transform: uppercase;
    padding: 3px 7px;
    border-radius: 3px;
    flex-shrink: 0;
  }

  .mvt-cat.gymnastics { background: rgba(59,130,246,0.12); color: #60a5fa; }
  .mvt-cat.weightlifting { background: rgba(239,68,68,0.12); color: #f87171; }
  .mvt-cat.monostructural { background: rgba(34,197,94,0.12); color: #4ade80; }

  /* ── EMOM SPECIFIC ── */
  .emom-list { padding: 4px 0; }

  .emom-minute {
    display: flex;
    align-items: center;
    padding: 12px 20px;
    border-bottom: 1px solid var(--border);
    gap: 14px;
    transition: background var(--transition);
  }

  .emom-minute:last-child { border-bottom: none; }
  .emom-minute:hover { background: var(--bg-surface-2); }

  .emom-min-label {
    font-family: 'DM Mono', monospace;
    font-size: 10px;
    color: var(--text-secondary);
    min-width: 52px;
    letter-spacing: 0.05em;
  }

  .emom-content { flex: 1; }

  .emom-line {
    font-size: 14px;
    color: var(--text);
    display: flex;
    align-items: center;
    gap: 8px;
  }

  .emom-line + .emom-line { margin-top: 4px; }

  .emom-qty {
    font-family: 'Bebas Neue', sans-serif;
    font-size: 20px;
    color: var(--accent);
    min-width: 32px;
    line-height: 1;
  }

  /* ── DESCENDING SCHEME ── */
  .descending-scheme {
    padding: 16px 20px;
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    gap: 8px;
    flex-wrap: wrap;
  }

  .scheme-label {
    font-family: 'DM Mono', monospace;
    font-size: 10px;
    color: var(--text-secondary);
    letter-spacing: 0.1em;
    text-transform: uppercase;
    margin-right: 4px;
  }

  .scheme-pill {
    font-family: 'DM Mono', monospace;
    font-size: 13px;
    font-weight: 500;
    color: var(--accent);
    background: var(--accent-dim2);
    border: 1px solid var(--border-accent);
    padding: 3px 10px;
    border-radius: 20px;
  }

  .scheme-sep {
    color: var(--text-muted);
    font-size: 12px;
  }

  /* ── ERROR ── */
  .error-card {
    background: rgba(239,68,68,0.08);
    border: 1px solid rgba(239,68,68,0.25);
    border-radius: var(--radius);
    padding: 16px 20px;
    color: #f87171;
    font-size: 13px;
    line-height: 1.5;
  }

  /* ── SEPARATOR ── */
  .sep {
    height: 1px;
    background: var(--border);
    margin: 8px 0;
  }

  /* ── FOOTER ── */
  /* ── FAQ ── */
  .faq-section {
    margin-top: 40px;
    padding-top: 24px;
    border-top: 1px solid var(--border);
  }

  .faq-title {
    font-family: 'DM Mono', monospace;
    font-size: 11px;
    letter-spacing: 0.15em;
    text-transform: uppercase;
    color: var(--text-secondary);
    margin-bottom: 16px;
  }

  .faq-item {
    background: var(--bg-surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    margin-bottom: 8px;
    overflow: hidden;
  }

  .faq-item summary {
    list-style: none;
    cursor: pointer;
    padding: 14px 16px;
    font-size: 13px;
    font-weight: 500;
    color: var(--text);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    transition: background var(--transition);
  }

  .faq-item summary::-webkit-details-marker { display: none; }

  .faq-item summary::after {
    content: '+';
    font-family: 'DM Mono', monospace;
    font-size: 16px;
    color: var(--accent);
    flex-shrink: 0;
    transition: transform var(--transition);
  }

  .faq-item[open] summary::after { transform: rotate(45deg); }

  .faq-item summary:hover { background: var(--bg-hover); }

  .faq-item p {
    padding: 0 16px 16px;
    font-size: 13px;
    line-height: 1.6;
    color: var(--text-muted);
    font-weight: 300;
  }

  .faq-item p a { color: var(--accent); }
  .faq-item p a:hover { color: var(--accent-hover); }

  .page-footer {
    margin-top: 40px;
    padding: 16px 0 8px;
    border-top: 1px solid var(--border);
    display: flex;
    align-items: center;
    gap: 8px;
    font-family: 'DM Mono', monospace;
    font-size: 10px;
    letter-spacing: 0.12em;
    text-transform: uppercase;
    color: var(--text-secondary);
  }

  .footer-sep { opacity: 0.4; }

  .page-footer a {
    color: inherit;
    text-decoration: none;
    border-bottom: 1px dotted var(--text-secondary);
  }

  .page-footer a:hover { color: var(--text); border-color: var(--text); }

  /* ── DESKTOP ── */
  @media (min-width: 480px) {
    #app {
      padding-left: max(24px, env(safe-area-inset-left));
      padding-right: max(24px, env(safe-area-inset-right));
    }
    .equipment-grid { grid-template-columns: 1fr 1fr 1fr; }
  }

  @media (min-width: 768px) {
    header { padding: 48px 0 32px; }
    h1 { font-size: 80px; }
  }
