/* ===== Web font ===== */
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400..700&display=swap');

/* ===== Reset & Variables ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg:             #f6f7f9;
  --bg-elevated:    #ffffff;
  --border:         #e1e4e8;
  --border-strong:  #c7cdd4;
  --text:           #1f2328;
  --text-secondary: #4b5563;
  --text-muted:     #6b7280;
  --accent:         #2563eb;          /* blue-600 — corporate, not vivid */
  --accent-soft:    rgba(37, 99, 235, .08);
  --header-bg:      #1c2128;
  --header-text:    #f3f4f6;
  --header-border:  #2d333b;
  --shadow-sm:      0 1px 2px rgb(15 23 42 / .04);
  --shadow-md:      0 4px 12px rgb(15 23 42 / .08), 0 1px 3px rgb(15 23 42 / .05);
  --radius-card:    10px;
  --radius-icon:    10px;
  --radius-sm:      6px;
  --icon-bg-image:  #f3f4f6;          /* tile colour for image-based icons */
}

/* Dark-mode variables.  Applied either via OS preference (when no explicit
 * data-theme is set on <html>) or via the manual toggle (data-theme="dark").
 * Light mode is forced by data-theme="light", which beats the @media via
 * higher selector specificity. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg:             #0d1117;
    --bg-elevated:    #2c333d;        /* lighter card surface so icons (esp. dark-tiled built-ins like vault) stand out */
    --border:         #454c58;
    --border-strong:  #5a6270;
    --text:           #e6edf3;
    --text-secondary: #c9d1d9;
    --text-muted:     #8b949e;
    --accent:         #4493f8;
    --accent-soft:    rgba(68, 147, 248, .12);
    --header-bg:      #0a0d12;
    --header-text:    #f0f6fc;
    --header-border:  #1c2128;
    --shadow-sm:      0 1px 2px rgb(0 0 0 / .4);
    --shadow-md:      0 4px 12px rgb(0 0 0 / .6), 0 1px 3px rgb(0 0 0 / .35);
    --icon-bg-image:  #e5e7eb;        /* brighter tile reads on dark surfaces */
  }
}

:root[data-theme="dark"] {
  --bg:             #0d1117;
  --bg-elevated:    #2c333d;
  --border:         #454c58;
  --border-strong:  #5a6270;
  --text:           #e6edf3;
  --text-secondary: #c9d1d9;
  --text-muted:     #8b949e;
  --accent:         #4493f8;
  --accent-soft:    rgba(68, 147, 248, .12);
  --header-bg:      #0a0d12;
  --header-text:    #f0f6fc;
  --header-border:  #1c2128;
  --shadow-sm:      0 1px 2px rgb(0 0 0 / .4);
  --shadow-md:      0 4px 12px rgb(0 0 0 / .6), 0 1px 3px rgb(0 0 0 / .35);
  --icon-bg-image:  #e5e7eb;
}

html { overflow-x: hidden; }   /* defensive — never horizontal-scroll */

body {
  font-family: -apple-system, BlinkMacSystemFont, "Inter", "Segoe UI", Roboto,
               "Helvetica Neue", Arial, system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  font-size: 14px;
  line-height: 1.5;
  overflow-x: hidden;
}

/* ===== Header ===== */
.header {
  background: var(--header-bg);
  border-bottom: 1px solid var(--header-border);
  padding: 0 32px;
  height: 56px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.header__brand {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.header__brand-link {
  display: flex;
  align-items: center;
  text-decoration: none;
  transition: opacity .15s ease;
}
.header__brand-link:hover { opacity: .82; }

.header__logo {
  width: 24px;
  height: 24px;
  display: block;
}

.header__title {
  font-size: 1.05rem;
  font-weight: 600;
  color: var(--header-text);
  letter-spacing: -.018em;
  font-feature-settings: "cv01", "cv11", "ss01";  /* Inter alternates if loaded */
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.header__user {
  display: flex;
  align-items: center;
  gap: 14px;
  font-size: .82rem;
  color: rgba(243, 244, 246, .65);
}

.header__name { font-weight: 500; color: rgba(243, 244, 246, .85); }

.header__logout-form { display: inline; margin: 0; padding: 0; }

.header__logout {
  padding: 5px 12px;
  background: transparent;
  border: 1px solid rgba(255, 255, 255, .14);
  border-radius: var(--radius-sm);
  color: rgba(243, 244, 246, .85);
  text-decoration: none;
  font-size: .8rem;
  font-weight: 500;
  font-family: inherit;
  cursor: pointer;
  transition: background .12s ease, border-color .12s ease, color .12s ease;
}
.header__logout:hover {
  background: rgba(255, 255, 255, .06);
  border-color: rgba(255, 255, 255, .26);
  color: #fff;
}

.header__theme {
  width: 30px;
  height: 30px;
  padding: 0;
  background: transparent;
  border: 1px solid rgba(255, 255, 255, .14);
  border-radius: var(--radius-sm);
  color: rgba(243, 244, 246, .85);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background .12s ease, border-color .12s ease, color .12s ease;
}
.header__theme:hover {
  background: rgba(255, 255, 255, .06);
  border-color: rgba(255, 255, 255, .26);
  color: #fff;
}
.header__theme svg { width: 14px; height: 14px; }

/* Show the right icon for the active theme.  Sun is shown when dark is
 * active (click → switch to light); moon is shown when light is active. */
.header__theme-icon { display: none; }
:root[data-theme="dark"]    .header__theme-icon--sun  { display: block; }
:root[data-theme="light"]   .header__theme-icon--moon { display: block; }
:root:not([data-theme])     .header__theme-icon--moon { display: block; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme])   .header__theme-icon--moon { display: none; }
  :root:not([data-theme])   .header__theme-icon--sun  { display: block; }
}
/* Layout-toggle icon shows the *target* layout: stacked-rows icon when
   currently compact (clicking returns to stacked), grid icon when
   currently stacked. */
:root[data-layout="compact"] .header__theme-icon--stacked { display: block; }
:root:not([data-layout="compact"]) .header__theme-icon--compact { display: block; }

/* Show-hidden toggle: open eye when hidden apps are visible, eye-with-
   slash when they're hidden. */
:root[data-show-hidden="1"] .header__theme-icon--eye { display: block; }
:root:not([data-show-hidden="1"]) .header__theme-icon--eye-off { display: block; }

/* Drag-mode toggle: opposing-arrows icon when swap-on-hover is active
   (the default), down-arrow-into-bars icon when classic move-aside is
   active.  Default = swap, so an unset attribute shows the swap icon. */
:root[data-drag-mode="insert"] .header__theme-icon--drag-insert { display: block; }
:root:not([data-drag-mode="insert"]) .header__theme-icon--drag-swap { display: block; }

/* ===== Banner (settings store unavailable) ===== */
.banner {
  padding: 10px 16px;
  text-align: center;
  font-size: .85rem;
}
.banner--warn {
  background: #fef3c7;
  color: #78350f;
  border-bottom: 1px solid #fcd34d;
}
:root[data-theme="dark"] .banner--warn {
  background: #422006;
  color: #fde68a;
  border-bottom-color: #92400e;
}

/* ===== Info note (top-of-page hint, e.g. how to request app access).
   Sits below the header/banner and above the layout.  Uses accent
   tokens so it picks up theme-correct colours automatically. */
.info-note {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  margin: 12px 24px 0;
  padding: 10px 14px;
  background: var(--accent-soft);
  border: 1px solid var(--border);
  border-left: 3px solid var(--accent);
  border-radius: var(--radius-sm);
  color: var(--text-secondary);
  font-size: .82rem;
  line-height: 1.45;
}
.info-note__icon {
  flex: 0 0 auto;
  width: 16px;
  height: 16px;
  margin-top: 1px;
  color: var(--accent);
}
.info-note__text { min-width: 0; }

/* ===== Toast (transient API failure notice) ===== */
.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: var(--bg-elevated, #1f2937);
  color: #fff;
  padding: 10px 18px;
  border-radius: 6px;
  box-shadow: 0 4px 12px rgba(0,0,0,.3);
  font-size: .85rem;
  opacity: 0;
  transition: opacity .25s ease, transform .25s ease;
  pointer-events: none;
  z-index: 100;
}
.toast--show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ===== Chiclet corner buttons (star + hide) =====
   Both live in the top-right corner.  Star is to the left of hide so
   the destructive "remove" action doesn't sit under the cursor when
   the user reaches for the star.  Visible on hover; star stays
   visible always when filled (favourited) so the indicator is read
   without interaction. */
.chiclet { position: relative; }
.chiclet__hide,
.chiclet__star {
  position: absolute;
  top: 4px;
  width: 18px;
  height: 18px;
  padding: 0;
  border: 0;
  border-radius: 4px;
  background: transparent;
  color: var(--text-muted);
  font-size: 14px;
  line-height: 1;
  cursor: pointer;
  opacity: 0;
  transition: opacity .15s ease, background .15s ease, color .15s ease;
}
.chiclet__hide { right: 4px; }
.chiclet__star { right: 24px; }
.chiclet:hover .chiclet__hide,
.chiclet:hover .chiclet__star { opacity: 1; }
.chiclet__hide:hover,
.chiclet__star:hover { background: var(--bg-elevated); color: var(--text); }
/* Filled star: always visible (no hover required) and gold-tinted. */
.chiclet--favourite .chiclet__star {
  opacity: 1;
  color: #f59e0b;
}
.chiclet--favourite .chiclet__star:hover { color: #d97706; }

/* Favourites section gets a subtle accent so it's recognisable as
   pinned rather than a regular config-defined category. */
.section--favourites .section__title {
  color: #f59e0b;
  cursor: default;
}
.section--favourites .section__title::before {
  background: #f59e0b;
}

/* ===== User-hidden chiclet (visible only in show-hidden mode) ===== */
.chiclet--user-hidden {
  opacity: 0.4;
  filter: grayscale(0.6);
}
.chiclet--user-hidden:hover { opacity: 0.7; }

/* ===== Smooth in-page nav ===== */
html { scroll-behavior: smooth; }

/* ===== Layout (sidebar + main) ===== */
.layout {
  display: flex;
  align-items: flex-start;
  gap: 32px;
  padding: 0 24px;
  /* No max-width: sidebar sits near the viewport's left edge, the grid
   * fills whatever horizontal space is available — more columns on wide
   * monitors, less vertical scrolling for users with lots of apps. */
}

/* ===== Sidebar ===== */
.sidebar {
  width: 240px;
  flex: 0 0 240px;
  position: sticky;
  top: 16px;
  padding: 28px 0 24px;
  max-height: calc(100vh - 16px);
  overflow-y: auto;
}

.sidebar__search-wrap {
  position: relative;
  margin-bottom: 18px;
}

.sidebar__search-icon {
  position: absolute;
  left: 10px;
  top: 50%;
  transform: translateY(-50%);
  width: 14px;
  height: 14px;
  color: var(--text-muted);
  pointer-events: none;
}

.sidebar__search {
  width: 100%;
  padding: 8px 12px 8px 32px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: .85rem;
  font-family: inherit;
  outline: none;
  transition: border-color .15s ease, box-shadow .15s ease;
}
.sidebar__search:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
.sidebar__search::placeholder { color: var(--text-muted); }
.sidebar__search::-webkit-search-cancel-button {
  -webkit-appearance: none;
  appearance: none;
}

.sidebar__nav {
  display: flex;
  flex-direction: column;
  gap: 1px;
}

.sidebar__item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 7px 10px;
  border-radius: var(--radius-sm);
  text-decoration: none;
  color: var(--text-secondary);
  font-size: .82rem;
  font-weight: 500;
  transition: background .12s ease, color .12s ease;
}
.sidebar__item:hover {
  background: var(--accent-soft);
  color: var(--text);
}
.sidebar__item--empty {
  opacity: .35;
  pointer-events: none;
}

.sidebar__item-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.sidebar__item-count {
  flex: 0 0 auto;
  min-width: 22px;
  padding: 1px 7px;
  font-size: .68rem;
  font-weight: 600;
  text-align: center;
  color: var(--text-muted);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
}

/* Header version chip — same chrome as the toggle buttons but
 * non-interactive (it's a <span>, not a <button>; cursor reverts to
 * default and the hover-tint stays so it still feels in-line).  The
 * version itself is exposed via title= so a hover reveals "v0.8.0"
 * without spending header real estate on the digits. */
.header__version {
  cursor: default;
}
.header__version:hover {
  background: transparent;
  border-color: rgba(255, 255, 255, .14);
  color: rgba(243, 244, 246, .85);
}

/* ===== Main ===== */
.main {
  flex: 1;
  min-width: 0;
  padding: 28px 0 80px;
}

/* When the sidebar is absent (e.g. empty dashboard), keep main centred */
.layout > .main:only-child {
  max-width: 1200px;
  margin: 0 auto;
}

/* Filter-hidden states.  Both classes are doubled in their selectors so they
 * beat the base .chiclet / .section { display:flex|block } rules that come
 * later in the file by specificity (0,2,0 vs 0,1,0). */
.chiclet.chiclet--hidden,
.section.section--hidden { display: none; }

/* Don't let sticky elements hide section start */
.section { scroll-margin-top: 16px; }

/* ===== Sections ===== */
.section + .section { margin-top: 36px; }

/* ===== Drag-to-reorder =====
   Section titles are the drag handle for moving categories; chiclets
   are draggable for reordering within a section.  Visual cue is just
   reduced opacity on the dragging element — keeps the drop landing
   spot obvious without flashy indicators. */
.section__title { cursor: grab; user-select: none; }
.section__title:active { cursor: grabbing; }
.chiclet.dragging,
.section.dragging-section { opacity: 0.4; }

/* ===== Compact layout =====
   Packs sections into a multi-column grid so two short categories sit
   side-by-side instead of stacking down the page.  auto-fill +
   minmax(380px, 1fr) means the layout self-collapses to a single column
   on narrow viewports — no media query needed.  align-items: start
   keeps sections of unequal height from stretching to match each other. */
:root[data-layout="compact"] .main {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(380px, 1fr));
  column-gap: 32px;
  row-gap: 24px;
  align-items: start;
}
:root[data-layout="compact"] .main .section + .section { margin-top: 0; }

.section__title {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 0 0 14px;
  padding-bottom: 8px;
  border-bottom: 1px solid var(--border);
  font-size: .68rem;
  font-weight: 600;
  letter-spacing: .12em;
  text-transform: uppercase;
  color: var(--text-muted);
}

.section__title::before {
  content: "";
  width: 2px;
  height: 12px;
  background: var(--accent);
  border-radius: 1px;
}

/* ===== Grid ===== */
.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  /* row-gap deliberately tighter than column-gap — chiclet height is
     ~80 px, so equal gaps make rows feel widely spaced relative to the
     row size. */
  gap: 6px 12px;
}

/* ===== Chiclet (App Card) ===== */
.chiclet {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  padding: 20px 14px 16px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-card);
  text-decoration: none;
  color: var(--text);
  box-shadow: var(--shadow-sm);
  transition: border-color .15s ease, box-shadow .15s ease, background .15s ease;
}
.chiclet:hover {
  border-color: var(--accent);
  box-shadow: var(--shadow-md);
  background: var(--bg-elevated);
}
.chiclet:hover .chiclet__name { color: var(--accent); }

.chiclet__icon {
  width: 48px;
  height: 48px;
  border-radius: var(--radius-icon);
  color: #ffffff;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 6px rgb(0 0 0 / .12);
}
.chiclet__icon svg,
.chiclet__icon img {
  width: 28px;
  height: 28px;
  object-fit: contain;
}

.chiclet__name {
  font-size: .82rem;
  font-weight: 500;
  line-height: 1.3;
  text-align: center;
  color: var(--text);
  word-break: break-word;
  max-width: 100%;
  transition: color .15s ease;
}

/* Disabled / no-URL */
.chiclet--disabled {
  cursor: default;
  opacity: .55;
}
.chiclet--disabled:hover {
  border-color: var(--border);
  box-shadow: var(--shadow-sm);
}
.chiclet--disabled:hover .chiclet__name { color: var(--text); }

.chiclet__reason {
  font-size: .68rem;
  font-style: italic;
  line-height: 1.3;
  text-align: center;
  color: var(--text-muted);
  margin-top: -4px;
  word-break: break-word;
}

.chiclet__env {
  display: inline-block;
  margin-top: -4px;
  padding: 1px 7px;
  font-size: .62rem;
  font-weight: 700;
  letter-spacing: .08em;
  text-transform: uppercase;
  border-radius: 4px;
  /* default (LAB-style amber) — used for any unrecognised env label */
  background: rgba(245, 158, 11, .14);
  color: #b45309;
  border: 1px solid rgba(245, 158, 11, .28);
}

/* PROD — calm green */
.chiclet__env[data-env="PROD"] {
  background: rgba(22, 163, 74, .12);
  color: #15803d;
  border-color: rgba(22, 163, 74, .26);
}

/* Dark-mode overrides — softer hues that read on dark surfaces */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .chiclet__env {
    background: rgba(251, 191, 36, .12);
    color: #fbbf24;
    border-color: rgba(251, 191, 36, .25);
  }
  :root:not([data-theme="light"]) .chiclet__env[data-env="PROD"] {
    background: rgba(74, 222, 128, .1);
    color: #4ade80;
    border-color: rgba(74, 222, 128, .22);
  }
}
:root[data-theme="dark"] .chiclet__env {
  background: rgba(251, 191, 36, .12);
  color: #fbbf24;
  border-color: rgba(251, 191, 36, .25);
}
:root[data-theme="dark"] .chiclet__env[data-env="PROD"] {
  background: rgba(74, 222, 128, .1);
  color: #4ade80;
  border-color: rgba(74, 222, 128, .22);
}

/* ===== Login screen ===== */
.login {
  min-height: calc(100vh - 56px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 32px 24px;
}

.login__card {
  width: 100%;
  max-width: 380px;
  padding: 40px 36px 36px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: var(--shadow-md);
  text-align: center;
}

.login__logo {
  width: 56px;
  height: 56px;
  margin: 0 auto 20px;
  display: block;
}

.login__title {
  margin: 0 0 6px;
  font-size: 1.4rem;
  font-weight: 600;
  letter-spacing: -.02em;
  color: var(--text);
}

.login__subtitle {
  margin: 0 0 28px;
  font-size: .88rem;
  line-height: 1.55;
  color: var(--text-secondary);
}

.login__button {
  display: block;
  width: 100%;
  padding: 11px 16px;
  background: var(--accent);
  color: #fff;
  border: 0;
  border-radius: 8px;
  font-size: .92rem;
  font-weight: 600;
  letter-spacing: -.005em;
  text-decoration: none;
  text-align: center;
  cursor: pointer;
  font-family: inherit;
  transition: filter .12s ease, transform .08s ease;
}
.login__button:hover  { filter: brightness(1.08); }
.login__button:active { transform: scale(.985); }

.login__hint {
  margin: 14px 0 0;
  font-size: .72rem;
  color: var(--text-muted);
}

/* ===== Empty State ===== */
.empty {
  text-align: center;
  padding: 100px 24px;
  color: var(--text-muted);
}
.empty__icon {
  width: 72px;
  height: 72px;
  border-radius: 16px;
  color: #ffffff;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto 20px;
  opacity: .5;
  box-shadow: var(--shadow-sm);
}
.empty__icon svg { width: 36px; height: 36px; }
.empty__text {
  font-size: .95rem;
  font-weight: 500;
}

/* ===== Responsive ===== */

/* Mid-range: keep sidebar beside grid but narrow it slightly so the grid
 * gains a little room.  No layout flip yet. */
@media (max-width: 1100px) {
  .layout { gap: 24px; padding: 0 18px; }
  .sidebar { width: 200px; flex: 0 0 200px; }
}

/* Small viewports: stack sidebar above grid, turn the section nav into a
 * horizontal pill row.  Header still horizontal here.  Also drop the card
 * min-width so the grid keeps showing multiple columns instead of letting
 * a single card stretch to the full width via 1fr. */
@media (max-width: 860px) {
  .layout {
    flex-direction: column;
    /* Without this, the row-mode `align-items: flex-start` causes children
     * to NOT stretch on the cross-axis (now horizontal) → main collapses
     * to its content width and the grid shows ~1 column with empty space
     * to the right.  Stretch makes both sidebar and main span full width. */
    align-items: stretch;
    gap: 12px;
    padding: 0 16px;
  }
  .sidebar {
    width: auto;
    flex: 1 1 auto;
    position: static;
    max-height: none;
    overflow: visible;
    padding: 18px 0 0;
  }
  .sidebar__nav {
    flex-direction: row;
    flex-wrap: wrap;
    gap: 6px;
  }
  .sidebar__item {
    background: var(--bg-elevated);
    border: 1px solid var(--border);
    padding: 5px 10px;
  }
  .main { padding: 12px 0 60px; }
  .grid { grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); gap: 12px; }
}

/* Header gets compressed before the full mobile breakpoint to avoid the
 * brand title fighting with the user controls. */
@media (max-width: 720px) {
  .header { padding: 0 16px; }
  .header__title { font-size: .95rem; }
  .header__logout { padding: 5px 10px; font-size: .76rem; }
}

/* Phone-sized: stack the header vertically, tighten card grid. */
@media (max-width: 560px) {
  .header {
    flex-direction: column;
    height: auto;
    padding: 12px 16px;
    gap: 10px;
    text-align: center;
  }
  .header__brand { justify-content: center; }
  .grid {
    grid-template-columns: repeat(auto-fill, minmax(105px, 1fr));
    gap: 8px;
  }
  .chiclet { padding: 14px 8px 10px; }
  .chiclet__icon { width: 42px; height: 42px; }
  .chiclet__icon svg,
  .chiclet__icon img { width: 24px; height: 24px; }
  .chiclet__name { font-size: .76rem; }
  .section + .section { margin-top: 28px; }
  .login__card { padding: 32px 24px 28px; }
}

/* Very narrow (≤ ~360 px): keep at least 2 columns by shrinking the min
 * once more.  Even at 320 px viewport this still fits 2 cards. */
@media (max-width: 380px) {
  .grid {
    grid-template-columns: repeat(auto-fill, minmax(95px, 1fr));
    gap: 6px;
  }
  .chiclet__icon { width: 38px; height: 38px; }
  .chiclet__icon svg,
  .chiclet__icon img { width: 22px; height: 22px; }
}
