/* Shared window boundaries; every app's own layout starts at these edges. */
.window {
  --app-paper: #e4edf7;
  --app-chrome: #d3dfed;
  --app-pane: #c8d8e9;
  --app-divider: #9bafc6;
  --app-ink: #2f4967;
  --app-mark: #6d89a8;
  position: absolute;
  container: tile / size;
  padding: 0;
  overflow: hidden;
  border: 1px solid #92a8c2;
  background: var(--app-paper);
  color: var(--app-ink);
  text-align: left;
}
:where(button).window:hover { border-color: var(--accent); }
.window.is-focused { border-color: #2e4868; z-index: 1; }
.window::after { content: ""; position: absolute; inset: 0; pointer-events: none; }
:where(button).window:hover::after { box-shadow: inset 0 0 0 1px var(--accent); }
.window.is-focused::after { box-shadow: inset 0 0 0 2px #2e4868; }
.window:focus-visible::after { outline: 2px dashed #2e4868; outline-offset: -5px; }
.window.pointer-focused:focus-visible,
.window.pointer-focused:focus-visible::after { outline: none; }
.screen .divider { z-index: 2; }

.app-ui, .app-compact { position: absolute; inset: 0; overflow: hidden; pointer-events: none; }
.app-ui {
  --app-pad: clamp(5px, 4cqw, 13px);
  --app-row: clamp(16px, 14cqh, 25px);
  display: flex;
  flex-direction: column;
  font-size: clamp(8px, 5.5cqw, 11px);
  line-height: 1.35;
}
.app-compact { display: none; }
.app-bar, .app-rail {
  display: flex;
  gap: 5px;
  background: var(--app-chrome);
  white-space: nowrap;
  overflow: hidden;
}
.app-bar { flex: none; height: var(--app-row); align-items: center; padding-inline: 6px; border-bottom: 1px solid var(--app-divider); }
.app-tab {
  align-self: stretch;
  display: grid;
  align-items: center;
  padding-inline: 6px;
  background: var(--app-paper);
  border-inline: 1px solid var(--app-divider);
  font-weight: 650;
}
.app-dim-tab { color: var(--muted); padding-inline: 4px; }
.app-rail { flex: none; flex-direction: column; border-right: 1px solid var(--app-divider); }
.app-rail > span { overflow: hidden; text-overflow: ellipsis; }
.app-current { background: #a9c0d9; font-weight: 650; }
.app-main { display: flex; flex: 1; min-width: 0; flex-direction: column; }
.app-line { display: block; height: 2px; flex-shrink: 0; background: var(--app-mark); opacity: .7; }

/* Terminal and editor: text runs in the primary pane, not in an inset drawing. */
[data-app="terminal"] { background: #334e6b; color: #e4edf7; }
.window[data-app="terminal"].is-focused::after { box-shadow: inset 0 0 0 2px #a6c2e1; }
[data-app="terminal"].window:focus-visible::after { outline-color: #e4edf7; }
.terminal-log, .code-source { display: grid; align-content: start; font-family: ui-monospace, monospace; white-space: nowrap; }
.terminal-log { gap: .25em; padding: var(--app-pad); }
.terminal-log b { color: #a9c9eb; }
.app-caret { display: inline-block; visibility: hidden; height: 1em; vertical-align: text-bottom; }
.window.is-focused .app-caret { visibility: visible; }
.terminal-cursor { width: .65em; background: #a9c9eb; }
.code-cursor { width: max(1px, .12em); margin-left: 1px; background: currentColor; }
[data-app="code"] .app-ui, [data-app="image"] .app-ui { flex-direction: row; }
.code-tree, .files-tree {
  gap: 1px;
  width: clamp(40px, 28cqw, 124px);
  padding-block: 4px;
}
.code-tree > *, .files-tree > * { padding: 2px 5px; }
.code-source { gap: .25em; padding: var(--app-pad) 5px; }
.code-source b { color: #365b84; }
.code-source i { margin-right: .65em; color: var(--muted); font-style: normal; }

/* Browser and file manager: their own chrome is the tile's top edge. */
.browser-toolbar, .files-path { background: #dbe7f3; }
.browser-address {
  min-width: 0;
  flex: 1;
  padding-inline: 5px;
  border: 1px solid var(--app-divider);
  border-radius: 3px;
  background: var(--app-paper);
  overflow: hidden;
  text-overflow: ellipsis;
}
.browser-page { flex: 1; overflow: hidden; }
.browser-banner {
  display: grid;
  gap: 5px;
  align-content: center;
  min-height: 52%;
  padding: var(--app-pad);
  background: var(--app-pane);
}
.browser-banner b { font-size: clamp(11px, 8cqw, 24px); }
.browser-columns { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px 12px; padding: var(--app-pad); }
.files-body { display: flex; flex: 1; min-height: 0; }
.file-grid {
  display: grid;
  flex: 1;
  min-width: 0;
  grid-template-columns: repeat(auto-fit, minmax(min(32px, 100%), 1fr));
  align-content: start;
  gap: 8px 4px;
  padding: var(--app-pad) 4px;
  text-align: center;
}
.file-grid > span { overflow: hidden; text-overflow: ellipsis; }
.file-grid svg {
  display: block;
  width: min(100%, 28px);
  margin-inline: auto;
  fill: var(--app-pane);
  stroke: currentColor;
  stroke-width: .8;
}

/* The worksheet grid repeats at a fixed cell height even in a tall tile. */
.sheet-formula i { padding-inline: 4px; border-inline: 1px solid var(--app-divider); }
.sheet-grid {
  --cell-height: clamp(15px, 12cqh, 25px);
  --data-columns: 4;
  --column-pitch: calc(100% / var(--data-columns));
  flex: 1;
  overflow: hidden;
  background: repeating-linear-gradient(to bottom, transparent 0 calc(var(--cell-height) - 1px),
    var(--app-divider) calc(var(--cell-height) - 1px) var(--cell-height));
  position: relative;
}
.sheet-grid::after {
  content: "";
  position: absolute;
  inset: var(--cell-height) 0 0 16%;
  background: repeating-linear-gradient(to right, var(--app-divider) 0 1px,
    transparent 1px var(--column-pitch));
  pointer-events: none;
}
.sheet-row { display: grid; grid-template-columns: 16% repeat(var(--data-columns), minmax(0, 1fr)); height: var(--cell-height); }
.sheet-row > span { display: grid; align-items: center; padding-inline: 3px; white-space: nowrap; overflow: hidden; }
.sheet-row > :first-child, .sheet-head > span { background: var(--app-chrome); box-shadow: inset -1px -1px var(--app-divider); }
.sheet-row > :first-child, .sheet-head { text-align: center; }
.sheet-row:nth-child(3) > :nth-child(3) { background: #b8cde3; outline: 1px solid var(--accent); outline-offset: -2px; }

.image-tools { width: clamp(16px, 16cqw, 30px); align-items: center; padding-block: 4px; }
.image-canvas { position: relative; flex: 1; overflow: hidden; background: #bfd2e7; }
.image-canvas svg { position: absolute; inset: 0; width: 100%; height: 100%; fill: #a0b9d5; stroke: #5d7da1; }
.image-canvas path { vector-effect: non-scaling-stroke; }
.image-canvas svg path:last-child { fill: #dbe7f3; }
.image-sun {
  position: absolute;
  top: 18%;
  left: 66%;
  width: clamp(6px, 12cqw, 28px);
  aspect-ratio: 1;
  border: 1px solid #5d7da1;
  border-radius: 50%;
  background: #dbe7f3;
}
.image-guides {
  position: absolute;
  inset: 12%;
  border: 1px dashed #42658b;
  background: linear-gradient(to right, transparent 49.6%, #5d7da14a 49.6% 50.4%, transparent 50.4%),
    linear-gradient(to bottom, transparent 49.6%, #5d7da14a 49.6% 50.4%, transparent 50.4%);
}

[data-app="solitaire"] { background: #bacee3; }
[data-app="solitaire"] .app-ui { gap: var(--app-pad); padding: var(--app-pad); }
.card-row { display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); gap: clamp(2px, 2cqw, 8px); }
.playing-card, .card-slot {
  height: clamp(16px, 36cqh, 112px);
  padding: 2px;
  border: 1px solid #829dbc;
  border-radius: 3px;
  overflow: hidden;
  white-space: nowrap;
}
.playing-card { background: var(--app-paper); }
.card-back { background: #93b0d0; }
.card-slot { border-style: dashed; }

.notes-page { display: grid; align-content: start; gap: 7px; padding: var(--app-pad); flex: 1; }
.notes-page b { font-size: clamp(10px, 12cqh, 17px); }
.music-track { display: flex; align-items: center; flex: 1; min-height: 0; gap: var(--app-pad); padding-inline: var(--app-pad); overflow: hidden; }
.music-turntable {
  position: relative;
  flex: none;
  width: clamp(18px, min(39cqw, 42cqh), 126px);
  max-width: 100%;
  aspect-ratio: 100 / 78;
  border: 1px solid var(--app-divider);
  border-radius: 4px;
  background: var(--app-chrome);
}
.music-record {
  position: absolute;
  inset: 10% auto auto 7%;
  display: grid;
  place-items: center;
  width: 62%;
  aspect-ratio: 1;
  border-radius: 50%;
  background: repeating-radial-gradient(circle closest-side, #27394e 0 8%, #394f68 8% 11%);
}
.music-record img {
  display: block;
  width: 40%;
  border: 1px solid #9ab1ca;
  border-radius: 50%;
  filter: grayscale(1) sepia(.35) hue-rotate(170deg) saturate(1.4);
}
.music-tonearm { position: absolute; inset: 0; width: 100%; height: 100%; }
.music-name { display: grid; white-space: nowrap; }
.music-wave { display: flex; flex: 1; height: 52%; align-items: end; justify-content: end; gap: 3px; }
.music-wave > span { width: clamp(1px, 1cqw, 6px); background: var(--app-mark); transform-origin: bottom; }
.music-controls { border-top: 1px solid var(--app-divider); border-bottom: 0; gap: var(--app-pad); }
.music-progress {
  position: relative;
  height: 10px;
  flex: 1;
  overflow: hidden;
  background: linear-gradient(var(--app-divider), var(--app-divider)) center / 100% 2px no-repeat;
}
.music-playhead {
  position: absolute;
  inset: 4px 0;
  background: var(--accent);
  transform: translateX(-65%);
}
.music-playhead::after {
  content: "";
  position: absolute;
  top: 50%;
  right: 0;
  width: 6px;
  aspect-ratio: 1;
  border-radius: 50%;
  background: var(--accent);
  transform: translate(50%, -50%);
}

@media (prefers-reduced-motion: no-preference) {
  .window.is-focused .app-caret { animation: caret-blink 1.1s step-end infinite; }
  .music-record { animation: record-spin 2.8s linear var(--music-delay) infinite; }
  .music-playhead { animation: track-progress 24s linear var(--music-delay) infinite; }
  .music-wave > span { animation: equalizer var(--beat) ease-in-out calc(var(--music-delay) - var(--beat-phase)) infinite alternate; }
}
@keyframes caret-blink {
  0%, 100% { opacity: 1; }
  52% { opacity: 0; }
}
@keyframes record-spin { to { transform: rotate(1turn); } }
@keyframes track-progress { from { transform: translateX(-100%); } to { transform: translateX(0); } }
@keyframes equalizer {
  0%, 100% { transform: scaleY(.28); }
  36% { transform: scaleY(1); }
  72% { transform: scaleY(.52); }
}

.calendar-bar { justify-content: space-between; }
.calendar-days { display: grid; flex: 1; min-height: 0; grid-template-columns: repeat(7, minmax(0, 1fr)); grid-auto-rows: minmax(12px, 1fr); overflow: hidden; }
.calendar-days > span { display: grid; place-items: center; box-shadow: inset -1px -1px var(--app-divider); }
.calendar-days > :nth-child(-n + 7) { background: #dbe7f3; font-weight: 650; }

/* Compact panes keep the recognizable parts and drop secondary controls. */
@container tile (aspect-ratio < 1) and (height >= 104px) {
  .music-track { flex-direction: column; justify-content: center; text-align: center; }
  .music-turntable { width: clamp(26px, min(86cqw, 42cqh), 126px); }
  .music-name { white-space: normal; }
  .music-wave { flex: none; width: 100%; height: clamp(12px, 17cqh, 44px); justify-content: center; gap: 2px; }
}
@container tile (width < 93px) {
  .code-tree, .files-tree, .browser-columns { display: none; }
  .sheet-grid { --data-columns: 2; }
  .sheet-row > :nth-child(n + 4) { display: none; }
  .file-grid svg { width: min(100%, 18px); }
  .files-path > :last-child { overflow: hidden; text-overflow: ellipsis; }
}
@container tile (height < 72px) {
  .browser-tabs, .card-deal, .music-controls, .sheet-formula { display: none; }
}
@container tile (height < 38px) {
  .notes-page { gap: 3px; padding-block: 2px; }
  .calendar-days { grid-auto-rows: minmax(9px, 1fr); font-size: 7px; }
}
@container tile (width < 46px) or (height < 18px) {
  .app-ui { display: none; }
  .app-compact { display: flex; justify-content: center; align-items: center; gap: 4px; }
  .app-compact svg { flex: none; width: min(15px, 50cqw, 76cqh); }
  .app-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 9px; }
}
@container tile (width < 82px) or (height < 13px) {
  .app-name { display: none; }
}
