/* ===== mobile: the same console as a phone app =====
 *
 * Below the breakpoint the frame turns from "command bar + left rail + page" into
 * "command bar + page + bottom tab bar", and the Ops control column and the result dock become
 * bottom sheets over a full-screen map. Nothing here changes what a control DOES — only where
 * it sits and how large it is under a finger.
 *
 * Every number that defines the frame comes from the formal specification
 * `lean/design/Design.lean` (breakpoint 760, command bar 56, tab bar 60, gutter 16, touch
 * target 44, sheet 148 / 55 % / 12). `lean/design/check_design.py` measures the rendered page
 * against that specification; change a value there first, then here.
 */

/* tokens shared by both shapes (the desktop values) */
:root {
  --tabbar: 60px;
  --gutter: 30px;
  --min-touch: 44px;
  --sab: env(safe-area-inset-bottom, 0px);
  --sheet-peek: 148px;
  --sheet-half: 55%;
  --sheet-gap: 12px;
}


@media (max-width: 759.98px) {
  :root {
    --topbar: 56px;
    --gutter: 16px;
    /* one step up the type scale: a phone is held further from the eye than a monitor is read */
    --fs-micro: 10.5px;
    --fs-tiny: 12px;
    --fs-small: 13px;
    --fs-body: 14.5px;
    --fs-lead: 16px;
    --fs-h3: 17px;
    --fs-h2: 20px;
    --fs-h1: 24px;
    --r-lg: 18px;
  }

  html,
  body {
    overscroll-behavior: none;
  }
  body {
    -webkit-tap-highlight-color: transparent;
    -webkit-text-size-adjust: 100%;
  }
  ::-webkit-scrollbar {
    width: 0;
    height: 0;
  }
  .m-only {
    display: inline-flex;
  }

  /* ---------- frame: bar · page · tabs ---------- */
  #shell {
    height: 100vh;
    height: 100dvh;
    grid-template-rows: var(--topbar) minmax(0, 1fr) calc(var(--tabbar) + var(--sab));
    grid-template-columns: minmax(0, 1fr);
    grid-template-areas:
      "top"
      "view"
      "rail";
  }

  /* ---------- command bar ---------- */
  .topbar {
    padding: 0 10px 0 14px;
    gap: 10px;
  }
  .brand {
    gap: 10px;
    min-width: 0;
    flex: none;
  }
  .brand svg {
    width: 28px;
    height: 28px;
  }
  .brand .name {
    font-size: 12.5px;
    letter-spacing: 2px;
    line-height: 1.18;
  }
  .brand .name span {
    display: block;
  }
  .brand .tag {
    display: none;
  }
  .topmeta {
    flex: 1 1 auto;
    justify-content: flex-end;
    gap: 8px;
    overflow: visible;
  }
  .topmeta .readout {
    display: none;
  }
  .pill {
    min-height: var(--min-touch);
    padding: 0 12px;
    display: inline-flex;
    align-items: center;
    gap: 7px;
    min-width: var(--min-touch);
    max-width: 100%;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    flex: 0 1 auto;
    font-size: 10.5px;
    letter-spacing: 0.9px;
  }
  /* Share is an icon on a phone: the label has no room and the glyph is the convention */
  #mShare {
    flex: none;
    width: var(--min-touch);
    padding: 0;
    justify-content: center;
    font-size: 0;
  }
  #mShare::before {
    display: none;
  }
  #mShare::after {
    content: "⇗";
    font-size: 17px;
    line-height: 1;
  }

  /* ---------- bottom tab bar ---------- */
  .rail {
    flex-direction: row;
    align-items: stretch;
    gap: 0;
    padding: 0 0 var(--sab);
    border-right: 0;
    border-top: 1px solid var(--stroke);
    overflow: visible;
    background: var(--surface-solid);
  }
  .rail a {
    flex: 1 1 0;
    width: auto;
    min-width: 0;
    min-height: var(--tabbar);
    padding: 0;
    border-radius: 0;
    align-content: center;
    gap: 4px;
  }
  .rail a svg {
    width: 22px;
    height: 22px;
  }
  .rail a span {
    font-size: 10.5px;
    letter-spacing: 0.5px;
  }
  .rail a:hover,
  .rail a.on {
    background: transparent;
  }
  .rail a.on::before {
    left: 30%;
    right: 30%;
    top: 0;
    bottom: auto;
    width: auto;
    height: 3px;
    border-radius: 0 0 3px 3px;
  }

  /* ---------- controls sized for a finger ---------- */
  .btn,
  .btn.sm {
    min-height: var(--min-touch);
  }
  .btn.sm {
    padding: 8px 14px;
    font-size: 12.5px;
  }
  .btnrow {
    flex-wrap: wrap;
  }
  .seg button {
    min-height: var(--min-touch);
  }
  /* four operations do not fit one row on a small phone: two rows of two */
  #opSeg {
    display: grid;
    grid-auto-flow: row;
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
  /* any label that carries a checkbox or a radio is the touch target for it */
  label:has(> input[type="checkbox"]),
  label:has(> input[type="radio"]) {
    min-height: var(--min-touch);
    display: inline-flex;
    align-items: center;
    gap: 8px;
  }
  /* 16 px: anything smaller makes the phone zoom the whole page when the field is focused */
  input[type="text"],
  input[type="search"],
  input[type="email"],
  input[type="password"],
  input[type="number"],
  input[type="date"],
  input[type="time"],
  select,
  textarea {
    min-height: var(--min-touch);
    font-size: 16px;
  }
  input[type="range"] {
    min-height: var(--min-touch);
  }
  .check,
  label.check {
    min-height: var(--min-touch);
    align-items: center;
  }
  .switch {
    position: relative;
  }
  /* the visual switch stays small; its hit area is a full touch target */
  .switch::after {
    content: "";
    position: absolute;
    inset: -12px -4px;
  }

  /* ---------- document pages: one column of cards ---------- */
  .page {
    padding: 18px var(--gutter) 28px;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
  }
  .page > header {
    flex-direction: column;
    align-items: stretch;
    gap: 12px;
    margin-bottom: 16px;
  }
  .page > header p {
    max-width: none;
  }
  .page > header .btnrow > * {
    width: 100% !important;
    flex: 1 1 100%;
  }
  .grid-metrics {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 10px;
    margin-bottom: 16px;
  }
  .split,
  .grid2 {
    grid-template-columns: minmax(0, 1fr);
    gap: 12px;
  }
  .card {
    border-radius: 16px;
  }
  /* wide content (tables, long paths) scrolls inside its own card, never the page */
  .page .card,
  .tablewrap {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    max-width: 100%;
  }
  .page .card > *,
  .page dl,
  .page dd,
  .page dt {
    min-width: 0;
  }
  .page dd,
  .page .mono {
    overflow-wrap: anywhere;
  }
  .page dl.kv {
    grid-template-columns: minmax(0, 1fr);
    gap: 2px 0;
  }
  .page dl.kv dd {
    margin-bottom: 10px;
  }

  /* ---------- Ops: the map is the page, the controls are a sheet ---------- */
  .ops {
    display: block;
    position: relative;
  }
  .theatre {
    position: absolute;
    inset: 0;
  }
  .ops .panel {
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 820;
    height: var(--sheet-h, var(--sheet-peek));
    max-height: calc(100% - var(--sheet-gap));
    padding: 0 12px 16px;
    gap: 10px;
    border-right: 0;
    border-top: 1px solid var(--stroke-2);
    border-radius: 20px 20px 0 0;
    background: var(--surface-solid);
    backdrop-filter: none;
    box-shadow: 0 -14px 44px rgba(0, 0, 0, 0.5);
    transition: height 0.28s var(--ease);
    overscroll-behavior: contain;
  }
  .ops[data-sheet="peek"] .panel {
    overflow: hidden;
  }
  .ops[data-sheet="half"] .panel {
    --sheet-h: var(--sheet-half);
  }
  .ops[data-sheet="full"] .panel {
    --sheet-h: calc(100% - var(--sheet-gap));
  }
  .ops.dragging .panel {
    transition: none;
  }

  .sheet-grip {
    display: flex;
    align-items: center;
    gap: 10px;
    position: sticky;
    top: 0;
    z-index: 6;
    flex: none;
    min-height: var(--min-touch);
    margin: 0 -12px;
    padding: 12px 14px 0;
    background: var(--surface-solid);
    border-radius: 20px 20px 0 0;
    touch-action: none;
    cursor: grab;
    user-select: none;
  }
  .sheet-grip > i {
    position: absolute;
    top: 7px;
    left: 50%;
    width: 40px;
    height: 5px;
    margin-left: -20px;
    border-radius: 3px;
    background: var(--stroke-2);
  }
  .sheet-grip .sg-t {
    font-family: var(--disp);
    font-weight: 650;
    font-size: 15px;
    letter-spacing: 0.4px;
  }
  .sheet-grip .sg-s {
    font-size: 11.5px;
    color: var(--ink-3);
    letter-spacing: 0.3px;
  }
  .sheet-grip .btn {
    margin-left: auto;
  }

  /* one sheet at a time: a result replaces the controls until the operator switches back */
  .ops.has-result:not(.show-controls) .panel {
    display: none;
  }
  .ops.has-result.show-controls .dock {
    display: none;
  }

  /* result dock as a sheet */
  .dock {
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 850;
    max-height: 64%;
    overflow-y: auto;
    padding: 0 14px 14px;
    border-width: 1px 0 0;
    border-radius: 20px 20px 0 0;
    background: var(--surface-solid);
    backdrop-filter: none;
    box-shadow: 0 -14px 44px rgba(0, 0, 0, 0.5);
    overscroll-behavior: contain;
  }
  .dock .sheet-grip {
    margin: 0 -14px -8px;
    padding: 0;
    min-height: var(--min-touch);
    justify-content: center;
  }
  .dock .head {
    gap: 8px;
    margin-bottom: 10px;
  }
  .dock .head .t {
    flex: 1 1 100%;
    font-size: 15px;
  }
  .dock .head .meta {
    flex: 1 1 100%;
    order: 9;
  }
  .dock .head .sp {
    display: none;
  }
  .dock .head select {
    flex: 1 1 140px;
    min-width: 0;
    width: auto !important;
  }
  .dock .row {
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    gap: 10px;
  }
  .dock .sliders {
    grid-column: 1 / -1;
    order: 3;
  }
  .dock .picker select {
    width: 100%;
    min-width: 0;
  }
  .dock .legend {
    max-height: 104px;
    overflow-y: auto;
  }
  .dock.min > :not(.sheet-grip):not(.head) {
    display: none;
  }
  .dock.min .head {
    margin-bottom: 0;
  }
  .playbox {
    gap: 8px;
  }
  .playbox #playLbl {
    flex: 1 1 100%;
    white-space: normal;
  }

  /* map furniture */
  .searchbar {
    top: 10px;
    left: 10px;
    right: 10px;
    width: auto;
    min-height: 48px;
    border-radius: 14px;
  }
  .hud {
    top: 68px;
    right: 10px;
    gap: 12px;
    padding: 6px 10px;
    border-radius: 12px;
  }
  .hud .h:nth-child(3) {
    display: none;
  }
  .maplegend {
    top: 68px !important;
    left: 10px !important;
    right: auto !important;
    max-width: min(64vw, 260px);
    border-radius: 12px;
    cursor: pointer;
  }
  .maplegend .mlt {
    min-height: 32px;
    display: flex;
    align-items: center;
    gap: 6px;
    margin: 0;
  }
  .maplegend .mlt::after {
    content: "▾";
    margin-left: auto;
    opacity: 0.7;
  }
  .maplegend.open .mlt::after {
    content: "▴";
  }
  .maplegend:not(.open) .mli {
    display: none;
  }
  .maplegend.open {
    max-height: 46vh;
    overflow-y: auto;
  }
  .leaflet-control-zoom {
    display: none;
  }
  .leaflet-bottom {
    bottom: calc(var(--sheet-peek) + 2px);
  }
  .divider {
    width: 44px;
    margin-left: -22px;
  }
  .divider .ln {
    left: 21px;
  }
  .divider .knob {
    width: 40px;
    height: 40px;
    font-size: 15px;
  }

  /* ---------- tables become cards ----------
   * Same rows, same cells, same click targets — only stacked, so nothing has to be scrolled
   * sideways to be read. The history and log tables keep their columns and scroll in their card. */
  .tablewrap {
    overflow: visible;
    border: 0;
    background: none;
    border-radius: 0;
  }
  .tablewrap table,
  .tablewrap tbody,
  #list table.tbl,
  #list table.tbl tbody {
    display: block;
    width: 100%;
  }
  .tablewrap thead,
  #list table.tbl thead {
    display: none;
  }
  .tablewrap tbody tr {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 5px 10px;
    align-items: center;
    padding: 13px 14px;
    margin-bottom: 10px;
    border: 1px solid var(--stroke);
    border-radius: 16px;
    background: var(--surface-2);
  }
  .tablewrap tbody tr.sel {
    border-color: var(--accent);
    background: var(--accent-wash);
  }
  .tablewrap tbody td {
    display: block;
    padding: 0;
    border: 0;
    white-space: normal;
    background: none;
    grid-column: 1 / -1;
    min-width: 0;
  }
  .tablewrap tbody td:nth-child(1) {
    grid-column: 1;
    font-weight: 700;
    font-size: 14px;
  }
  .tablewrap tbody td:nth-child(3) {
    grid-column: 2;
    grid-row: 1;
    justify-self: end;
  }
  .tablewrap tbody td:nth-child(2) {
    color: var(--ink-2);
  }
  .tablewrap tbody td:nth-child(n + 5) {
    font-size: 12px;
    color: var(--ink-3);
  }
  .tablewrap tbody td:nth-child(5)::before {
    content: "Area  ";
  }
  .tablewrap tbody td:nth-child(6)::before {
    content: "Runtime  ";
  }
  .tablewrap tbody td:nth-child(7)::before {
    content: "Created  ";
  }
  .tablewrap tbody td[colspan] {
    grid-column: 1 / -1;
  }

  #list table.tbl tr {
    display: grid;
    grid-template-columns: auto minmax(0, 1fr) auto;
    gap: 3px 12px;
    align-items: center;
    padding: 12px 2px;
    border-bottom: 1px solid var(--stroke);
  }
  #list table.tbl tr:last-child {
    border-bottom: 0;
  }
  #list table.tbl td {
    display: block;
    padding: 0;
    border: 0;
    white-space: normal;
    background: none;
    grid-column: 2;
    min-width: 0;
    font-size: 12.5px;
  }
  #list table.tbl td:nth-child(1) {
    grid-column: 1;
    grid-row: 1 / span 4;
    align-self: start;
    padding-top: 12px;
  }
  #list table.tbl td:nth-child(2) {
    font-size: 14px;
  }
  #list table.tbl td:nth-child(6) {
    grid-column: 3;
    grid-row: 1 / span 4;
    align-self: start;
  }
  #list table.tbl td:nth-child(3)::before {
    content: "every  ";
    color: var(--ink-3);
  }
  #list table.tbl td:nth-child(4)::before {
    content: "next  ";
    color: var(--ink-3);
  }
  #list table.tbl td:nth-child(5)::before {
    content: "last  ";
    color: var(--ink-3);
  }
  #list table.tbl tr.sel {
    background: var(--accent-wash);
    border-radius: 12px;
  }
  #list table.tbl tr.sel td {
    background: none;
  }

  /* the map's own corner text stays under the sheets */
  .leaflet-control-container .leaflet-bottom,
  .leaflet-control-container .leaflet-top {
    z-index: 500;
  }

  /* ---------- transient surfaces ---------- */
  #toasts {
    left: 12px;
    right: 12px;
    top: calc(var(--topbar) + 8px);
  }
  .toast {
    min-width: 0;
    max-width: none;
  }
  .backdrop {
    place-items: end center;
    padding: 0;
  }
  .modal {
    width: 100%;
    border-radius: 20px 20px 0 0;
    padding: 20px 16px calc(20px + var(--sab));
    animation: sheetin 0.24s var(--ease);
  }
  .modal .actions {
    flex-wrap: wrap;
  }
  .modal .actions .btn {
    flex: 1 1 140px;
  }

  /* ---------- motion: cards arrive in order, surfaces answer a press ----------
   * Decoration only: with reduced motion (base.css) or the Classic interface nothing here runs,
   * and no control waits for an animation to finish. */
  .page > header,
  .page .card,
  .page .metric,
  .ops .panel > .card {
    animation: cardin 0.34s var(--ease) both;
  }
  .page .grid-metrics > :nth-child(2),
  .page .split > :nth-child(2),
  .ops .panel > .card:nth-of-type(2) {
    animation-delay: 0.05s;
  }
  .page .grid-metrics > :nth-child(3),
  .ops .panel > .card:nth-of-type(3) {
    animation-delay: 0.1s;
  }
  .page .grid-metrics > :nth-child(4),
  .ops .panel > .card:nth-of-type(4) {
    animation-delay: 0.15s;
  }
  .btn:active:not(:disabled),
  .seg button:active,
  .rail a:active,
  .jobrow:active {
    transform: scale(0.97);
    transition: transform 0.08s var(--ease);
  }
  .rail a svg {
    transition: transform 0.22s var(--ease);
  }
  .rail a.on svg {
    transform: translateY(-1px) scale(1.08);
  }
}

@keyframes sheetin {
  from {
    transform: translateY(24px);
    opacity: 0;
  }
}

@keyframes cardin {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
}
@media (prefers-reduced-motion: reduce) {
  .page > header,
  .page .card,
  .page .metric,
  .ops .panel > .card,
  .modal {
    animation: none !important;
  }
}
@media print {
  .rail,
  .topbar,
  .sheet-grip,
  #toasts {
    display: none !important;
  }
  #shell {
    display: block;
    height: auto;
  }
  .page {
    height: auto;
    overflow: visible;
  }
}
