:root {
    --bg: #f5f7fb;
    --panel: #ffffff;
    --ink: #1b2536;
    --muted: #6a7689;
    --line: #e3e8f0;
    --line-strong: #d2d9e6;
    --brand: #2a5bd7;
    --brand-ink: #ffffff;
    --brand-soft: #eaf0ff;
    --danger: #c8362b;
    --good: #1f8f4b;

    --topbar-bg: #101828;
    --topbar-bg-hi: #1a2540;
    --topbar-ink: #f1f4fb;
    --topbar-ink-dim: #98a5c2;
    --topbar-hover: rgba(255, 255, 255, 0.08);
    --topbar-active: rgba(255, 255, 255, 0.14);
    --topbar-border: rgba(255, 255, 255, 0.08);
    --topbar-h: 56px;

    --shadow-sm: 0 1px 2px rgba(16, 24, 40, 0.06);
    --shadow-md: 0 6px 18px rgba(16, 24, 40, 0.08);
    --shadow-lg: 0 16px 40px rgba(16, 24, 40, 0.18);
    --radius-sm: 6px;
    --radius-md: 10px;
    --radius-lg: 14px;
}

* { box-sizing: border-box; }
html, body {
    margin: 0; height: 100%;
    background: var(--bg); color: var(--ink);
    font-family: -apple-system, BlinkMacSystemFont, "Inter", "Segoe UI", Roboto, sans-serif;
    -webkit-font-smoothing: antialiased;
}

a { color: var(--brand); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ─── Page chrome ─────────────────────────────────────────────────────── */

.page { display: flex; flex-direction: column; min-height: 100vh; }
main  { flex: 1; padding: 24px 28px 40px; }
.content { max-width: 1400px; margin: 0 auto; }

/* ─── Top bar ────────────────────────────────────────────────────────── */

.topbar {
    display: grid;
    grid-template-columns: auto 1fr auto;
    align-items: stretch;
    background: var(--topbar-bg);
    color: var(--topbar-ink);
    min-height: var(--topbar-h);
    position: sticky;
    top: 0;
    z-index: 50;
    border-bottom: 1px solid var(--topbar-border);
    padding: 0 18px;
}

.topbar-left, .topbar-right { display: flex; align-items: center; gap: 14px; }
.topbar-right { gap: 10px; }

.topbar a, .topbar a:visited { color: var(--topbar-ink); }
.topbar a:hover { text-decoration: none; color: #fff; }

.topbar-brand {
    font-size: 1.02rem;
    letter-spacing: 0.1px;
    padding: 0 4px;
    color: var(--topbar-ink) !important;
}
.topbar-brand strong { color: #fff; font-weight: 700; }

.topbar-sep {
    color: var(--topbar-ink-dim);
    font-weight: 300;
    user-select: none;
    margin: 0 2px;
}
.topbar-appname {
    font-weight: 600;
    font-size: 0.97rem;
    padding: 6px 10px;
    border-radius: var(--radius-sm);
}
.topbar-appname:hover { background: var(--topbar-hover); }

.topbar .user {
    color: var(--topbar-ink-dim);
    font-size: 0.92rem;
    padding-right: 4px;
    max-width: 220px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.topbar-iconlink {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 34px; height: 34px;
    border-radius: var(--radius-sm);
    background: transparent;
    border: 0;
    cursor: pointer;
    color: var(--topbar-ink-dim);
    padding: 0;
}
.topbar-iconlink svg { width: 18px; height: 18px; display: block; }
.topbar-iconlink:hover { background: var(--topbar-hover); color: #fff; }
.logout-form { margin: 0; display: inline-flex; }

/* ─── App switcher (waffle dropdown) ─────────────────────────────────── */

.app-switcher { position: relative; }
.app-switcher > summary {
    list-style: none;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 38px;
    height: 38px;
    border-radius: var(--radius-sm);
    color: var(--topbar-ink);
    transition: background 0.12s ease;
}
.app-switcher > summary::-webkit-details-marker,
.app-switcher > summary::marker { display: none; content: ""; }
.app-switcher > summary:hover { background: var(--topbar-hover); }
.app-switcher[open] > summary { background: var(--topbar-active); }
.waffle-svg { width: 22px; height: 22px; display: block; color: var(--topbar-ink); }

.app-switcher-panel {
    position: absolute;
    top: calc(100% + 8px);
    left: 0;
    background: var(--panel);
    color: var(--ink);
    border: 1px solid var(--line);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-lg);
    padding: 18px 18px 20px;
    min-width: 560px;
    z-index: 60;
}
.app-switcher-header {
    font-size: 0.78rem;
    font-weight: 600;
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: 0.06em;
    margin-bottom: 12px;
}

/* ─── Top submenu (horizontal) ───────────────────────────────────────── */

.topbar-submenu {
    display: flex;
    align-items: stretch;
    gap: 2px;
    min-height: var(--topbar-h);
    overflow-x: auto;
    overflow-y: hidden;
    white-space: nowrap;
    padding: 0 8px;
    scrollbar-width: thin;
}
.topbar-submenu::-webkit-scrollbar { height: 4px; }
.topbar-submenu::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.18); border-radius: 2px; }

.topbar-submenu .topbar-link {
    display: inline-flex;
    align-items: center;
    padding: 0 14px;
    color: var(--topbar-ink-dim);
    font-size: 0.93rem;
    font-weight: 500;
    border-bottom: 2px solid transparent;
    transition: color 0.12s ease, border-color 0.12s ease, background 0.12s ease;
}
.topbar-submenu .topbar-link:hover {
    color: #fff;
    background: var(--topbar-hover);
}
.topbar-submenu .topbar-link.active {
    color: #fff;
    border-bottom-color: var(--brand);
    background: linear-gradient(180deg, transparent 70%, rgba(42,91,215,0.12) 100%);
}

/* ─── Apps grid (home + switcher panel) ──────────────────────────────── */

.apps-grid-title {
    margin: 0 0 4px;
    font-size: 1.6rem;
    font-weight: 700;
    letter-spacing: -0.01em;
}

.apps-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(170px, 1fr));
    gap: 18px;
    margin-top: 20px;
}

.app-tile {
    --app-color: var(--brand);
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: flex-start;
    gap: 14px;
    padding: 22px 14px 18px;
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: var(--radius-lg);
    text-align: center;
    color: var(--ink);
    transition: transform 0.12s ease, box-shadow 0.18s ease, border-color 0.12s ease;
    overflow: hidden;
}
.app-tile::before {
    content: "";
    position: absolute;
    top: 0; left: 0; right: 0;
    height: 3px;
    background: var(--app-color);
    opacity: 0;
    transition: opacity 0.18s ease;
}
.app-tile:hover {
    text-decoration: none;
    border-color: var(--line-strong);
    box-shadow: var(--shadow-md);
    transform: translateY(-2px);
}
.app-tile:hover::before { opacity: 1; }

.app-tile-icon {
    width: 56px;
    height: 56px;
    border-radius: 14px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    color: #fff;
    background: var(--app-color);
    box-shadow: 0 6px 14px -4px color-mix(in srgb, var(--app-color) 60%, transparent);
}
.app-tile-icon .app-svg { width: 28px; height: 28px; display: block; }

.app-tile-title {
    font-weight: 600;
    font-size: 0.95rem;
    line-height: 1.25;
    color: var(--ink);
}

.app-tile.is-disabled {
    opacity: 0.72;
    cursor: not-allowed;
}
.app-tile.is-disabled .app-tile-title { color: var(--muted); }
.app-tile.is-disabled:hover {
    transform: none;
    box-shadow: none;
    border-color: var(--line);
}
.app-tile.is-disabled:hover::before { opacity: 0; }

.app-tile-badge {
    font-size: 0.7rem;
    font-weight: 600;
    color: var(--muted);
    background: #eef1f7;
    border-radius: 999px;
    padding: 2px 8px;
    letter-spacing: 0.02em;
    text-transform: uppercase;
}

/* Compact tiles inside the switcher panel */
.app-switcher-panel .apps-grid {
    grid-template-columns: repeat(3, minmax(150px, 1fr));
    margin-top: 0;
    gap: 10px;
}
.app-switcher-panel .app-tile {
    padding: 14px 10px;
    border-radius: var(--radius-md);
}
.app-switcher-panel .app-tile-icon {
    width: 44px; height: 44px; border-radius: 11px;
}
.app-switcher-panel .app-tile-icon .app-svg { width: 22px; height: 22px; }
.app-switcher-panel .app-tile-title { font-size: 0.88rem; }

/* ─── Content typography & shared widgets ────────────────────────────── */

h1 { font-size: 1.6rem; margin: 0 0 10px; letter-spacing: -0.01em; }
h2 { font-size: 1.15rem; margin: 0 0 8px; }
.lead { color: var(--muted); }

.page-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 14px; }
.header-actions { display: flex; gap: 8px; }

.button { display: inline-block; padding: 6px 12px; border: 1px solid var(--line);
          background: var(--panel); color: var(--ink); border-radius: var(--radius-sm); cursor: pointer; font: inherit; }
.button:hover { border-color: #b8c2d4; text-decoration: none; }
.button.primary { background: var(--brand); color: var(--brand-ink); border-color: var(--brand); }
.button.primary:hover { background: #1e4cc4; }
.button:disabled { opacity: 0.5; cursor: not-allowed; }

.link-button { background: none; border: 0; color: var(--brand); cursor: pointer; padding: 0; font: inherit; }
.link-button.danger { color: var(--danger); }

.toolbar { display: flex; gap: 8px; align-items: center; margin: 8px 0 16px; flex-wrap: wrap; }
.toolbar input[type=search] { flex: 1 1 280px; padding: 6px 8px; border: 1px solid var(--line); border-radius: var(--radius-sm); }
.toolbar select { padding: 6px 8px; border: 1px solid var(--line); border-radius: var(--radius-sm); }
.toolbar label.inline { display: flex; align-items: center; gap: 6px; color: var(--muted); }

table.grid { width: 100%; border-collapse: collapse; background: var(--panel);
             border: 1px solid var(--line); border-radius: var(--radius-sm); overflow: hidden; }
table.grid th, table.grid td { padding: 8px 10px; text-align: left; border-bottom: 1px solid var(--line); }
table.grid th { background: #eef1f7; font-weight: 600; }
table.grid tbody tr:hover { background: #fafbfd; }
table.grid td.actions { display: flex; gap: 8px; }

.pager { display: flex; align-items: center; gap: 12px; margin-top: 12px; color: var(--muted); }

.form { max-width: 720px; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius-sm); padding: 18px; }
.form fieldset { border: 0; padding: 0; margin: 0 0 14px; }
.form legend { font-weight: 600; padding-bottom: 6px; }
.form label { display: block; margin-bottom: 10px; }
.form label span { display: block; font-size: 0.85rem; color: var(--muted); }
.form label em { color: var(--danger); font-style: normal; }
.form input, .form select, .form textarea {
    width: 100%; padding: 6px 8px; border: 1px solid var(--line); border-radius: var(--radius-sm); font: inherit; background: #fff;
}
.form label.inline { display: flex; align-items: center; gap: 6px; }
.form label.inline input { width: auto; }
.form-actions { display: flex; gap: 8px; margin-top: 14px; }
.form .roles { display: grid; grid-template-columns: repeat(3, minmax(0,1fr)); gap: 4px 18px; }

.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius-sm); padding: 16px 18px; margin-bottom: 14px; }
.card dl { display: grid; grid-template-columns: 160px 1fr; gap: 6px 14px; margin: 0; }
.card dt { color: var(--muted); }
.card dd { margin: 0; }

.alert { padding: 8px 12px; border-radius: var(--radius-sm); margin: 8px 0; }
.alert.error { background: #fde9e7; color: #8a201a; border: 1px solid #f5b5af; }

.form.login { max-width: 380px; margin: 24px auto; }
.cards { list-style: none; padding: 0; }

/* ─── Responsive ─────────────────────────────────────────────────────── */

@media (max-width: 860px) {
    .topbar { grid-template-columns: auto 1fr auto; padding: 0 12px; column-gap: 8px; }
    .topbar .user { display: none; }
    .topbar-submenu { grid-column: 1 / -1; min-height: 44px; padding: 0; border-top: 1px solid var(--topbar-border); }
    .app-switcher-panel { min-width: 88vw; left: -8px; }
    .app-switcher-panel .apps-grid { grid-template-columns: repeat(2, 1fr); }
    main { padding: 18px 14px 28px; }
}
