/**
 * Journal Theme
 *
 * Warm paper aesthetic — cream backgrounds, ink-dark serif text
 * inspired by themahjong.guide. Tuned for long, comfortable reading
 * and writing sessions.
 */

@layer theme {
  :root {
    /* Typography — Source Serif for prose, Inter for chrome */
    --font-family-serif: 'Iowan Old Style', 'Charter', Georgia, serif;
    --font-family-base:
      'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    --font-family-mono:
      'IBM Plex Mono', ui-monospace, 'SF Mono', Menlo, monospace;

    /* Font sizes */
    --f1: 1.75rem;
    --f2: 1.375rem;
    --f3: 1.125rem;
    --f4: 1rem;
    --f5: 0.875rem;
    --f6: 0.8125rem;
    --f7: 0.6875rem;

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

    /* Weights */
    --fw-normal: 400;
    --fw-medium: 500;
    --fw-semibold: 600;

    /* Line height */
    --lh-tight: 1.25;
    --lh-base: 1.65;
    --lh-prose: 1.75;

    /* Border radius — gentle, not bubbly */
    --br-sm: 4px;
    --br-base: 6px;
    --br-lg: 10px;
    --br-xl: 14px;
    --br-full: 9999px;

    /* Soft, paper-like elevation */
    --shadow-paper:
      0 1px 2px rgba(35, 30, 18, 0.04), 0 4px 12px rgba(35, 30, 18, 0.05);

    --transition-fast: 0.12s cubic-bezier(0.4, 0, 0.2, 1);
    --transition-base: 0.22s cubic-bezier(0.4, 0, 0.2, 1);

    --z-fixed: 1030;

    /* Paper palette (HSL splits used by Civility) */

    /* Background — warm cream #f1ede0 */
    --whiteH: 46;
    --whiteS: 38%;
    --whiteL: 91%;

    /* Ink — warm near-black #1a1a12 */
    --blackH: 60;
    --blackS: 18%;
    --blackL: 9%;

    /* Jade — primary accent #006C00 */
    --brand-primaryH: 120;
    --brand-primaryS: 100%;
    --brand-primaryL: 21%;

    /* Soft jade — muted secondary */
    --brand-softH: 120;
    --brand-softS: 28%;
    --brand-softL: 36%;

    /* Wash — used for active states */
    --brand-lightH: 120;
    --brand-lightS: 30%;
    --brand-lightL: 88%;

    /* Gold — rare highlight #a88536 */
    --goldH: 42;
    --goldS: 51%;
    --goldL: 44%;

    /* Status (kept restrained) */
    --greenH: 120;
    --greenS: 60%;
    --greenL: 28%;

    --yellowH: 42;
    --yellowS: 60%;
    --yellowL: 48%;

    --redH: 4;
    --redS: 60%;
    --redL: 44%;

    --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: 46;
    --borderS: 36%;
    --borderL: 77%;
  }

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

    /* Custom paper colors used directly via var() */
    --paper: #faf6e9;
    --paper-alt: #ece5d0;
    --rule: #d9cfae;
    --rule-soft: #e8dfc9;
    --ink: #1a1a12;
    --ink-soft: #3d3a30;
    --ink-muted: #7a7565;
    --ink-faint: #b0a993;
    --jade: #006c00;
    --jade-deep: #003500;
    --jade-wash: rgba(0, 108, 0, 0.08);
    --gold: #a88536;
  }

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

  body {
    font-family: var(--font-family-base);
    color: var(--ink);
    background: hsl(var(--backgroundH), var(--backgroundS), var(--backgroundL));
  }

  ::selection {
    background: var(--jade-wash);
    color: var(--ink);
  }

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

  header {
    background: hsl(
      var(--backgroundH),
      var(--backgroundS),
      var(--backgroundL)
    );
    transition: opacity 0.6s cubic-bezier(0.4, 0, 0.2, 1);
  }

  footer[fixed] {
    transition: opacity 0.6s cubic-bezier(0.4, 0, 0.2, 1);
  }

  .entry-head {
    transition: opacity 0.6s cubic-bezier(0.4, 0, 0.2, 1);
  }

  body.entry-focused header,
  body.entry-focused footer[fixed],
  body.entry-focused .entry-head[data-today] {
    opacity: 0;
    pointer-events: none;
  }

  body:has(> header[hidden]) {
    --header-height: 0px;
  }

  ui-header-content {
    display: flex;
    align-items: center;
    gap: var(--s2);
    height: var(--header-height);
    padding: 0 var(--s4);
  }

  ui-header-content h1 {
    flex: 1;
    margin: 0;
    font-family: var(--font-family-base);
    font-size: var(--f5);
    font-weight: var(--fw-medium);
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--ink-soft);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  ui-header-content .header-back {
    background: transparent;
    border: none;
    padding: var(--s1);
    color: var(--ink-soft);
    cursor: pointer;
  }

  ui-header-content .header-back:hover {
    color: var(--jade);
  }

  ui-header-content .header-actions {
    display: flex;
    align-items: center;
    gap: var(--s2);
  }

  /* ── Bottom tab bar ───────────────────────────────────────────────────── */

  footer[fixed] {
    padding: 0;
    height: var(--footer-height);
    background: var(--paper);
    border-top: 1px solid var(--rule);
  }

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

  ui-bottom-bar a {
    color: var(--ink-muted);
    transition: color var(--transition-fast);
  }

  ui-bottom-bar a:hover {
    color: var(--ink-soft);
  }

  ui-bottom-bar a[aria-current='page'] {
    color: var(--jade);
  }

  /* ── Page hosts ───────────────────────────────────────────────────────── */

  entry-page {
    display: block;
    padding: var(--s4) var(--s3) var(--s5);
  }

  calendar-page,
  settings-page,
  settings-about-page {
    display: flex;
    flex-direction: column;
    gap: var(--s5);
    padding: var(--s4) var(--s3);
    max-width: 720px;
    margin: 0 auto;
  }

  /* ── Entry page (the writing surface) ────────────────────────────────── */

  .entry-shell {
    max-width: 38rem;
    margin: 0 auto;
    display: flex;
    flex-direction: column;
    gap: var(--s4);
  }

  .entry-loading {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: var(--s6);
    opacity: 0.5;
  }

  .entry-head {
    display: grid;
    grid-template-columns: auto 1fr auto;
    align-items: center;
    gap: var(--s2);
  }

  .entry-nav {
    background: transparent;
    border: 1px solid var(--rule);
    border-radius: var(--br-full);
    width: 36px;
    height: 36px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    color: var(--ink-soft);
    cursor: pointer;
    padding: 0;
    transition:
      border-color var(--transition-fast),
      color var(--transition-fast);
  }

  .entry-nav:hover:not([disabled]) {
    color: var(--jade);
    border-color: var(--jade);
  }

  .entry-nav[disabled] {
    opacity: 0.3;
    cursor: default;
  }

  .entry-nav img {
    width: 18px;
    height: 18px;
    display: block;
  }

  .entry-date {
    text-align: center;
  }

  .entry-date-eyebrow {
    display: block;
    font-size: var(--f7);
    font-weight: var(--fw-medium);
    letter-spacing: 0.16em;
    text-transform: uppercase;
    color: var(--ink-muted);
    margin-bottom: 2px;
  }

  .entry-date-full {
    margin: 0;
    font-family: var(--font-family-serif);
    font-weight: var(--fw-medium);
    font-size: var(--f2);
    color: var(--ink);
    line-height: var(--lh-tight);
    letter-spacing: -0.01em;
  }

  .entry-body {
    display: block;
    width: 100%;
    min-height: 60vh;
    padding: var(--s4) var(--s4);
    background: transparent;
    border: 1px solid var(--rule-soft);
    border-radius: var(--br-lg);
    font-family: var(--font-family-serif);
    font-size: 1.0625rem;
    line-height: var(--lh-prose);
    color: var(--ink);
    resize: vertical;
    outline: none;
    transition: border-color var(--transition-base);
    -webkit-appearance: none;
  }

  .entry-body::placeholder {
    color: var(--ink-faint);
    font-style: italic;
  }

  .entry-meta {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    font-size: var(--f6);
    color: var(--ink-muted);
    font-variant-numeric: tabular-nums;
    padding: 0 var(--s2);
  }

  .entry-meta #entry-state {
    font-style: italic;
  }

  /* ── Calendar page ───────────────────────────────────────────────────── */

  .section-title {
    font-family: var(--font-family-serif);
    font-size: var(--f3);
    font-weight: var(--fw-medium);
    color: var(--ink);
    margin: 0 0 var(--s3);
    letter-spacing: -0.01em;
  }

  .section-subtitle {
    color: var(--ink-muted);
    font-weight: var(--fw-normal);
    font-size: var(--f5);
    font-family: var(--font-family-base);
  }

  ui-calendar {
    display: flex;
    flex-direction: column;
    gap: var(--s1);
    overflow-x: auto;
    padding: var(--s3);
    background: var(--paper);
    border: 1px solid var(--rule-soft);
    border-radius: var(--br-lg);
  }

  ui-calendar-months,
  ui-calendar-grid {
    display: grid;
    gap: 3px;
  }

  ui-calendar-months {
    font-size: var(--f7);
    color: var(--ink-muted);
    letter-spacing: 0.06em;
    text-transform: uppercase;
  }

  ui-calendar-months span {
    white-space: nowrap;
  }

  ui-calendar-grid {
    grid-template-rows: repeat(7, auto);
    grid-auto-flow: column;
  }

  .cal-cell {
    width: 100%;
    aspect-ratio: 1;
    border-radius: 2px;
    background: var(--rule-soft);
    cursor: pointer;
    user-select: none;
    transition: transform var(--transition-fast);
  }

  .cal-cell:hover:not([data-future]) {
    transform: scale(1.15);
  }

  .cal-cell[data-level='0'] {
    cursor: default;
  }

  .cal-cell[data-level='0']:hover {
    transform: none;
  }

  .cal-cell[data-level='1'] {
    background: rgba(0, 108, 0, 0.18);
  }

  .cal-cell[data-level='2'] {
    background: rgba(0, 108, 0, 0.4);
  }

  .cal-cell[data-level='3'] {
    background: rgba(0, 108, 0, 0.65);
  }

  .cal-cell[data-level='4'] {
    background: var(--jade);
  }

  .cal-cell[data-future] {
    visibility: hidden;
  }

  .cal-cell[data-selected] {
    outline: 2px solid var(--jade);
    outline-offset: 1px;
  }

  /* ── Entries list ────────────────────────────────────────────────────── */

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

  .entry-row {
    display: flex;
    flex-direction: column;
    gap: var(--s2);
    padding: var(--s3) var(--s4);
    text-align: left;
    background: var(--paper);
    color: inherit;
    border: 1px solid var(--rule-soft);
    border-radius: var(--br-lg);
    cursor: pointer;
    transition:
      border-color var(--transition-fast),
      transform var(--transition-fast);
  }

  .entry-row:hover {
    border-color: var(--rule);
    transform: translateY(-1px);
  }

  .entry-row-head {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    gap: var(--s3);
  }

  .entry-row-date {
    font-family: var(--font-family-serif);
    font-size: var(--f4);
    font-weight: var(--fw-medium);
    color: var(--ink);
  }

  .entry-row-count {
    font-size: var(--f6);
    color: var(--ink-muted);
    font-variant-numeric: tabular-nums;
  }

  .entry-row-preview {
    margin: 0;
    font-family: var(--font-family-serif);
    font-size: var(--f5);
    line-height: var(--lh-base);
    color: var(--ink-soft);
    overflow: hidden;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
  }

  #cal-load-more {
    margin-top: var(--s3);
    width: 100%;
    padding: var(--s3);
    background: transparent;
    border: 1px dashed var(--rule);
    border-radius: var(--br-lg);
    color: var(--ink-muted);
    font-size: var(--f6);
    cursor: pointer;
    transition:
      color var(--transition-fast),
      border-color var(--transition-fast);
  }

  #cal-load-more:hover {
    color: var(--jade);
    border-color: var(--jade);
  }

  .empty-message {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: var(--s6);
    color: var(--ink-muted);
    font-family: var(--font-family-serif);
    font-style: italic;
    text-align: center;
    line-height: var(--lh-base);
    margin: 0;
  }

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

  settings-page section,
  settings-about-page section {
    display: flex;
    flex-direction: column;
    gap: var(--s2);
  }

  settings-page h2,
  settings-about-page h2 {
    font-family: var(--font-family-serif);
    font-size: var(--f3);
    font-weight: var(--fw-medium);
    margin: 0 0 var(--s2);
    color: var(--ink);
    letter-spacing: -0.01em;
  }

  settings-page p,
  settings-about-page p {
    margin: 0 0 var(--s2);
    color: var(--ink-muted);
    line-height: var(--lh-base);
    font-size: var(--f5);
  }

  settings-page a,
  settings-about-page a {
    color: var(--jade);
    text-decoration: underline;
    text-underline-offset: 2px;
    text-decoration-thickness: 1px;
  }

  settings-page a:hover,
  settings-about-page a:hover {
    color: var(--jade-deep);
  }

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

  .settings-nav-link {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--s3);
    padding: var(--s3) var(--s4);
    background: var(--paper);
    border: 1px solid var(--rule-soft);
    border-radius: var(--br-lg);
    text-decoration: none;
    color: var(--ink);
    font-weight: var(--fw-medium);
    font-size: var(--f5);
    transition: border-color var(--transition-fast);
  }

  .settings-nav-link:hover {
    border-color: var(--rule);
    color: var(--ink);
  }

  .settings-nav-link-meta {
    font-size: var(--f6);
    color: var(--ink-muted);
    font-weight: var(--fw-normal);
    margin-top: 2px;
  }

  /* ── Civility component restyles ─────────────────────────────────────── */

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

  ui-sync label {
    display: flex;
    flex-direction: column;
    gap: var(--s1);
    font-size: var(--f6);
    color: var(--ink-soft);
  }

  ui-sync label span {
    color: var(--ink-muted);
  }

  ui-sync input {
    width: 100%;
    padding: var(--s2) var(--s3);
    background: var(--paper);
    border: 1px solid var(--rule);
    border-radius: var(--br-base);
    color: var(--ink);
    font-family: var(--font-family-base);
    font-size: var(--f5);
    box-sizing: border-box;
  }

  ui-sync input:focus {
    outline: none;
    border-color: var(--jade);
    box-shadow: 0 0 0 3px var(--jade-wash);
  }

  ui-sync .ui-sync__status {
    display: flex;
    align-items: baseline;
    gap: var(--s3);
    font-size: var(--f6);
    color: var(--ink-muted);
  }

  ui-sync .ui-sync__indicator--ok {
    color: var(--jade);
  }

  ui-sync .ui-sync__error {
    color: hsl(var(--errorH), var(--errorS), var(--errorL));
    font-size: var(--f6);
  }

  ui-sync .ui-sync__hint,
  ui-sync .ui-sync__server {
    color: var(--ink-muted);
    font-size: var(--f6);
  }

  ui-sync .ui-sync__actions {
    display: flex;
    gap: var(--s2);
    flex-wrap: wrap;
  }

  ui-sync button,
  ui-data-actions button {
    padding: var(--s2) var(--s4);
    background: var(--paper);
    border: 1px solid var(--rule);
    border-radius: var(--br-base);
    color: var(--ink);
    font-family: var(--font-family-base);
    font-size: var(--f5);
    cursor: pointer;
    transition:
      border-color var(--transition-fast),
      color var(--transition-fast);
  }

  ui-sync button:hover,
  ui-data-actions button:hover {
    border-color: var(--jade);
    color: var(--jade);
  }

  ui-pwa-version,
  ui-pwa-install {
    display: block;
    color: var(--ink-muted);
    font-size: var(--f6);
  }

  /* Skip link */

  .skip-to-main {
    position: absolute;
    left: -10000px;
    top: auto;
  }

  .skip-to-main:focus {
    left: var(--s3);
    top: var(--s3);
    background: var(--paper);
    color: var(--ink);
    padding: var(--s2) var(--s3);
    border: 1px solid var(--jade);
    border-radius: var(--br-base);
    z-index: var(--z-fixed);
  }

  /* ── Responsive ──────────────────────────────────────────────────────── */

  @media (min-width: 720px) {
    entry-page {
      padding: var(--s5) var(--s4) var(--s6);
    }

    .entry-body {
      min-height: 65vh;
      padding: var(--s5) var(--s5);
    }
  }
}
