/*
 * Mamore UI v2 — layout.
 *
 * The 12-column grid, the spacing scale and the shell regions. Component styles
 * live in components.css; this file is only about how space is divided.
 *
 * Breakpoints follow docs/ui-v2/RESPONSIVE_STRATEGY.md. Mobile-first: each rule
 * is the MINIMUM the layout needs, and wider viewports add capability.
 */

/* ------------------------------------------------------------- 12-col grid
 * `.grid` establishes the columns; `.col-*` places an item. Column count drops
 * 12 -> 8 -> 4 as width falls, so a dashboard reflows instead of shrinking. */
.grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: var(--space-5);
}

@media (min-width: 768px)  { .grid { grid-template-columns: repeat(8, minmax(0, 1fr)); } }
@media (min-width: 1280px) { .grid { grid-template-columns: repeat(12, minmax(0, 1fr)); } }

/* ------------------------------------------------------------- KPI rows
 * A row of KPI cards is NOT a page of dashboard columns. Putting .metric
 * straight into .grid handed each card ONE of twelve tracks, so at 1280px and
 * above every card was about 100px wide and a value like "1,250,000" had to
 * break mid-figure -- the cards were unreadable exactly where there is the most
 * room for them.
 *
 * auto-fit with a floor is the whole fix: the row is as wide as the container
 * allows and always fits as many cards as it can at a readable size, which is
 * the 4-up -> 2-up -> 1-up progression docs/ui-v2/RESPONSIVE_STRATEGY.md
 * requires. The `min(100%, ...)` term is what stops the floor from forcing a
 * horizontal scrollbar on a 320px phone, where 100% is narrower than the floor.
 * Order is never reshuffled, because KPI order is priority order.
 */
.kpi-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 12.5rem), 1fr));
  gap: var(--space-5);
}

/* Command-centre intelligence strip (§37): the same grid language as the KPI
 * row, but each item is a drill button, so a reading is one press away from
 * the panel that holds its detail. */
.tmap__strip {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 9.5rem), 1fr));
  gap: var(--space-3);
  margin-bottom: var(--space-5);
}

.tmap__strip-item {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  padding: var(--space-4);
  text-align: left;
  background: var(--surface);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-md, 10px);
  color: var(--text-primary);
  cursor: pointer;
}

.tmap__strip-item span {
  color: var(--text-muted);
  font-size: var(--text-caption);
}

.tmap__strip-item strong {
  font-size: var(--text-h3);
}

.tmap__strip-item:hover { border-color: var(--accent); }
.tmap__strip-item--good strong { color: var(--tone-good, var(--accent)); }
.tmap__strip-item--warn strong { color: var(--tone-warn, var(--gold)); }

/* Column spans are declared per tier, because an item spanning 6 of 12 columns
 * must span 4 of 8 to keep the same visual weight rather than take half a row. */
.col-full            { grid-column: 1 / -1; }
.col-half            { grid-column: span 2; }
.col-third           { grid-column: span 2; }
.col-two-thirds      { grid-column: span 4; }
.col-quarter         { grid-column: span 2; }
.col-three-quarters  { grid-column: span 6; }

@media (min-width: 768px) {
  .col-half           { grid-column: span 4; }
  .col-third          { grid-column: span 4; }
  .col-two-thirds     { grid-column: span 6; }
  .col-quarter        { grid-column: span 4; }
  .col-three-quarters { grid-column: span 8; }
}

@media (min-width: 1280px) {
  .col-half           { grid-column: span 6; }
  .col-third          { grid-column: span 4; }
  .col-two-thirds     { grid-column: span 8; }
  .col-quarter        { grid-column: span 3; }
  .col-three-quarters { grid-column: span 9; }

  /* Fractions of a 12-column row, used by the layouts in
     docs/ui-v2/DASHBOARD_ARCHITECTURE.md. */
  .col-1  { grid-column: span 1; }
  .col-2  { grid-column: span 2; }
  .col-5  { grid-column: span 5; }
  .col-7  { grid-column: span 7; }
  .col-8  { grid-column: span 8; }
  .col-9  { grid-column: span 9; }
  .col-11 { grid-column: span 11; }
}

/* ------------------------------------------------------------ stack & flow
 * A 4-based spacing scale, used instead of arbitrary margins so vertical rhythm
 * is consistent across 23 panels without anyone tuning each one. */
.stack      { display: flex; flex-direction: column; gap: var(--space-4); }
.stack-sm   { display: flex; flex-direction: column; gap: var(--space-2); }
.stack-lg   { display: flex; flex-direction: column; gap: var(--space-6); }

.cluster     { display: flex; flex-wrap: wrap; align-items: center; gap: var(--space-3); }
.cluster-sm  { display: flex; flex-wrap: wrap; align-items: center; gap: var(--space-2); }
.cluster-end { display: flex; flex-wrap: wrap; align-items: center; justify-content: flex-end; gap: var(--space-3); }
.spread      { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: var(--space-4); }

/* --------------------------------------------------------------- shell
 * The application frame. app.css defines .app-shell/.sidebar/.topbar today with
 * a fixed 16rem sidebar and no collapsed state; these are the token-driven
 * versions, loaded after app.css so they win. */
.shell {
  display: grid;
  grid-template-columns: var(--sidebar-collapsed) minmax(0, 1fr);
  min-height: 100vh;
  background: var(--bg-app);
}

.shell__sidebar {
  position: sticky;
  top: 0;
  z-index: var(--z-sidebar);
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow-y: auto;
  color: var(--text-inverse);
  background: var(--surface-nav);
  border-right: 1px solid var(--border-nav);
}

.shell__main { display: flex; flex-direction: column; min-width: 0; }

.shell__content {
  flex: 1;
  width: 100%;
  max-width: var(--content-max);
  margin-inline: auto;
  padding: var(--space-6) var(--space-6) var(--space-12);
}

@media (min-width: 768px) {
  .shell { grid-template-columns: var(--sidebar-width) minmax(0, 1fr); }
  .shell__content { padding: var(--space-8) var(--space-8) var(--space-16); }
}

@media (min-width: 1536px) { .shell__content { padding-inline: var(--space-10); } }

@media (max-width: 767px) {
  /* Mobile: the sidebar becomes an overlay drawer rather than a column. */
  .shell { grid-template-columns: minmax(0, 1fr); }
  .shell__sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    width: min(19rem, 86vw);
    box-shadow: var(--shadow-overlay);
  }
}

/* -------------------------------------------------------------- page header
 * Every major page gets one, so the operator always knows which domain they are
 * in and how fresh the data is. */
.page-header {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  justify-content: space-between;
  gap: var(--space-4);
  margin-bottom: var(--space-6);
}

.page-header__title { display: flex; flex-direction: column; gap: var(--space-1); min-width: 0; }
.page-header__actions { display: flex; flex-wrap: wrap; align-items: center; gap: var(--space-3); }

/* --------------------------------------------------------------- freshness
 * A first-class design element: every data surface declares how old it is.
 * The thresholds are computed in JS, but the four visual states are defined
 * here so no component can invent its own. */
.freshness {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  color: var(--text-muted);
  font-size: var(--text-caption);
}

.freshness::before {
  content: "";
  width: var(--space-2);
  height: var(--space-2);
  background: currentColor;
  border-radius: 50%;
}

.freshness[data-state="live"]    { color: var(--tone-success); }
.freshness[data-state="delayed"] { color: var(--tone-warning); }
.freshness[data-state="stale"]   { color: var(--tone-danger); }
.freshness[data-state="offline"] { color: var(--tone-neutral); }

/* The only animation in the layout layer, and it is a single element. */
.freshness[data-state="live"]::before {
  animation: freshness-pulse 2.4s var(--ease-in-out, ease-in-out) infinite;
}

@keyframes freshness-pulse {
  0%, 100% { opacity: 1; }
  50%      { opacity: 0.35; }
}

