/* ---------- Theme ---------- */
:root,
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0e0f13;
  --bg-field: #050506;
  --bg-hover: #181a20;
  --border: #2b2d35;
  --border-strong: #6b6e78;
  --text: #f4f4f5;
  --text-dim: #9a9da6;
  --text-faint: #6a6d76;
  --accent: #6f8cff;
  --invert-bg: #ffffff;
  --invert-text: #0e0f13;
}

:root[data-theme="light"] {
  color-scheme: light;
  --bg: #f7f7f8;
  --bg-field: #ffffff;
  --bg-hover: #ececef;
  --border: #d3d4da;
  --border-strong: #7c7f89;
  --text: #0e0f13;
  --text-dim: #555862;
  --text-faint: #80838c;
  --accent: #2f4fd8;
  --invert-bg: #0e0f13;
  --invert-text: #ffffff;
}

/* ---------- Sizes ---------- */
:root {
  --control-h: 2.4rem;   /* default height of buttons and inputs */
}

/* ---------- Base ---------- */
*, *::before, *::after { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  display: flex;
  flex-direction: column;
  margin: 0;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;   /* the page never scrolls; only the app list does */
  background: var(--bg);
  color: var(--text);
  font: 1rem/1.45 "Inter", "Segoe UI", system-ui, -apple-system, Roboto, "Helvetica Neue", Arial, sans-serif;
}

button, input, select { font: inherit; color: inherit; border-radius: 0; }
button, select { cursor: pointer; }

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* Label text: monospace, uppercase, wide tracking */
.label,
.cat,
.card-ver,
.card-vercol,
.list-head,
.card-tags,
.statusbar,
.btn,
.btn-open {
  font-family: ui-monospace, "Cascadia Mono", "SF Mono", Consolas, "Liberation Mono", Menlo, monospace;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.1rem;
}

/* ---------- GUI icons (SVG files in the icons folder, coloured via mask) ---------- */
.ico {
  display: inline-block;
  flex: none;
  width: 1.2rem;
  height: 1.2rem;
  background-color: currentColor;
  -webkit-mask: var(--icon) center / contain no-repeat;
  mask: var(--icon) center / contain no-repeat;
}
.ico-grid    { --icon: url("icons/grid.svg"); }
.ico-list    { --icon: url("icons/list.svg"); }
.ico-close   { --icon: url("icons/close.svg"); }
.ico-empty   { --icon: url("icons/empty.svg"); }
.ico-chevron { --icon: url("icons/chevron.svg"); }
.ico-external { --icon: url("icons/external.svg"); }
.ico-theme   { --icon: url("icons/sun.svg"); }
:root[data-theme="light"] .ico-theme { --icon: url("icons/moon.svg"); }

/* ---------- Top bar ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  height: 3.2rem;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
}

.brand {
  display: flex;
  height: 100%;
  align-items: center;
  gap: 0.6rem;
  padding: 0 1rem;
  border-right: 1px solid var(--border);
  color: inherit;
  text-decoration: none;
}
/* The brand is the first tab (the app list); the current tab carries a coloured line. */
.brand:hover { background: var(--bg-hover); }
.brand { box-shadow: inset 0 -2px 0 var(--accent); }
.viewing .brand { box-shadow: none; color: var(--text-dim); }
.viewing .brand:hover { color: var(--text); }
/* Same icon and name size as an app tab (.tab-main), so the brand reads as the first tab. */
.brand img {
  display: block;
  flex: none;
  width: 1.4rem;
  height: 1.4rem;
  image-rendering: pixelated;
}
.brand-name {
  font-size: 1rem;
  font-weight: 700;
  white-space: nowrap;
}

.topbar-btn {
  display: grid;
  place-items: center;
  flex: none;
  width: 3.2rem;
  height: 100%;
  padding: 0;
  border: 0;
  border-left: 1px solid var(--border);
  background: transparent;
  color: var(--text-dim);
  text-decoration: none;
}
.topbar-btn[hidden] { display: none; }
.topbar-btn:hover { background: var(--bg-hover); color: var(--text); }

/* ---------- App tabs ---------- */
.tabs {
  display: flex;
  flex: 1;
  height: 100%;
  min-width: 0;
  overflow-x: auto;
  scrollbar-width: none;
}
.tabs::-webkit-scrollbar { display: none; }

.tab {
  --app: var(--accent);
  display: flex;
  flex: none;
  height: 100%;
  max-width: 14rem;
  border-right: 1px solid var(--border);
  color: var(--text-dim);
}
.tab:hover { background: var(--bg-hover); color: var(--text); }
.tab.is-active {
  color: var(--text);
  box-shadow: inset 0 -2px 0 var(--app);
}

.tab-main {
  display: flex;
  align-items: center;
  height: 100%;
  gap: 0.6rem;
  min-width: 0;
  padding: 0 0.4rem 0 1rem;
  border: 0;
  background: transparent;
  font-weight: 700;
}
.tab-main img {
  display: block;
  flex: none;
  width: 1.4rem;
  height: 1.4rem;
  object-fit: contain;
}
.tab-name {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.tab-close {
  display: grid;
  place-items: center;
  flex: none;
  align-self: center;
  width: 1.6rem;
  height: 1.6rem;
  margin-right: 0.6rem;
  padding: 0;
  border: 0;
  background: transparent;
  color: var(--text-faint);
}
.tab-close:hover { background: var(--border); color: var(--text); }
.tab-close .ico { width: 0.8rem; height: 0.8rem; }

/* ---------- App viewer (iframes) ---------- */
/* The viewer always covers the area under the top bar, and every iframe always has
   its full size; inactive ones are only made invisible. Apps keep running and never
   see a zero-sized window. */
.viewer {
  position: fixed;
  top: 3.2rem;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 5;
  background: var(--bg);
  visibility: hidden;
}
.viewing .viewer { visibility: visible; }
.viewer iframe {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  border: 0;
  background: var(--bg);
  visibility: hidden;
}
.viewer iframe.is-active { visibility: inherit; }

/* While an app tab is active the list underneath is covered and not reachable. */
.viewing .main,
.viewing .statusbar { visibility: hidden; }

/* ---------- Toolbar ---------- */
.main {
  display: flex;
  flex: 1;
  flex-direction: column;
  gap: 1.2rem;
  min-height: 0;
  padding: 1.4rem 1.6rem 0;
}

/* The only scrolling area: the app list. */
.list {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
}

/* ---------- Scrollbar: square, thin, in theme colours ---------- */
::-webkit-scrollbar {
  width: 0.8rem;
  height: 0.8rem;
}
::-webkit-scrollbar-track {
  background: var(--bg);
}
::-webkit-scrollbar-thumb {
  /* the transparent border insets the thumb inside the track */
  border: 0.2rem solid transparent;
  background: var(--border-strong);
  background-clip: padding-box;
}
::-webkit-scrollbar-thumb:hover { background-color: var(--text-dim); }
::-webkit-scrollbar-thumb:active { background-color: var(--accent); }
::-webkit-scrollbar-button { display: none; }
::-webkit-scrollbar-corner { background: var(--bg); }

/* Firefox has no ::-webkit-scrollbar; use the standard properties there. */
@supports not selector(::-webkit-scrollbar) {
  * {
    scrollbar-width: thin;
    scrollbar-color: var(--border-strong) var(--bg);
  }
}

.toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.8rem;
}

.toolbar-right {
  display: flex;
  align-items: center;
  flex: none;
  gap: 0.8rem;
}

.field {
  display: flex;
  align-items: center;
  gap: 0.6rem;
}
.label {
  color: var(--text-faint);
  font-size: 0.8rem;
}

.select {
  position: relative;
  display: block;
}
.select select {
  display: block;
  width: 11.8rem;
  height: var(--control-h);
  padding: 0 2.4rem 0 0.8rem;
  border: 1px solid var(--border);
  background: var(--bg);
  -webkit-appearance: none;
  appearance: none;
  text-overflow: ellipsis;
}
.select select:hover { border-color: var(--border-strong); }
.select option { background: var(--bg); color: var(--text); }
.select .ico {
  position: absolute;
  top: 50%;
  right: 0.8rem;
  width: 1rem;
  height: 1rem;
  transform: translateY(-50%);
  pointer-events: none;
}

.search {
  display: flex;
  align-items: center;
  flex: 0 1 18.8rem;
  min-width: 0;
  height: var(--control-h);
  border: 1px solid var(--border);
  background: var(--bg-field);
}
.search:hover { border-color: var(--border-strong); }
.search:focus-within { border-color: var(--accent); }
.search input {
  flex: 1;
  min-width: 0;
  height: 100%;
  padding: 0 0.8rem;
  border: 0;
  outline: 0;
  background: transparent;
}
.search input::placeholder { color: var(--text-dim); }
.search input::-webkit-search-cancel-button { display: none; }
.search-clear {
  display: grid;
  place-items: center;
  flex: none;
  width: 2.2rem;
  height: 100%;
  padding: 0;
  border: 0;
  background: transparent;
  color: var(--text-dim);
}
.search-clear[hidden] { display: none; }
.search-clear:hover { color: var(--text); }
.search-clear .ico { width: 1rem; height: 1rem; }

.view-toggle {
  display: flex;
  flex: none;
  height: var(--control-h);
  border: 1px solid var(--border);
}
.view-toggle button {
  display: grid;
  place-items: center;
  width: var(--control-h);
  height: 100%;
  padding: 0;
  border: 0;
  background: transparent;
  color: var(--text-dim);
}
.view-toggle button:hover { color: var(--text); }
.view-toggle button[aria-pressed="true"] {
  background: var(--invert-bg);
  color: var(--invert-text);
}

/* ---------- Category buttons ---------- */
.cats {
  display: flex;
  flex: none;
  flex-wrap: wrap;
  gap: 0.6rem;
}
/* Exception to --control-h: the category buttons are small filter chips. */
.cat {
  height: 2rem;
  padding: 0 0.6rem;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text-dim);
  font-size: 0.6rem;
  letter-spacing: 0.1rem;
  white-space: nowrap;
}
.cat:hover {
  border-color: var(--accent);
  background: color-mix(in srgb, var(--accent) 14%, transparent);
  color: var(--accent);
}
.cat[aria-pressed="true"] {
  border-color: var(--invert-bg);
  background: var(--invert-bg);
  color: var(--invert-text);
}

/* ---------- App list: ruled cells with no gaps ---------- */
.apps {
  display: grid;
  margin: 0;
  padding: 0;
  list-style: none;
  border-top: 1px solid var(--border);
  border-left: 1px solid var(--border);
}
.apps[hidden] { display: none; }
.apps > li {
  min-width: 0;
  border-right: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
}

/* --app is the colour of the app (apps.json "color"); it tints the card on hover. */
.card {
  --app: var(--accent);
  display: flex;
  flex-direction: column;
  height: 100%;
}
.card:hover {
  background: color-mix(in srgb, var(--app) 9%, transparent);
  box-shadow: inset 0 2px 0 var(--app);
}
.view-list .card:hover { box-shadow: inset 2px 0 0 var(--app); }

.card-head {
  display: flex;
  align-items: center;
  gap: 1.2rem;
  min-width: 0;
}
.card-icon { display: block; flex: none; width: 4.2rem; height: 4.2rem; object-fit: contain; }
.card-title { min-width: 0; }
.card-name {
  margin: 0;
  font-size: 1.8rem;
  font-weight: 700;
  line-height: 1.15;
  overflow-wrap: anywhere;
}
.card-name-tail { color: var(--app); }
.card-ver,
.card-vercol {
  margin: 0;
  font-size: 0.8rem;
  letter-spacing: 0.1rem;
}
.card-ver { margin-top: 0.4rem; color: var(--app); }
.card-vercol { display: none; color: var(--text); font-size: 0.8rem; }
.card-desc {
  margin: 0;
  font-size: 0.8rem;
  overflow-wrap: anywhere;
}

.btn-open {
  display: flex;
  align-items: center;
  justify-content: center;
  height: var(--control-h);
  padding: 0 1.8rem;
  border: 1px solid var(--border-strong);
  color: var(--text);
  font-size: 0.8rem;
  text-decoration: none;
  white-space: nowrap;
}
.btn-open:hover {
  border-color: var(--invert-bg);
  background: var(--invert-bg);
  color: var(--invert-text);
}

/* Grid (default) */
/* Four across on wide screens, then 3, 2 and (on phones) 1. */
.view-grid { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.view-grid .card-body {
  display: flex;
  flex: 1;
  flex-direction: column;
  gap: 1.4rem;
  padding: 1.6rem;
}
.view-grid .card-desc { flex: 1; min-height: 4.4rem; }

/* Rows: a table with a column header */
.list-head,
.view-list .card-body {
  display: grid;
  grid-template-columns: minmax(13.8rem, 1.1fr) minmax(0, 2.4fr) minmax(11.8rem, 1fr) 5.6rem 11.2rem;
  align-items: center;
  gap: 1.8rem;
  padding: 0 1.6rem;
}
.list-head {
  position: sticky;
  top: 0;
  z-index: 1;
  height: 3.8rem;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text-faint);
  font-size: 0.8rem;
}
.list-head[hidden] { display: none; }

.card-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  margin: 0;
  padding: 0;
  list-style: none;
}
/* Exception to --control-h: small chips. Fixed height, horizontal padding only, text centred. */
.card-tags li {
  display: flex;
  align-items: center;
  height: 1.2rem;
  padding: 0 0.4rem;
  border: 1px solid var(--border-strong);
  color: var(--text-dim);
  font-size: 0.6rem;
  letter-spacing: 0.1rem;
  white-space: nowrap;
}

.view-list { grid-template-columns: 1fr; border-top: 0; }
.view-list .card-body { min-height: 5.8rem; padding-top: 1rem; padding-bottom: 1rem; }
.view-list .card-ver { display: none; }
.view-list .card-vercol { display: block; }
.view-list .card-icon { width: 3.2rem; height: 3.2rem; }
.view-list .card-head { gap: 1rem; }
.view-list .card-name { font-size: 1.4rem; }
.view-list .card-desc { color: var(--text-dim); }

/* ---------- Empty ---------- */
.empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.8rem;
  padding: 4rem 1.2rem;
  border: 1px solid var(--border);
  color: var(--text-dim);
  text-align: center;
}
.empty[hidden] { display: none; }
.empty .ico { width: 2.2rem; height: 2.2rem; }
.empty p { margin: 0; overflow-wrap: anywhere; }

.btn {
  height: var(--control-h);
  padding: 0 1.2rem;
  border: 1px solid var(--border-strong);
  background: transparent;
  color: var(--text);
  font-size: 0.8rem;
}
.btn[hidden] { display: none; }
.btn:hover { background: var(--invert-bg); color: var(--invert-text); }

/* ---------- Status bar ---------- */
.statusbar {
  position: sticky;
  bottom: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  height: 2.4rem;
  padding: 0 1.6rem;
  background: var(--bg);
  border-top: 1px solid var(--border);
  color: var(--text-faint);
  font-size: 0.8rem;
}

/* ---------- Small screens ---------- */
/* Narrower columns: slightly smaller icon and name so names stay on one line. */
@media (max-width: 96rem) {
  .view-grid .card-head { gap: 1rem; }
  .view-grid .card-icon { width: 3.6rem; height: 3.6rem; }
  .view-grid .card-name { font-size: 1.6rem; }
}

@media (max-width: 72rem) {
  .view-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}

@media (max-width: 56rem) {
  .view-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (max-width: 62.6rem) {
  .list-head { display: none; }
  .apps.view-list { border-top: 1px solid var(--border); }
  .view-list .card-body {
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 0.8rem 1.2rem;
  }
  .view-list .card-tags,
  .view-list .card-desc { grid-column: 1 / -1; }
  .view-list .card-ver { display: block; }
  .view-list .card-vercol { display: none; }
  .view-list .btn-open { grid-column: 2; grid-row: 1; }
}

@media (max-width: 40rem) {
  .brand { padding: 0 1rem; }
  .has-tabs .brand-name { display: none; }
  .tab { max-width: 10rem; }
  .main { padding: 1rem 1rem 0; }
  .statusbar { padding: 0 1rem; }

  /* Stacked toolbar: the controls keep their own height. */
  .toolbar { flex-direction: column; align-items: stretch; }
  .search { flex: none; }
  .toolbar-right { flex: none; }
  .field { flex: 1; min-width: 0; }
  .cats { flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; }
  .cats::-webkit-scrollbar { display: none; }
  .cat { flex: none; }
  .select { flex: 1; min-width: 0; }
  .select select { width: 100%; }

  .view-grid { grid-template-columns: 1fr; }
  .view-grid .card-body { padding: 1.2rem 1rem; gap: 1.2rem; }
  .view-grid .card-desc { min-height: 0; }
  .card-icon { width: 3.6rem; height: 3.6rem; }
  .card-name { font-size: 1.6rem; }

  .view-list .card-body { grid-template-columns: 1fr; padding: 1rem; }
  .view-list .btn-open { grid-column: auto; grid-row: auto; }
}
