:root {
  /* Light palette (the default). */
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-hover: #f9fafb;
  --border: #e3e6ea;
  --text: #1c2024;
  --text-muted: #6b7280;
  --accent: #eab308;
  /* Text/icons placed on top of the (light) accent — dark for contrast. */
  --on-accent: #1c2024;
  --shadow: 0 1px 2px rgba(0, 0, 0, .05), 0 4px 12px rgba(0, 0, 0, .06);
  --radius: 14px;
  /* Make native controls (the <select> dropdown, scrollbars) match the theme. */
  color-scheme: light;

  /* Dark palette values, defined once here and applied by the two selectors
     below (explicit choice + system preference). */
  --dark-bg: #0c0e12;
  --dark-surface: #16191f;
  --dark-surface-hover: #1e222a;
  --dark-border: #2a2f38;
  --dark-text: #e6e8eb;
  --dark-text-muted: #9aa1ac;
  --dark-accent: #facc15;
  --dark-shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 8px 24px rgba(0, 0, 0, .45);
}

/* Remap the semantic tokens onto the dark palette when dark is chosen
   explicitly (the system-preference case is handled by the media query below). */
:root[data-theme="dark"] {
  --bg: var(--dark-bg);
  --surface: var(--dark-surface);
  --surface-hover: var(--dark-surface-hover);
  --border: var(--dark-border);
  --text: var(--dark-text);
  --text-muted: var(--dark-text-muted);
  --accent: var(--dark-accent);
  --shadow: var(--dark-shadow);
  color-scheme: dark;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]):not([data-theme="dark"]) {
    --bg: var(--dark-bg);
    --surface: var(--dark-surface);
    --surface-hover: var(--dark-surface-hover);
    --border: var(--dark-border);
    --text: var(--dark-text);
    --text-muted: var(--dark-text-muted);
    --accent: var(--dark-accent);
    --shadow: var(--dark-shadow);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

.site-header {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
  max-width: 1100px;
  margin: 0 auto;
  padding: 2.5rem 1.5rem 1rem;
}

.brand h1 { margin: 0; font-size: 1.9rem; letter-spacing: -.02em; }
.subtitle { margin: .15rem 0 0; color: var(--text-muted); }

.header-actions { display: flex; align-items: center; gap: 1rem; flex-wrap: wrap; }

.theme-toggle {
  display: inline-flex;
  padding: 3px;
  gap: 2px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  transition: border-color .15s ease;
}
.theme-toggle:hover { border-color: var(--accent); }
.theme-toggle button {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 28px;
  padding: 0;
  border: 0;
  border-radius: 7px;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  transition: background .15s ease, color .15s ease;
}
.theme-toggle button:hover { color: var(--text); background: var(--surface-hover); }
.theme-toggle button[aria-pressed="true"] { background: var(--accent); color: var(--on-accent); }
.theme-toggle svg { width: 17px; height: 17px; display: block; }

.lang-select { position: relative; display: inline-flex; }
/* Chevron drawn in CSS so it follows the theme colour. */
.lang-select::after {
  content: "";
  position: absolute;
  right: .7rem;
  top: 50%;
  width: .42rem;
  height: .42rem;
  border-right: 2px solid var(--text-muted);
  border-bottom: 2px solid var(--text-muted);
  transform: translateY(-65%) rotate(45deg);
  pointer-events: none;
}
.lang-select select {
  height: 36px;
  padding: 0 2rem 0 .7rem;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: .82rem;
  font-weight: 600;
  cursor: pointer;
  appearance: none;
  -webkit-appearance: none;
  transition: background .15s ease, color .15s ease, border-color .15s ease;
}
.lang-select select:hover { background: var(--surface-hover); border-color: var(--accent); }
.lang-select select:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.lang-select option { background: var(--surface); color: var(--text); }

/* Merged user name / logout control: shows the name, and on hover/focus swaps
   to "Log out" with the accent border. The two labels are stacked in one grid
   cell so the box width stays fixed (no layout shift) as they cross-fade. */
.user-logout {
  display: inline-grid;
  place-items: center;
  font-size: .9rem;
  font-weight: 600;
  text-decoration: none;
  padding: .4rem .8rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  cursor: pointer;
  transition: background .15s ease, border-color .15s ease;
}
.user-logout > span {
  grid-area: 1 / 1;
  white-space: nowrap;
  transition: opacity .15s ease;
}
.user-logout-name { color: var(--text-muted); }
.user-logout-action { color: var(--text); opacity: 0; }
.user-logout:hover,
.user-logout:focus-visible {
  background: var(--surface-hover);
  border-color: var(--accent);
}
.user-logout:hover .user-logout-name,
.user-logout:focus-visible .user-logout-name { opacity: 0; }
.user-logout:hover .user-logout-action,
.user-logout:focus-visible .user-logout-action { opacity: 1; }

.admin-link {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text-muted);
  text-decoration: none;
  transition: background .15s ease, color .15s ease, border-color .15s ease;
}
.admin-link:hover,
.admin-link:focus-visible { color: var(--text); background: var(--surface-hover); border-color: var(--accent); }
.admin-link svg { width: 18px; height: 18px; display: block; }

.user-login {
  font-size: .9rem;
  font-weight: 600;
  text-decoration: none;
  color: var(--text);
  padding: .4rem .8rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  transition: background .15s ease, border-color .15s ease;
}
.user-login:hover,
.user-login:focus-visible {
  background: var(--surface-hover);
  border-color: var(--accent);
}

.content { max-width: 1100px; margin: 0 auto; padding: 1rem 1.5rem 3rem; }

.category { margin-top: 2rem; }
.category-title {
  font-size: .8rem;
  text-transform: uppercase;
  letter-spacing: .08em;
  color: var(--text-muted);
  margin: 0 0 .9rem .2rem;
  font-weight: 600;
}

/* Pinterest-style masonry: CSS multi-column packs variable-height tiles and
   reflows the column count to the available width. */
.masonry {
  column-width: 230px;
  column-gap: 1rem;
  padding-bottom: 4px; /* room for tile box-shadow to not clip at column bottom */
}

.tile {
  display: block;
  -webkit-column-break-inside: avoid;
  break-inside: avoid;
  margin: 0 0 1rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  text-decoration: none;
  color: inherit;
  box-shadow: var(--shadow);
  isolation: isolate;           /* Safari: prevents gradient bleed-through in composited layers */
  transition: transform .12s ease, border-color .15s ease;
}
.tile:hover { transform: translateY(-2px); border-color: var(--accent); }

/* Offline (health check failed): greyed out and non-navigable. */
.tile--offline { opacity: .5; filter: grayscale(1); cursor: not-allowed; }
.tile--offline:hover { transform: none; border-color: var(--border); }

.tile-media { display: block; background: var(--surface-hover); }
.tile-media img { display: block; width: 100%; height: auto; }

/* Theme-aware images: light shown by default, dark swapped in for dark theme.
   Selectors must include .tile-media so specificity (0,2,0) beats
   .tile-media img (0,1,1) which would otherwise keep dark images visible. */
.tile-media .tile-img-dark { display: none; }
[data-theme="dark"] .tile-media .tile-img-light { display: none; }
[data-theme="dark"] .tile-media .tile-img-dark  { display: block; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]):not([data-theme="dark"]) .tile-media .tile-img-light { display: none; }
  :root:not([data-theme="light"]):not([data-theme="dark"]) .tile-media .tile-img-dark  { display: block; }
}

/* Placeholder shown until a screenshot is set: a per-service colour gradient
   with the logo/initial centred. --hue and --h are set inline per tile. */
.tile-media--placeholder {
  height: var(--h, 190px);
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(
    135deg,
    hsl(var(--hue, 220) 60% 52%),
    hsl(calc(var(--hue, 220) + 45) 65% 40%)
  );
}
.tile-glyph {
  width: 56px;
  height: 56px;
  object-fit: contain;
  filter: drop-shadow(0 2px 6px rgba(0, 0, 0, .35));
}
.tile-glyph-initial { font-size: 2.4rem; font-weight: 700; color: #fff; }

.tile-body { padding: .8rem .9rem 1rem; }
.tile-info { display: flex; align-items: flex-start; gap: .55rem; }
.tile-icon { flex-shrink: 0; width: 22px; height: 22px; object-fit: contain; margin-top: .12rem; }
.tile-text { min-width: 0; }
.tile-name { margin: 0; font-size: 1rem; font-weight: 600; }
.tile-desc { margin: .25rem 0 0; font-size: .85rem; color: var(--text-muted); }

.empty {
  text-align: center;
  margin-top: 4rem;
  color: var(--text);
}
.muted { color: var(--text-muted); }

.site-footer {
  max-width: 1100px;
  margin: 0 auto;
  padding: 1.5rem;
  color: var(--text-muted);
  font-size: .8rem;
  text-align: center;
}

/* The rules below give these elements an explicit `display`, which would
   otherwise override the `hidden` attribute the JS toggles them with. Restore
   `hidden` meaning `display: none` for them. */
.weather-chip[hidden],
.weather-mode[hidden],
.system-chips[hidden],
.tile-widget[hidden],
.ql-overlay[hidden] { display: none; }

/* --- Header widgets (search trigger + weather + system) --- */
.header-widgets { display: flex; align-items: center; gap: .6rem; flex-wrap: wrap; }

/* Search icon that opens Quick Launch — the discoverable entry point now that
   the standalone search bar is gone. Styled like the theme-toggle buttons. */
.ql-trigger {
  display: inline-flex;
  align-items: center;
  gap: .45rem;
  height: 36px;
  padding: 0 .8rem;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text-muted);
  font: inherit;
  cursor: pointer;
  transition: background .15s ease, color .15s ease, border-color .15s ease;
}
.ql-trigger:hover,
.ql-trigger:focus-visible { color: var(--text); background: var(--surface-hover); border-color: var(--accent); }
.ql-trigger svg { width: 16px; height: 16px; display: block; flex-shrink: 0; }
/* Placeholder-style label; looks like a search field but is a button, so a
   click (or typing, which the global handler catches) opens Quick Launch. */
.ql-trigger-label { font-size: .85rem; }

.weather-chip {
  display: inline-flex;
  align-items: center;
  gap: .35rem;
  height: 36px;
  padding: 0 .7rem;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  text-decoration: none;
  font-size: .85rem;
  font-weight: 600;
  cursor: default;
}
.weather-chip[href] { cursor: pointer; }
.weather-chip[href]:hover,
.weather-chip[href]:focus-visible { border-color: var(--accent); }
.weather-icon { font-size: 1.05rem; line-height: 1; }
.weather-temp { font-variant-numeric: tabular-nums; }

/* Location-source toggle (current position vs configured "home"). */
.weather-mode {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  font-size: 1rem;
  line-height: 1;
  cursor: pointer;
  transition: background .15s ease, border-color .15s ease;
}
.weather-mode:hover,
.weather-mode:focus-visible { background: var(--surface-hover); border-color: var(--accent); }

.system-chips {
  display: inline-flex;
  align-items: center;
  gap: .4rem;
  height: 36px;
  padding: 0 .6rem;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  text-decoration: none;
  cursor: default;
}
.system-chips[href] { cursor: pointer; }
.system-chips[href]:hover,
.system-chips[href]:focus-visible { border-color: var(--accent); }
.sys-chip { display: inline-flex; align-items: baseline; gap: .3rem; font-size: .78rem; }
.sys-chip + .sys-chip { border-left: 1px solid var(--border); padding-left: .4rem; }
.sys-label { color: var(--text-muted); font-weight: 600; }
.sys-value { font-weight: 600; font-variant-numeric: tabular-nums; }
@media (max-width: 640px) { .system-chips { display: none; } }

/* --- Bookmarks --- */
.bookmarks { margin-top: 2.5rem; }
.bookmark-columns {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 1rem 1.5rem;
}
.bookmark-group-title {
  margin: 0 0 .5rem;
  font-size: .82rem;
  font-weight: 600;
  color: var(--text);
}
.bookmark-list { list-style: none; margin: 0; padding: 0; }
.bookmark-link {
  display: flex;
  align-items: center;
  gap: .55rem;
  padding: .35rem .5rem;
  border-radius: 8px;
  text-decoration: none;
  color: var(--text);
  font-size: .88rem;
  transition: background .12s ease;
}
.bookmark-link:hover { background: var(--surface-hover); }
.bookmark-badge {
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 24px;
  height: 24px;
  border-radius: 6px;
  background: var(--surface);
  border: 1px solid var(--border);
  font-size: .7rem;
  font-weight: 700;
  color: var(--text-muted);
  overflow: hidden;
}
.bookmark-badge img { width: 16px; height: 16px; object-fit: contain; }
.bookmark-name { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* --- Per-service widget stats on a tile --- */
.tile-widget {
  display: flex;
  gap: 1rem;
  flex-wrap: wrap;
  margin-top: .7rem;
  padding-top: .7rem;
  border-top: 1px solid var(--border);
}
.tw-stat { display: flex; flex-direction: column; }
.tw-value { font-size: .95rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.tw-label { font-size: .68rem; text-transform: uppercase; letter-spacing: .05em; color: var(--text-muted); }
.tile-widget--error { color: var(--text-muted); }
.tw-dot { font-size: .95rem; }

/* --- Quick Launch overlay --- */
.ql-overlay {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  padding: 12vh 1rem 1rem;
  background: color-mix(in srgb, #000 45%, transparent);
  backdrop-filter: blur(3px);
}
.ql-panel {
  width: min(640px, 94vw);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}
.ql-input {
  width: 100%;
  height: 54px;
  padding: 0 1.1rem;
  border: 0;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: 1.05rem;
  outline: none;
}
.ql-input::placeholder { color: var(--text-muted); }
.ql-results { list-style: none; margin: 0; padding: .35rem; max-height: 46vh; overflow-y: auto; }
.ql-result {
  display: flex;
  align-items: center;
  gap: .7rem;
  padding: .55rem .7rem;
  border-radius: 9px;
  cursor: pointer;
}
.ql-result[aria-selected="true"] { background: var(--surface-hover); }
.ql-icon {
  flex-shrink: 0;
  width: 24px;
  height: 24px;
  object-fit: contain;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.ql-icon--initial, .ql-icon--action {
  border-radius: 6px;
  background: var(--bg);
  border: 1px solid var(--border);
  font-weight: 700;
  color: var(--text-muted);
  font-size: .85rem;
}
.ql-text { min-width: 0; display: flex; flex-direction: column; }
.ql-name { font-size: .92rem; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ql-sub { font-size: .78rem; color: var(--text-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ql-empty { padding: 1rem; text-align: center; color: var(--text-muted); font-size: .9rem; }
.ql-hint {
  padding: .55rem .9rem;
  border-top: 1px solid var(--border);
  font-size: .74rem;
  color: var(--text-muted);
}

/* --- Admin: public/VPN-only access toggle --- */
.access-list {
  list-style: none;
  margin: 1rem 0 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: .6rem;
}
.access-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
  padding: .8rem 1rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
.access-info { display: flex; flex-direction: column; min-width: 0; }
.access-name { font-weight: 600; }
.access-host { font-size: .82rem; color: var(--text-muted); }
.access-toggle {
  display: inline-flex;
  flex-shrink: 0;
  padding: 3px;
  gap: 2px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  transition: border-color .15s ease;
}
.access-toggle:hover { border-color: var(--accent); }
.access-toggle button {
  width: auto;
  height: 32px;
  padding: 0 .8rem;
  border: 0;
  border-radius: 7px;
  background: transparent;
  color: var(--text-muted);
  font: inherit;
  font-size: .82rem;
  font-weight: 600;
  white-space: nowrap;
  cursor: pointer;
  transition: background .15s ease, color .15s ease;
}
.access-toggle button:hover { color: var(--text); background: var(--surface-hover); }
.access-toggle button[aria-pressed="true"] { background: var(--accent); color: var(--on-accent); }
