/* ============================================
   CTRL Design System - Widget Components
   w- prefixed component library for AI widgets
   ============================================ */

/* ============================================
   Grid Sizing System
   Like iOS Home Screen widgets: defined sizes
   with sensible constraints per template.

   All sizes (column x row):
          1 col    2 col    3 col    4 col
   1 row  sm       med      wide     banner
   2 row  tall     lg       xl       pano
   3 row  col      poster   max      cinema
   4 row  —        board    wall     full

   Valid sizes per template (editable in showcase):
     verdict:    sm, med, lg
     list:       sm, med, tall, lg, wide
     spectrum:   sm, med, lg
     split:      med, lg              (min 2 cols)
     narrative:  med, tall, lg
     comparison: med, lg              (min 2 cols)
     choices:    sm, med, lg
     checklist:  sm, tall, med, lg
     suggestion: sm, med, tall, lg
     grouped:    med, tall, lg
   ============================================ */

/* --- Grid span classes --- */
.w-shell--sm     { grid-column: span 1; grid-row: span 1; }
.w-shell--med    { grid-column: span 2; grid-row: span 1; }
.w-shell--tall   { grid-column: span 1; grid-row: span 2; }
.w-shell--lg     { grid-column: span 2; grid-row: span 2; }
.w-shell--wide   { grid-column: span 3; grid-row: span 1; }
.w-shell--xl     { grid-column: span 3; grid-row: span 2; }
.w-shell--col    { grid-column: span 1; grid-row: span 3; }
.w-shell--poster { grid-column: span 2; grid-row: span 3; }
.w-shell--max    { grid-column: span 3; grid-row: span 3; }
.w-shell--banner { grid-column: span 4; grid-row: span 1; }
.w-shell--pano   { grid-column: span 4; grid-row: span 2; }
.w-shell--cinema { grid-column: span 4; grid-row: span 3; }
.w-shell--board  { grid-column: span 2; grid-row: span 4; }
.w-shell--wall   { grid-column: span 3; grid-row: span 4; }
.w-shell--full   { grid-column: span 4; grid-row: span 4; }

/* ============================================
   Shell / Structure
   Every widget uses w-shell > w-header + w-body + w-footer
   ============================================ */

.w-shell {
  container-type: inline-size;
  display: flex;
  flex-direction: column;
  min-width: 0;
  overflow: hidden;
}

.w-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-2);
  margin-bottom: var(--space-3);
  flex-shrink: 0;
}

.w-header__left {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  flex: 1;
  min-width: 0;
}

.w-header__left > .w-text--label {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.w-header__controls {
  display: flex;
  align-items: center;
  gap: var(--space-1);
  flex-shrink: 0;
}

.w-body {
  border: var(--border-thin) solid var(--border-subtle);
  background: var(--bg-surface);
  padding: var(--space-4);
  flex: 1;
  min-height: 0;
  overflow-y: auto;
}

.w-footer {
  padding: var(--space-2) 0;
  flex-shrink: 0;
}

.w-action-bar {
  display: flex;
  gap: var(--space-2);
  flex-wrap: wrap;
}

/* Anchored actions — sits inside the body border as a visual continuation.
   Use this instead of w-footer to avoid the "hanging button" feel. */
.w-actions {
  display: flex;
  gap: var(--space-2);
  flex-wrap: wrap;
  padding: var(--space-3) var(--space-4);
  border-top: var(--border-thin) solid var(--border-subtle);
  background: var(--bg-surface);
  border-left: var(--border-thin) solid var(--border-subtle);
  border-right: var(--border-thin) solid var(--border-subtle);
  border-bottom: var(--border-thin) solid var(--border-subtle);
  margin-top: -1px; /* merge with body border */
}

/* ============================================
   ATOMS — Smallest visual units
   ============================================ */

/* --- w-text --- */
.w-text {
  font-family: var(--font-primary);
  font-size: var(--text-xs);
  color: var(--fg);
  line-height: var(--leading-tight);
  margin: 0;
}

.w-text--display {
  font-family: var(--font-serif);
  font-size: var(--text-3xl);
  line-height: var(--leading-tight);
}

.w-text--title {
  display: block;
  font-size: var(--text-lg);
  text-transform: uppercase;
  letter-spacing: var(--tracking-wide);
}

.w-text--meta {
  display: block;
  font-size: var(--text-xs);
  color: var(--fg-muted);
}

.w-text--value {
  font-size: var(--text-2xl);
  line-height: 1;
}

.w-text--label {
  font-size: var(--text-xs);
  text-transform: uppercase;
  letter-spacing: var(--tracking-wider);
  color: var(--fg-muted);
}

.w-text--note {
  font-size: var(--text-xs);
  font-style: italic;
  color: var(--fg-muted);
}

.w-text--prose {
  font-size: var(--text-lg);
  line-height: var(--leading-normal);
}

/* --- w-badge --- */
.w-badge {
  display: inline-flex;
  align-items: center;
  padding: 2px var(--space-2);
  font-family: var(--font-primary);
  font-size: 9px;
  text-transform: uppercase;
  letter-spacing: var(--tracking-wide);
  background: transparent;
  color: var(--fg);
  border: var(--border-thin) solid var(--border-subtle);
  white-space: nowrap;
}

.w-badge--filled {
  background: var(--fg);
  color: var(--bg);
  border-color: var(--fg);
}

.w-badge--accent {
  background: var(--color-warning);
  color: var(--color-black);
  border-color: var(--color-warning);
}

.w-badge--success {
  background: var(--color-success);
  color: var(--color-black);
  border-color: var(--color-success);
}

.w-badge--error {
  background: var(--color-error);
  color: var(--color-white);
  border-color: var(--color-error);
}

/* --- w-bar --- */
.w-bar {
  height: 4px;
  background: var(--border-subtle);
  overflow: hidden;
  width: 100%;
}

.w-bar__fill {
  height: 100%;
  background: var(--fg);
  width: var(--fill, 0%);
  transition: width var(--duration-slow) var(--ease-default);
}

.w-bar--thick { height: 8px; }
.w-bar--thin  { height: 2px; }

/* --- w-icon --- */
.w-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 16px;
  height: 16px;
  flex-shrink: 0;
}

.w-icon svg {
  width: 100%;
  height: 100%;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.5;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.w-icon--sm svg { width: 12px; height: 12px; }

/* --- w-icon-btn --- */
.w-icon-btn {
  width: 20px;
  height: 20px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  border: none;
  color: var(--fg-muted);
  cursor: pointer;
  transition: color var(--duration-normal) var(--ease-default);
  padding: 0;
  flex-shrink: 0;
}

.w-icon-btn svg {
  width: 14px;
  height: 14px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.5;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.w-icon-btn:hover { color: var(--fg); }

/* --- w-img --- */
.w-img {
  position: relative;
  overflow: hidden;
  background: var(--border-subtle);
  aspect-ratio: 1;
  flex-shrink: 0;
}

.w-img img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.w-img--wide   { aspect-ratio: 16 / 9; }
.w-img--banner  { aspect-ratio: 3 / 1; width: 100%; }
.w-img--thumb  { width: 40px; height: 40px; }
.w-img--card   { width: 80px; height: 80px; }
.w-img--hero   { width: 100%; aspect-ratio: 4 / 3; }

/* Placeholder when no image loaded */
.w-img__placeholder {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--fg-muted);
}

.w-img__placeholder svg {
  width: 24px;
  height: 24px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1;
}

/* --- w-divider --- */
.w-divider {
  border: none;
  border-top: var(--border-thin) solid var(--border-subtle);
  margin: var(--space-2) 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.w-divider__label {
  font-family: var(--font-primary);
  font-size: var(--text-lg);
  color: var(--fg-muted);
  background: var(--bg-surface);
  padding: 0 var(--space-2);
  position: relative;
  top: -1px;
}

.w-divider--vertical {
  border-top: none;
  border-left: var(--border-thin) solid var(--border-subtle);
  margin: 0;
  align-self: stretch;
  min-height: 100%;
}

.w-divider--labeled {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  color: var(--fg-muted);
  font-family: var(--font-primary);
  font-size: var(--text-xs);
  text-transform: uppercase;
  letter-spacing: var(--tracking-wide);
  border-top: none;
  margin: var(--space-3) 0;
}

.w-divider--labeled::before,
.w-divider--labeled::after {
  content: '';
  flex: 1;
  border-top: var(--border-thin) solid var(--border-subtle);
}

/* --- w-checkbox --- */
.w-checkbox {
  display: inline-flex;
  align-items: center;
  cursor: pointer;
  flex-shrink: 0;
}

.w-checkbox input { display: none; }

.w-checkbox__mark {
  width: 16px;
  height: 16px;
  border: var(--border-thin) solid var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background var(--duration-normal) var(--ease-default);
}

.w-checkbox input:checked + .w-checkbox__mark {
  background: var(--fg);
}

.w-checkbox input:checked + .w-checkbox__mark::after {
  content: '\2713';
  color: var(--bg);
  font-size: 11px;
  line-height: 1;
}

/* --- w-btn --- */
.w-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-1);
  padding: var(--space-1) var(--space-3);
  font-family: var(--font-primary);
  font-size: 9px;
  text-transform: uppercase;
  letter-spacing: var(--tracking-wide);
  text-decoration: none;
  background: transparent;
  color: var(--fg);
  border: var(--border-thin) solid var(--border);
  cursor: pointer;
  transition: background var(--duration-normal) var(--ease-default),
              color var(--duration-normal) var(--ease-default);
}

.w-btn:hover {
  background: var(--fg);
  color: var(--bg);
}

.w-btn--filled {
  background: var(--fg);
  color: var(--bg);
}

.w-btn--filled:hover {
  background: transparent;
  color: var(--fg);
}

.w-btn--sm {
  padding: 2px var(--space-2);
  font-size: 8px;
}

/* ============================================
   MOLECULES — Composed from atoms
   ============================================ */

/* --- w-headline --- */
.w-headline {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: var(--space-2);
  text-align: left;
}

/* --- w-tag-group --- */
.w-tag-group {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-1);
}

.w-tag-group--center { justify-content: flex-start; }

/* --- w-stat --- */
.w-stat {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--space-1);
  min-width: 60px;
  flex: 1;
}

/* --- w-row --- */
.w-row {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-2) 0;
}

.w-row + .w-row {
  border-top: var(--border-thin) solid var(--border-subtle);
}

.w-row__indicator {
  flex-shrink: 0;
  width: 20px;
  text-align: center;
  font-size: var(--text-sm);
}

.w-row__content {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.w-row__trailing {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: var(--space-1);
}

/* --- w-axis --- */
.w-axis {
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
}

.w-axis__labels { display: flex; justify-content: space-between; }

.w-axis__track {
  position: relative;
  height: 2px;
  background: var(--border-subtle);
}

.w-axis__marker {
  position: absolute;
  top: 50%;
  width: 10px;
  height: 10px;
  background: var(--fg);
  border-radius: 50%;
  transform: translate(-50%, -50%);
  transition: left var(--duration-slower) var(--ease-default);
}

.w-axis__note { text-align: left; }

/* --- w-option --- */
.w-option {
  border: var(--border-thin) solid var(--border-subtle);
  padding: var(--space-3);
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  cursor: pointer;
  transition: border-color var(--duration-normal) var(--ease-default),
              background var(--duration-normal) var(--ease-default);
}

.w-option:hover { border-color: var(--border); }

.w-option--selected {
  border-color: var(--fg);
  background: var(--bg-elevated);
}

/* --- w-section --- */
.w-section {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
}

.w-section__title { padding-bottom: var(--space-1); }

/* --- w-column --- */
.w-column {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
}

/* ============================================
   BODY LAYOUT MODIFIERS
   Applied to .w-body to control internal layout
   ============================================ */

/* Verdict: headline + tags */
.w-body--verdict {
  text-align: left;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  justify-content: center;
  gap: var(--space-3);
  padding: var(--space-6) var(--space-4);
}

/* List: vertical stack of rows */
.w-body--list {
  display: flex;
  flex-direction: column;
}

.w-body--list > .w-row:first-child {
  padding-top: 0;
  border-top: none;
}

/* Spectrum: stacked axes */
.w-body--spectrum {
  display: flex;
  flex-direction: column;
  gap: var(--space-4);
}

/* Split: two columns with divider */
.w-body--split {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  gap: 0;
  padding: 0;
}

.w-body--split > .w-column {
  padding: var(--space-4);
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
}

/* Narrative: prose text */
.w-body--narrative {
  padding: var(--space-4);
}

.w-body--narrative .w-text--prose {
  margin-bottom: var(--space-3);
}

.w-body--narrative .w-text--prose:last-child {
  margin-bottom: 0;
}

/* Comparison: two options side by side with labeled divider */
.w-body--comparison {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: stretch;
  gap: 0;
  padding: 0;
}

.w-body--comparison > .w-option {
  border: none;
}

.w-body--comparison > .w-divider--labeled {
  writing-mode: vertical-lr;
  margin: 0;
  padding: var(--space-2);
  flex-direction: column;
}

.w-body--comparison > .w-divider--labeled::before,
.w-body--comparison > .w-divider--labeled::after {
  border-top: none;
  border-left: var(--border-thin) solid var(--border-subtle);
}

/* Choices: stacked selectable options */
.w-body--choices {
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
}

/* Checklist: rows with checkboxes + running total */
.w-body--checklist {
  display: flex;
  flex-direction: column;
}

/* Suggestion: featured single item */
.w-body--suggestion {
  display: flex;
  flex-direction: row;
  gap: var(--space-4);
  align-items: flex-start;
}

/* Grouped: labeled sections */
.w-body--grouped {
  display: flex;
  flex-direction: column;
  gap: var(--space-4);
}

/* --- w-items --- */
.w-items {
  display: flex;
  gap: var(--space-3);
  flex-wrap: wrap;
}

/* --- w-item --- */
.w-item {
  flex: 0 0 auto;
  width: 100px;
  text-decoration: none;
  color: inherit;
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
  transition: transform var(--duration-normal) var(--ease-default),
              box-shadow var(--duration-normal) var(--ease-default);
}

.w-item:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

/* ============================================
   BODY MODIFIER: Stats row
   ============================================ */

.w-body--stats {
  display: flex;
  gap: var(--space-3);
  justify-content: space-around;
}

/* ============================================
   LOADING STATE
   ============================================ */

.w-body--loading {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 80px;
}

.w-loader {
  font-family: var(--font-primary);
  font-size: var(--text-xs);
  text-transform: uppercase;
  letter-spacing: var(--tracking-wider);
  color: var(--fg-muted);
  animation: w-pulse 1.5s ease-in-out infinite;
}

@keyframes w-pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.4; }
}

/* ============================================
   CONTAINER QUERY RESPONSIVE
   Adapts layouts based on actual widget width.
   Breakpoints map to grid sizes:
     < 180px  = sm compressed (1x1 in 5-col grid)
     < 280px  = sm standard
     < 400px  = med or tall
     >= 400px = lg, wide, xl
   ============================================ */

/* --- SM: Compressed (< 180px) --- */
@container (max-width: 180px) {
  .w-header__left > .w-badge { display: none; }

  .w-text--display { font-size: var(--text-xl); }
  .w-text--title   { font-size: var(--text-xs); }
  .w-text--value   { font-size: var(--text-lg); }
  .w-text--prose   { font-size: var(--text-xs); }

  .w-body { padding: var(--space-2); }

  .w-body--verdict { padding: var(--space-3) var(--space-2); gap: var(--space-2); align-items: flex-start; }

  .w-img--card { width: 48px; height: 48px; }
  .w-img--hero { aspect-ratio: 3 / 2; }

  .w-item { width: 60px; }
  .w-item .w-img { height: 60px; }
  .w-items { gap: var(--space-2); }

  .w-body--list > .w-row { gap: var(--space-2); }
  .w-row__indicator { display: none; }
  .w-row__trailing  { display: none; }

  .w-body--spectrum { gap: var(--space-2); }
  .w-axis__labels .w-text { font-size: 8px; }

  .w-body--choices > .w-option { padding: var(--space-2); }

  .w-tag-group { gap: 2px; }
  .w-badge { font-size: 7px; padding: 1px var(--space-1); }

  .w-footer { display: none; }
}

/* --- SM: Standard (< 280px) --- */
@container (max-width: 280px) {
  .w-text--display { font-size: var(--text-2xl); }
  .w-text--prose   { font-size: var(--text-sm); }

  .w-body--split {
    grid-template-columns: 1fr;
  }

  .w-body--split > .w-divider--vertical {
    border-left: none;
    border-top: var(--border-thin) solid var(--border-subtle);
    min-height: 0;
    align-self: auto;
  }

  .w-body--comparison {
    grid-template-columns: 1fr;
  }

  .w-body--comparison > .w-divider--labeled {
    writing-mode: horizontal-tb;
    flex-direction: row;
  }

  .w-body--comparison > .w-divider--labeled::before,
  .w-body--comparison > .w-divider--labeled::after {
    border-left: none;
    border-top: var(--border-thin) solid var(--border-subtle);
  }

  .w-item { width: 80px; }
  .w-item .w-img { height: 80px; }

  .w-body--suggestion {
    flex-direction: column;
  }

  .w-body--narrative .w-tag-group { display: none; }

  .w-action-bar > .w-btn:not(:first-child) { display: none; }
}

/* --- MED/TALL: Standard (280-400px) — default styles apply --- */

/* --- LG+: Expanded (> 400px) --- */
@container (min-width: 400px) {
  .w-text--display { font-size: 28px; }

  .w-body--verdict {
    padding: var(--space-8) var(--space-6);
    gap: var(--space-4);
    align-items: flex-start;
  }

  .w-body--spectrum { gap: var(--space-6); }
  .w-body--grouped  { gap: var(--space-6); }

  .w-body--stats {
    gap: var(--space-6);
  }

  .w-body--choices {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
    gap: var(--space-3);
  }

  .w-body--narrative { padding: var(--space-6); }
  .w-text--prose { font-size: var(--text-xl); }
}

/* --- WIDE: Strip (> 560px) --- */
@container (min-width: 560px) {
  .w-body--list {
    columns: 2;
    column-gap: var(--space-6);
  }

  .w-body--list > .w-row {
    break-inside: avoid;
  }
}
