/* The dashboard as a layer surface (js/layers/layer-host.js): a transparent window on
   the desktop, in the overlay or anchored to an app, showing only the tiles
   placed on it. Everything else the page builds stays in the DOM, so each tile
   keeps its code and data feed, but is never drawn. */

/* !important: themes paint html/body with their own, more specific rules, and a
   layer window must stay see-through whatever theme is active. */
html[data-layer],
html[data-layer] body {
  background: transparent !important;
}

/* [data-layer-sheet]: a dialog a tile opened on the body (js/layers/layer-host.js). */
html[data-layer] body > :not(#layer-root):not(#picker-overlay):not([data-layer-sheet]) {
  display: none !important;
}

/* The device picker opens over the tile that asked for it, not over the screen:
   the window shows, and takes clicks on, only the tiles' rectangles. The
   rectangle is the slot last pressed (js/layers/layer-host.js). */
html[data-layer] #picker-overlay {
  inset: auto;
  left: var(--layer-sheet-x, 0px);
  top: var(--layer-sheet-y, 0px);
  width: var(--layer-sheet-w, 100%);
  height: var(--layer-sheet-h, 100%);
  padding: 8px;
}

/* The System tile's Optimize button opens a dashboard panel the light page
   does not have (js/layers/layer-lite-tiles.js). */
html[data-layer-lite] .sys-optimize-btn {
  display: none !important;
}

.layer-root {
  position: fixed;
  inset: 0;
  /* Clicks pass through the empty parts of the window; the shell makes the
     window itself click-through where no tile is. */
  pointer-events: none;
  overflow: hidden;
}

.layer-slot {
  /* The rectangle is a fraction of the screen, so on a small or portrait screen
     a tile could shrink below what it can draw: keep a usable minimum, and keep
     the grown box on screen by sliding it back from the far edges. */
  --layer-min-w: 160px;
  --layer-min-h: 120px;
  --layer-box-w: min(100%, max(var(--layer-w, 20%), var(--layer-min-w)));
  --layer-box-h: min(100%, max(var(--layer-h, 30%), var(--layer-min-h)));
  position: absolute;
  left: clamp(0px, var(--layer-x, 0%), 100% - var(--layer-box-w));
  top: clamp(0px, var(--layer-y, 0%), 100% - var(--layer-box-h));
  width: var(--layer-box-w);
  height: var(--layer-box-h);
  opacity: var(--layer-opacity, 1);
  pointer-events: auto;
  display: flex;
  min-width: 0;
  min-height: 0;
}

/* A built-in tile needs the minimum above; a Store widget lays itself out, down
   to one button the size of a comfortable touch target. */
.layer-slot[data-small] {
  --layer-min-w: 48px;
  --layer-min-h: 48px;
}

/* One that sizes its own box (js/layers/layer-fit.js) keeps the size it asked for, or the
   room under a short readout showed as a dark band. */
.layer-slot[data-small][data-fit]:not(:has(.cw-state)) {
  --layer-min-w: 16px;
  --layer-min-h: 16px;
}

/* Asking for permission needs room to be read and answered. */
.layer-slot[data-small]:has(.cw-state) {
  --layer-min-w: 220px;
  --layer-min-h: 140px;
}
.layer-slot[data-small] .cw-state {
  background: var(--surface);
  border-radius: 10px;
}
.layer-slot[data-small]:has(.cw-state) .cw-head,
.layer-slot[data-small] .cw-state-ico,
.layer-slot[data-small] .cw-state-hint,
.layer-slot[data-small] .cw-state-btns .cw-btn:not(.cw-btn--primary) { display: none; }
.layer-slot[data-small] .cw-state-btns .cw-btn { min-height: 44px; }

/* A box over a game has no room for the dashboard's padding. */
.layer-slot .perf-widget-mount { padding: 8px 10px; }

.layer-slot > [data-dashboard-widget] {
  flex: 1 1 auto;
  width: 100%;
  height: 100%;
  min-width: 0;
  min-height: 0;
  margin: 0;
  position: relative;
}

/* A Store widget open at full size (the SDK `expand`, js/custom-widget/custom-widget.js) fills
   the window: the box grows with it, so the window is cut to all of it and takes
   every click, and it covers the other widgets at full opacity. */
.layer-slot:has(.cw-wrap.cw-expanded) {
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  opacity: 1;
  z-index: 1;
}

/* A widget can be smaller than any dashboard tile. The System tile shares its height
   out among the cards, which on a 320x240 widget left each one 33px for 60px of words
   and cut the numbers. Here a card keeps the height its words need, spare height is
   still shared out, and what does not fit scrolls. The selectors are this long to beat
   DashboardLayout.css, which closes the panel for the dashboard. */
body:not([data-panel]) .layer-slot .side-panel { overflow: hidden auto; }
body:not([data-panel]) .layer-slot .side-panel .system-block { flex: 1 0 auto; }
body:not([data-panel]) .layer-slot .side-panel .system-grid {
  flex: 1 0 auto;
  /* Two cards side by side until each would be under 96px: then one under the other. */
  grid-template-columns: repeat(auto-fit, minmax(max(96px, calc(50% - 4px)), 1fr));
  grid-auto-rows: minmax(min-content, 1fr);
  overflow: visible;
}
/* The Volume tab goes side by side when short, for the wide strip of the Edge. A widget is
   short and narrow, and the card then ran into the device rows: here it stays stacked. */
body:not([data-panel]) .layer-slot .system-audio-pane:not([hidden]) { container: none; flex: 1 0 auto; }
.layer-slot .system-audio-pane .device-section {
  grid-template-columns: repeat(auto-fit, minmax(max(96px, calc(50% - 4px)), 1fr));
}
/* At the smallest size even the icons alone ran past the right edge: they share the row. */
.layer-slot .system-tabs[data-fit="3"] .system-tabs-left { flex: 1 1 auto; }
.layer-slot .system-tabs[data-fit="3"] .sys-tab { flex: 1 1 0; min-width: 0; padding-left: 0; padding-right: 0; }

/* Without its panel, by the person's choice: only what the widget paints. A built-in
   tile's words get an edge, as nothing behind them promises any contrast. */
.layer-slot[data-bare] > [data-dashboard-widget] {
  background: none;
  border: 0;
  box-shadow: none;
  backdrop-filter: none;
  -webkit-backdrop-filter: none;
  text-shadow: 0 0 2px #000, 0 1px 2px #000;
}
/* Dark words take a light edge: a dark one smears them. */
:root[data-appearance="light"] .layer-slot[data-bare] > [data-dashboard-widget] { text-shadow: 0 0 2px #fff, 0 1px 2px #fff; }
.layer-slot[data-bare] > [data-dashboard-widget]::before,
.layer-slot[data-bare] > [data-dashboard-widget]::after { content: none; }

/* Widgets grouped as tabs: the dashboard's tab-group tile (DashboardGrid.css). */
.layer-slot > .layer-tabs {
  flex: 1 1 auto;
  min-width: 0;
  margin: 0;
}

/* "Show when": game-running is set by the game detector on every surface. While
   editing every widget stays in sight, or a hidden one could never be moved: it is
   dimmed instead, so the screen still says what shows right now. `data-peek`: its screen
   is open in the menu, and a change made there has to be seen (server/layers/layers-peek.js). */
html:not([data-layer-editing]) body:not(.game-running) .layer-slot[data-show="gaming"]:not([data-peek]),
html:not([data-layer-editing]) body.game-running .layer-slot[data-show="notGaming"]:not([data-peek]),
html:not([data-layer-editing]) .layer-slot[data-when-off]:not([data-peek]) {
  display: none;
}

html[data-layer-editing] body:not(.game-running) .layer-slot[data-show="gaming"]:not([data-peek]) > [data-dashboard-widget],
html[data-layer-editing] body.game-running .layer-slot[data-show="notGaming"]:not([data-peek]) > [data-dashboard-widget],
html[data-layer-editing] .layer-slot[data-when-off]:not([data-peek]) > [data-dashboard-widget] {
  opacity: 0.45;
}

/* ── Edit mode (js/layer-edit/layer-edit.js) ─────────────────────────────────────────────
   The window takes the clicks on its widgets and on what edit mode adds, no more:
   another window under it stays reachable. The frames show what can be dragged. */
.layer-edit-frame {
  position: absolute;
  left: var(--layer-edit-left, 0px);
  right: var(--layer-edit-right, 0px);
  bottom: var(--layer-edit-bottom, 0px);
  top: var(--layer-edit-top, 0px);
  z-index: 20;
  cursor: move;
  touch-action: none;
  border: 2px dashed rgba(var(--accent-rgb), 0.75);
  border-radius: 14px;
  /* The widget recedes while it is being arranged: the frame's controls are
     what can be pressed, not the widget under them. */
  background: rgba(0, 0, 0, 0.38);
}

.layer-edit-frame.is-dragging {
  background: rgba(0, 0, 0, 0.24);
}

/* Words have nothing to recede from: a dark box behind them reads as a black bar. */
.layer-edit-frame.is-words,
.layer-edit-frame.is-words.is-dragging {
  background: none;
}

.layer-edit-frame.is-picked {
  border-style: solid;
}

/* When it shows, on every frame: outside it, so even a line of words keeps its label. */
.layer-edit-tag {
  position: absolute;
  bottom: calc(100% + 4px);
  left: 0;
  max-width: 240px;
  overflow: hidden;
  padding: 2px 8px;
  border-radius: 6px;
  color: var(--text);
  background: var(--surface, var(--panel));
  font-size: 12px;
  font-weight: 600;
  line-height: 18px;
  text-overflow: ellipsis;
  white-space: nowrap;
  pointer-events: none;
}

.layer-slot[data-v="top"] > .layer-edit-frame > .layer-edit-tag {
  top: calc(100% + 4px);
  bottom: auto;
}

.layer-slot[data-h="right"] > .layer-edit-frame > .layer-edit-tag {
  left: auto;
  right: 0;
}

/* The line a box is pulled to, while it is dragged. */
.layer-edit-guide {
  position: absolute;
  z-index: 25;
  background: rgba(var(--accent-rgb), 0.9);
  pointer-events: none;
}

.layer-edit-guide--x {
  top: 0;
  bottom: 0;
  left: var(--layer-edit-at, 0px);
  width: 1px;
}

.layer-edit-guide--y {
  left: 0;
  right: 0;
  top: var(--layer-edit-at, 0px);
  height: 1px;
}
