/**
 * Moonboard Theme
 */

@layer theme {
  :root {
    /* Typography */
    --font-family-base:
      'Inter', 'SF Pro Text', -apple-system, BlinkMacSystemFont, 'Segoe UI',
      'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', sans-serif;
    --font-family-mono:
      'SF Mono', Monaco, 'Inconsolata', 'Roboto Mono', 'Source Code Pro',
      monospace;

    /* FONT SIZE */
    --f1: 1.5rem;
    --f2: 1.25rem;
    --f3: 1.125rem;
    --f4: 1rem;
    --f5: 0.875rem;
    --f6: 0.75rem;
    --f7: 0.625rem;

    /* SPACING */
    --s0: 0;
    --s1: 0.25rem;
    --s2: 0.5rem;
    --s3: 1rem;
    --s4: 1.5rem;
    --s5: 2rem;
    --s6: 3rem;

    /* FONT WEIGHT */
    --fw-normal: 400;
    --fw-medium: 500;
    --fw-semibold: 600;
    --fw-bold: 700;

    /* LINE HEIGHT */
    --lh-tight: 1.3;
    --lh-base: 1.6;

    /* BORDER RADIUS */
    --br-sm: 6px;
    --br-base: 10px;
    --br-lg: 20px;
    --br-xl: 20px;
    --br-full: 9999px;

    /* Shadows */
    --shadow-sm:
      0 2px 4px 0 rgba(0, 0, 0, 0.08), 0 1px 2px 0 rgba(0, 0, 0, 0.04);
    --shadow-base:
      0 4px 8px 0 rgba(0, 0, 0, 0.12), 0 2px 4px 0 rgba(0, 0, 0, 0.08);
    --shadow-md:
      0 8px 16px -2px rgba(0, 0, 0, 0.15), 0 4px 8px -2px rgba(0, 0, 0, 0.1);
    --shadow-lg:
      0 16px 32px -4px rgba(0, 0, 0, 0.18), 0 8px 16px -4px rgba(0, 0, 0, 0.12);

    /* Transitions */
    --transition-fast: 0.15s cubic-bezier(0.4, 0, 0.2, 1);
    --transition-base: 0.25s cubic-bezier(0.4, 0, 0.2, 1);

    /* Z-index scale */
    --z-fixed: 1030;

    /* Color palette */
    --whiteH: 244;
    --whiteS: 20%;
    --whiteL: 98%;

    --blackH: 237;
    --blackS: 25%;
    --blackL: 25%;

    --brand-primaryH: 237;
    --brand-primaryS: 50%;
    --brand-primaryL: 65%;

    --brand-secondaryH: 244;
    --brand-secondaryS: 85%;
    --brand-secondaryL: 88%;

    --brand-lightH: 244;
    --brand-lightS: 60%;
    --brand-lightL: 92%;

    --brand-softH: 240;
    --brand-softS: 35%;
    --brand-softL: 70%;

    --greenH: 142;
    --greenS: 76%;
    --greenL: 36%;

    --yellowH: 45;
    --yellowS: 93%;
    --yellowL: 58%;

    --redH: 0;
    --redS: 84%;
    --redL: 60%;

    --errorH: var(--redH);
    --errorS: var(--redS);
    --errorL: var(--redL);

    --warningH: var(--yellowH);
    --warningS: var(--yellowS);
    --warningL: var(--yellowL);

    --successH: var(--greenH);
    --successS: var(--greenS);
    --successL: var(--greenL);

    --backgroundH: var(--whiteH);
    --backgroundS: var(--whiteS);
    --backgroundL: var(--whiteL);

    --bodyH: var(--blackH);
    --bodyS: var(--blackS);
    --bodyL: var(--blackL);

    --secondaryH: var(--brand-softH);
    --secondaryS: var(--brand-softS);
    --secondaryL: var(--brand-softL);

    --primaryH: var(--brand-primaryH);
    --primaryS: var(--brand-primaryS);
    --primaryL: var(--brand-primaryL);

    --borderH: var(--brand-primaryH);
    --borderS: var(--brand-primaryS);
    --borderL: 80%;
  }

  :root {
    --header-height: 56px;
    --footer-height: 64px;
    --main-padding: 0;
  }

  html {
    -ms-overflow-style: none;
    scrollbar-width: none;
  }

  /*body {
    padding-top: env(safe-area-inset-top);
    padding-bottom: env(safe-area-inset-bottom);
  }*/

  /* ── Header ───────────────────────────────────────────────────────────── */

  header {
    display: flex;
    align-items: center;
    gap: var(--s2);
    height: var(--header-height);
    padding: 0 var(--container-padding);
    border-bottom: 1px solid currentColor;
  }

  header strong {
    flex: 1;
    font-size: var(--f4);
    font-weight: var(--fw-semibold);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  /* ── Fixed bottom tab bar ──────────────────────────────────────────────── */

  footer[fixed] {
    padding: 0;
    height: var(--footer-height);
  }

  ui-bottom-bar img {
    width: 24px;
    height: 24px;
  }

  /* Stopwatch running indicator in nav */
  .sw-running {
    color: var(--primary);
    font-variant-numeric: tabular-nums;
  }

  /* ── Civility component overrides ─────────────────────────────────────── */

  /* Filter bars: sticky below the header, flex column for search/filters */
  /* Pages without a sub-header set hidden attribute via JS */
  #sub-header[hidden] {
    display: none;
  }

  ui-sub-header {
    display: flex;
    flex-direction: column;
    gap: var(--s2);
    padding: var(--s3);
    border-bottom: 1px solid currentColor;
  }

  /* Filter components are transparent to layout — children flow into ui-sub-header's flex */
  library-filters {
    display: contents;
  }

  /* Climb header: full-width flex row inside ui-sub-header */
  climb-header {
    display: flex;
    align-items: center;
    gap: var(--s2);
    width: 100%;
  }

  ui-sub-header input[type='search'],
  ui-sub-header select {
    margin: 0;
  }

  ui-sub-header input[type='search'] {
    padding: var(--s2) var(--s3);
    border-radius: var(--br-full);
    font-size: var(--f5);
    background: transparent;
    width: 100%;
  }

  /* Pill-shaped filter buttons in the sub-header */
  ui-sub-header ui-button-group button {
    flex: 1;
    border-radius: var(--br-full);
    font-size: var(--f6);
    padding: var(--s2);
    margin: var(--s2) 0 var(--s2) 0;
  }

  ui-button-group button[aria-pressed='true'] {
    background: var(--primary);
    color: var(--background);
    border-color: var(--primary);
    opacity: 1;
    transform: none;
  }

  ui-button-group button[aria-pressed='true']:hover {
    opacity: 0.85;
    transform: none;
  }

  /* Grade and status badges */
  ui-badge {
    text-transform: none;
    letter-spacing: normal;
  }

  ui-badge.grade {
    font-size: var(--f7);
    font-weight: var(--fw-semibold);
    padding: 2px var(--s2);
    white-space: nowrap;
    flex-shrink: 0;
  }

  /* ── Page host elements ────────────────────────────────────────────────── */

  library-page {
    display: flex;
    flex-direction: column;
    min-height: 100%;
  }

  #cp-body {
    padding: var(--s3);
  }

  settings-page {
    display: block;
    padding: var(--s4) var(--s3);
    max-width: 600px;
    margin: 0 auto;
  }

  sessions-page,
  session-page {
    display: flex;
    flex-direction: column;
    padding: var(--s4) var(--s3);
    gap: var(--s4);
    max-width: 600px;
    margin: 0 auto;
  }

  .active-session-card {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: var(--s4);
    padding: var(--s4) var(--s3);
    border: 1px solid var(--border);
    border-radius: var(--br-lg);
  }

  .sessions-history {
    display: flex;
    flex-direction: column;
    gap: var(--s3);
  }

  .sessions-history-title {
    font-size: var(--f5);
    font-weight: var(--fw-semibold);
    margin: 0;
  }

  .sessions-list {
    display: flex;
    flex-direction: column;
    gap: var(--s2);
  }

  .session-row {
    display: flex;
    flex-direction: column;
    gap: var(--s1);
    padding: var(--s3);
    text-align: left;
    background: transparent;
    border: 1px solid var(--border);
    border-radius: var(--br);
    cursor: pointer;
  }

  .session-row-main {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-weight: var(--fw-semibold);
  }

  .session-row-meta {
    display: flex;
    gap: var(--s3);
    font-size: var(--f6);
    color: var(--muted);
  }

  #sessions-load-more {
    margin-top: var(--s2);
  }

  .session-detail-header {
    display: flex;
    flex-direction: column;
    gap: var(--s2);
  }

  .session-detail-row {
    display: flex;
    align-items: center;
    gap: var(--s2);
  }

  .session-detail-meta {
    display: flex;
    flex-wrap: wrap;
    gap: var(--s2);
    font-size: var(--f6);
    color: var(--muted);
  }

  .session-attempts-list {
    list-style: none;
    margin: 0;
    padding: 0;
    display: flex;
    flex-direction: column;
    gap: var(--s2);
  }

  .session-attempt {
    display: flex;
    flex-direction: column;
    gap: var(--s1);
    padding: var(--s3);
    border: 1px solid var(--border);
    border-radius: var(--br);
  }

  .session-attempt-row {
    display: flex;
    align-items: center;
    gap: var(--s2);
  }

  .session-attempt-name {
    font-weight: var(--fw-semibold);
    flex: 1;
  }

  .session-attempt-meta {
    display: flex;
    gap: var(--s2);
    font-size: var(--f6);
    color: var(--muted);
  }

  .session-notes {
    margin-top: var(--s3);
    padding: var(--s3);
    background: var(--surface);
    border-radius: var(--br);
    white-space: pre-wrap;
  }

  /* ── Grade range filter row ────────────────────────────────────────────── */
  .filters-row {
    display: flex;
    gap: var(--s4);
    flex-wrap: wrap;
    justify-content: space-between;
  }

  .sort-filter {
    display: flex;
    align-items: center;
    gap: var(--s1);
    font-size: var(--f6);
    flex-grow: 0.5;
    margin-left: var(--s3);
  }

  .sort-filter label {
    font-weight: var(--fw-medium);
    margin: 0;
  }

  .sort-filter select {
    padding: var(--s1) var(--s2);
    font-size: var(--f6);
    margin: 0;
    min-width: 6.5em;
    width: 100%;
  }

  .grade-filter {
    display: flex;
    align-items: center;
    gap: var(--s2);
    font-size: var(--f6);
    flex-grow: 0.45;
  }

  .grade-filter label {
    font-weight: var(--fw-medium);
    display: block;
    margin: 0;
    flex-shrink: 0;
  }

  .grade-filter select {
    flex: 1;
    padding: var(--s1) var(--s2);
    font-size: var(--f6);
    margin: 0;
  }

  /* ── Benchmark list (home page) ────────────────────────────────────────── */

  #bm-list {
    flex: 1;
  }

  .empty-message {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: var(--s6);
    opacity: 0.6;
    margin: 0;
  }

  .bm-count {
    padding: var(--s2) var(--s3);
    font-size: var(--f6);
    opacity: 0.6;
    border-bottom: 1px solid currentColor;
  }

  [data-bm-id] {
    display: block;
    width: 100%;
    text-align: left;
    padding: var(--s3);
    border: none;
    border-bottom: 1px solid currentColor;
    background: transparent;
    color: inherit;
    cursor: pointer;
    transition: background var(--transition-fast);
    border-radius: 0;
  }

  [data-bm-id]:hover {
    background: var(--primary-dull);
    opacity: 1;
    transform: none;
  }

  [data-bm-id]:active {
    transform: none;
  }

  .bm-item-main {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--s2);
    margin-bottom: var(--s1);
  }

  .bm-name {
    font-weight: var(--fw-semibold);
    font-size: var(--f5);
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .bm-item-meta {
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: var(--f6);
    opacity: 0.6;
  }

  .bm-setter {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .bm-stats {
    flex-shrink: 0;
    white-space: nowrap;
  }

  #bm-load-more {
    display: block;
    width: 100%;
    padding: var(--s3);
    text-align: center;
    border: none;
    border-radius: 0;
    background: transparent;
    color: var(--primary);
    font-weight: var(--fw-medium);
    font-size: var(--f5);
    cursor: pointer;
    opacity: 1;
    transform: none;
  }

  #bm-load-more:hover {
    opacity: 0.7;
    transform: none;
  }

  /* ── Climb detail header ───────────────────────────────────────────────── */

  .back {
    display: flex;
    align-items: center;
    justify-content: center;
    background: transparent;
    border: none;
    padding: var(--s1);
    cursor: pointer;
    border-radius: var(--br-base);
    flex-shrink: 0;
    opacity: 0.8;
    transition: opacity var(--transition-fast);
  }

  .back:hover {
    opacity: 1;
    transform: none;
  }

  .back img {
    width: 20px;
    height: 20px;
    transform: scaleX(-1);
    display: block;
  }

  .title-info {
    flex: 1;
    min-width: 0;
  }

  .name {
    font-size: var(--f4);
    font-weight: var(--fw-bold);
    display: block;
    line-height: var(--lh-tight);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .subtitle {
    font-size: var(--f7);
    opacity: 0.6;
    display: block;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .meta {
    flex-shrink: 0;
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    gap: 2px;
    font-size: var(--f6);
    font-weight: var(--fw-medium);
    white-space: nowrap;
    opacity: 0.8;
  }

  /* ── Board canvas ──────────────────────────────────────────────────────── */

  .board-wrap {
    position: relative;
    max-width: 480px;
    margin: 0 auto var(--s3);
    border-radius: var(--br-base);
    overflow: hidden;
  }

  .board-wrap img {
    width: 100%;
    display: block;
  }

  .board-wrap canvas {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
  }

  .board-placeholder {
    background: #1a1a1a;
    aspect-ratio: 450 / 485;
    border-radius: var(--br-base);
    max-width: 480px;
    margin: 0 auto var(--s3);
    position: relative;
    overflow: hidden;
  }

  .board-placeholder canvas {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
  }

  /* ── Generic action link/button (YouTube, Log) ─────────────────────────── */

  .action {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: var(--s2);
    padding: var(--s3);
    border: 1px solid currentColor;
    border-radius: var(--br-base);
    text-decoration: none;
    color: inherit;
    font-weight: var(--fw-medium);
    transition: opacity var(--transition-base);
    width: 100%;
    box-sizing: border-box;
    background: transparent;
  }

  .action:hover {
    opacity: 0.7;
    transform: none;
  }

  /* ── progress section in climb detail ───────────────────────────────────── */

  .lb-log-section {
    margin-top: var(--s3);
    padding-top: var(--s3);
    display: flex;
    flex-direction: column;
    gap: var(--s2);
  }

  .lb-log-summary {
    display: flex;
    align-items: center;
    gap: var(--s2);
    font-size: var(--f5);
    flex-wrap: wrap;
  }

  /* ── Settings page ─────────────────────────────────────────────────────── */

  settings-page section {
    margin-bottom: var(--s5);
  }

  settings-page section > p {
    opacity: 0.6;
    margin-bottom: var(--s3);
  }

  settings-page [role='radiogroup'] {
    display: flex;
    flex-direction: column;
    gap: var(--s2);
  }

  settings-page label {
    display: flex;
    align-items: center;
    gap: var(--s3);
    padding: var(--s3);
    border: 1px solid currentColor;
    border-radius: var(--br-base);
    cursor: pointer;
    font-weight: var(--fw-medium);
    transition: all var(--transition-base);
    margin: 0;
  }

  settings-page label:has(input:checked) {
    border-color: var(--primary);
    background: var(--primary-dull);
  }

  settings-page label input[type='radio'] {
    margin: 0;
    width: 18px;
    height: 18px;
    accent-color: var(--primary);
    cursor: pointer;
    border: none;
    padding: 0;
    flex-shrink: 0;
  }

  settings-page label small {
    display: block;
    font-size: var(--f6);
    opacity: 0.6;
    font-weight: var(--fw-normal);
    margin-top: 2px;
  }

  .settings-version-status {
    font-size: var(--f6);
    opacity: 0.7;
  }

  /* ── Link-style button ──────────────────────────────────────────────────── */

  button.link,
  a.link {
    background: none;
    border: none;
    padding: 0;
    color: var(--primary, inherit);
    text-decoration: underline;
    cursor: pointer;
    font-size: inherit;
    font-family: inherit;
  }

  button.link:hover,
  a.link:hover {
    opacity: 0.7;
  }

  /* ── ui-sync component ──────────────────────────────────────────────────── */

  ui-sync form {
    display: flex;
    flex-direction: column;
    gap: var(--s3);
  }

  ui-sync label {
    display: flex;
    flex-direction: column;
    gap: var(--s1);
    cursor: default;
    border: none;
    padding: 0;
    font-weight: var(--fw-normal);
    border-radius: 0;
    background: none;
  }

  ui-sync label span {
    font-size: var(--f6);
    opacity: 0.7;
  }

  ui-sync label input {
    width: 100%;
    box-sizing: border-box;
  }

  ui-sync .ui-sync__error {
    font-size: var(--f6);
    color: var(--danger, #e05);
    margin: 0;
  }

  ui-sync .ui-sync__server {
    opacity: 0.6;
    margin: 0 0 var(--s2);
    font-size: var(--f6);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  ui-sync .ui-sync__hint {
    opacity: 0.6;
    font-size: var(--f6);
    display: block;
  }

  ui-sync .ui-sync__status {
    display: flex;
    align-items: baseline;
    gap: var(--s3);
    margin-bottom: var(--s3);
  }

  ui-sync .ui-sync__status small {
    opacity: 0.6;
  }

  ui-sync .ui-sync__indicator {
    font-weight: var(--fw-medium);
  }

  ui-sync .ui-sync__indicator--ok {
    color: var(--success, #2a2);
  }

  ui-sync .ui-sync__indicator--syncing {
    opacity: 0.7;
  }

  ui-sync .ui-sync__actions {
    display: flex;
    align-items: center;
    gap: var(--s3);
    flex-wrap: wrap;
  }

  ui-sync .ui-sync__actions .action {
    width: auto;
    padding: var(--s2) var(--s4);
  }

  /* ── progress session dialog ─────────────────────────────────────────────── */

  #cp-dialog {
    border: none;
    padding: 0;
    background: transparent;
  }

  #cp-dialog::backdrop {
    background: rgba(0, 0, 0, 0.5);
  }

  .lb-dialog {
    background: var(--background);
    border: 1px solid currentColor;
    border-radius: var(--br-lg);
    padding: var(--s4);
    display: flex;
    flex-direction: column;
    gap: var(--s3);
  }

  .lb-dialog-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }

  .lb-dialog-title {
    font-weight: var(--fw-semibold);
    font-size: var(--f3);
  }

  .lb-dialog-close {
    background: none;
    border: none;
    cursor: pointer;
    font-size: var(--f3);
    opacity: 0.6;
    padding: var(--s1);
    line-height: 1;
    color: inherit;
  }

  .lb-dialog-close:hover {
    opacity: 1;
    transform: none;
  }

  .lb-field {
    display: flex;
    flex-direction: column;
    gap: var(--s1);
  }

  .lb-field--row {
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
  }

  .lb-field-label {
    font-size: var(--f6);
    font-weight: var(--fw-medium);
    opacity: 0.7;
  }

  #lb-dialog-box ui-counter {
    height: 40px;
  }

  #lb-dialog-box ui-counter button {
    width: 40px;
    height: 40px;
    font-size: var(--f2);
    border-radius: var(--br-base);
  }

  #lb-dialog-box ui-counter input {
    font-size: var(--f3);
    font-weight: var(--fw-semibold);
    font-variant-numeric: tabular-nums;
    border-radius: var(--br-base);
  }

  .lb-checkbox {
    width: 22px;
    height: 22px;
    accent-color: var(--primary);
    cursor: pointer;
    flex-shrink: 0;
  }

  .lb-star-row {
    display: flex;
    gap: var(--s1);
  }

  .lb-star-btn {
    background: none;
    border: none;
    font-size: 1.75rem;
    cursor: pointer;
    color: var(--border);
    padding: 0 var(--s1);
    line-height: 1;
    transition: color var(--transition-fast);
    opacity: 1;
  }

  .lb-star-btn:hover {
    opacity: 1;
    transform: none;
  }

  .lb-star-btn--active {
    color: hsl(var(--yellowH), var(--yellowS), var(--yellowL));
  }

  .lb-submit-btn {
    width: 100%;
    padding: var(--s3);
    background: var(--primary);
    color: var(--background);
    border: none;
    border-radius: var(--br-base);
    font-size: var(--f4);
    font-weight: var(--fw-semibold);
    cursor: pointer;
    transition: opacity var(--transition-fast);
  }

  .lb-submit-btn:hover {
    opacity: 0.85;
    transform: none;
  }

  /* ── Stopwatch page ─────────────────────────────────────────────────────── */

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

  #stopwatch-time {
    font-size: clamp(2.5rem, 12vw, 6rem);
    font-weight: var(--fw-bold);
    font-variant-numeric: tabular-nums;
    line-height: 1;
    letter-spacing: -0.02em;
  }

  .stopwatch-rest {
    margin-top: var(--s3);
    display: inline-flex;
    align-items: baseline;
    gap: var(--s2);
    font-variant-numeric: tabular-nums;
    opacity: 0.7;
  }

  .stopwatch-rest[hidden] {
    display: none;
  }

  .stopwatch-rest-label {
    font-size: var(--f6);
    font-weight: var(--fw-semibold);
    text-transform: uppercase;
    letter-spacing: 0.08em;
  }

  .stopwatch-rest-value {
    font-size: var(--f3);
    font-weight: var(--fw-medium);
  }

  #stopwatch-controls {
    display: flex;
    gap: var(--s3);
    width: 100%;
    justify-content: center;
    flex-wrap: wrap;
  }

  #stopwatch-controls button {
    padding: var(--s3) var(--s4);
    font-size: var(--f4);
    font-weight: var(--fw-semibold);
    border: 2px solid currentColor;
    border-radius: var(--br-lg);
    min-width: 120px;
  }

  #stopwatch-controls button.primary {
    background: var(--primary);
    color: var(--background);
  }

  #laps-container {
    width: 100%;
    display: flex;
    flex-direction: column;
    gap: var(--s2);
    max-height: 300px;
    overflow-y: auto;
  }

  .lap-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: var(--s2) var(--s3);
    border: 1px solid currentColor;
    border-radius: var(--br-base);
    font-variant-numeric: tabular-nums;
  }

  .lap-number {
    font-weight: var(--fw-semibold);
    opacity: 0.6;
  }

  .lap-time {
    font-weight: var(--fw-medium);
  }

  @media (max-width: 480px) {
    #stopwatch-display {
      min-height: 30vh;
    }

    #stopwatch-controls button {
      min-width: 100px;
      padding: var(--s2) var(--s3);
      font-size: var(--f5);
    }
  }
}
