/* Apple-style admin UI.
   Tokens roughly mirror Apple's design system (HIG): SF system fonts, light
   gray bg #f5f5f7, accent #0071e3, ample whitespace, 12-18px radii.
   Mobile-first considerations live in the @media block at the bottom:
   collapsible nav drawer, scrollable tables, 16px input font (no iOS zoom),
   ≥44px touch targets. */

:root {
    --bg: #f5f5f7;
    --surface: #ffffff;
    --surface-2: #fafafa;
    --border: #d2d2d7;
    --border-soft: #e5e5ea;
    --text: #1d1d1f;
    --text-secondary: #6e6e73;
    --text-tertiary: #86868b;
    --accent: #0071e3;
    --accent-hover: #0077ed;
    --accent-active: #006edb;
    --accent-soft: #e3f0ff;
    --danger: #ff3b30;
    --danger-soft: #ffeceb;
    --success: #34c759;
    --success-soft: #e7f8ec;
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.04);
    --shadow: 0 4px 16px rgba(0, 0, 0, 0.06);
    --shadow-lg: 0 24px 48px rgba(0, 0, 0, 0.08);
    --radius-sm: 8px;
    --radius: 14px;
    --radius-lg: 18px;
    --sidebar-width: 240px;
    --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* {
    box-sizing: border-box;
}

html, body {
    margin: 0;
    padding: 0;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "SF Pro Display",
        "Helvetica Neue", Helvetica, Arial, sans-serif;
    /* 16px so iOS doesn't auto-zoom on input focus. */
    font-size: 16px;
    line-height: 1.5;
    color: var(--text);
    background-color: var(--bg);
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    -webkit-tap-highlight-color: transparent;
}

a {
    color: var(--accent);
    text-decoration: none;
}

a:hover {
    text-decoration: underline;
}

h1, h2, h3, h4 {
    margin: 0 0 12px;
    letter-spacing: -0.015em;
}

h1 { font-size: 32px; font-weight: 600; }
h2 { font-size: 22px; font-weight: 600; }
h3 { font-size: 17px; font-weight: 600; }

p { margin: 0 0 12px; }

/* ---------- Layout ---------- */

.app {
    display: flex;
    min-height: 100vh;
}

.sidebar {
    width: var(--sidebar-width);
    flex-shrink: 0;
    background-color: var(--surface);
    border-right: 1px solid var(--border);
    position: sticky;
    top: 0;
    height: 100vh;
    display: flex;
    flex-direction: column;
}

.sidebar__header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 24px 16px 16px;
    gap: 8px;
}

.sidebar__brand {
    font-size: 17px;
    font-weight: 600;
    color: var(--text);
    line-height: 1.15;
    text-decoration: none;
    flex: 1;
    min-width: 0;
}

.sidebar__brand small {
    display: block;
    font-size: 12px;
    color: var(--text-secondary);
    font-weight: 400;
    margin-top: 2px;
}

.sidebar__brand:hover { text-decoration: none; }

.sidebar__toggle {
    display: none;
    flex-direction: column;
    gap: 5px;
    width: 44px;
    height: 44px;
    padding: 0;
    border: 1px solid var(--border);
    background: var(--surface);
    border-radius: 10px;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    flex-shrink: 0;
}

.sidebar__toggle span {
    display: block;
    width: 18px;
    height: 2px;
    background: var(--text);
    border-radius: 2px;
    transition: transform 0.2s ease, opacity 0.2s ease;
}

.sidebar.is-open .sidebar__toggle span:nth-child(1) {
    transform: translateY(7px) rotate(45deg);
}
.sidebar.is-open .sidebar__toggle span:nth-child(2) {
    opacity: 0;
}
.sidebar.is-open .sidebar__toggle span:nth-child(3) {
    transform: translateY(-7px) rotate(-45deg);
}

.sidebar__panel {
    display: flex;
    flex-direction: column;
    flex: 1;
    padding: 0 16px 16px;
}

.sidebar__nav {
    list-style: none;
    margin: 0;
    padding: 0;
    flex: 1;
}

.sidebar__nav li + li {
    margin-top: 2px;
}

.sidebar__nav a {
    display: block;
    padding: 10px 12px;
    border-radius: var(--radius-sm);
    color: var(--text);
    font-weight: 500;
    transition: background-color 0.12s ease;
}

.sidebar__nav a:hover {
    background-color: var(--surface-2);
    text-decoration: none;
}

.sidebar__nav a.active {
    background-color: var(--accent-soft);
    color: var(--accent);
}

.sidebar__footer {
    padding: 12px 0 0;
    border-top: 1px solid var(--border);
    margin-top: 12px;
    color: var(--text-secondary);
    font-size: 13px;
}

.sidebar__user {
    padding: 0 4px 8px;
    overflow: hidden;
    text-overflow: ellipsis;
}

.sidebar__footer form {
    margin: 0;
}

.main {
    flex: 1;
    min-width: 0;
    padding: 32px 40px 64px;
    max-width: 1200px;
}

.page-header {
    margin-bottom: 24px;
}

.page-header__title {
    font-size: 30px;
    font-weight: 600;
    letter-spacing: -0.02em;
    margin: 0 0 4px;
}

.page-header__subtitle {
    color: var(--text-secondary);
    font-size: 15px;
    margin: 0;
}

/* ---------- Cards & lists ---------- */

.card {
    background: var(--surface);
    border-radius: var(--radius);
    box-shadow: var(--shadow-sm);
    border: 1px solid var(--border);
}

.card + .card {
    margin-top: 16px;
}

.card__body {
    padding: 24px;
}

.card__header {
    padding: 18px 24px;
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    flex-wrap: wrap;
}

.card__header h2 {
    margin: 0;
    font-size: 17px;
    font-weight: 600;
}

.kpi-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
    gap: 16px;
    margin-bottom: 24px;
}

.kpi {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 20px;
    box-shadow: var(--shadow-sm);
}

.kpi__label {
    color: var(--text-secondary);
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    margin-bottom: 6px;
}

.kpi__value {
    font-size: 30px;
    font-weight: 600;
    letter-spacing: -0.02em;
}

/* ---------- Tables ---------- */

.table-wrapper {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    border-radius: var(--radius);
    border: 1px solid var(--border);
    box-shadow: var(--shadow-sm);
    background: var(--surface);
}

.table {
    width: 100%;
    border-collapse: collapse;
    background: var(--surface);
    /* Wrapper handles border + shadow + radius so we can stay flush inside it */
}

/* Backwards compatibility: old templates that still use .table without
   the wrapper get the border styles directly. */
.table:not(.table-wrapper > .table) {
    border-radius: var(--radius);
    overflow: hidden;
    border: 1px solid var(--border);
    box-shadow: var(--shadow-sm);
}

.table th, .table td {
    text-align: left;
    padding: 12px 16px;
    border-bottom: 1px solid var(--border-soft);
    font-size: 14px;
    vertical-align: middle;
}

.table th {
    background: var(--surface-2);
    font-weight: 600;
    color: var(--text-secondary);
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    white-space: nowrap;
}

.table tr:last-child td {
    border-bottom: none;
}

.table tr:hover td {
    background: var(--surface-2);
}

.table .muted {
    color: var(--text-tertiary);
}

/* ---------- Forms ---------- */

.form {
    display: flex;
    flex-direction: column;
    gap: 18px;
}

.form-row {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.form-row label {
    font-weight: 500;
    font-size: 13px;
    color: var(--text-secondary);
}

.form-row input[type="text"],
.form-row input[type="password"],
.form-row input[type="url"],
.form-row input[type="number"],
.form-row input[type="datetime-local"],
.form-row textarea,
.form-row select {
    width: 100%;
    border: 1px solid var(--border);
    background: var(--surface);
    border-radius: var(--radius-sm);
    padding: 12px 14px;
    /* 16px so iOS doesn't zoom on focus. */
    font-size: 16px;
    font-family: inherit;
    color: var(--text);
    transition: border-color 0.12s ease, box-shadow 0.12s ease;
    -webkit-appearance: none;
    appearance: none;
}

.form-row input:focus,
.form-row textarea:focus,
.form-row select:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(0, 113, 227, 0.18);
}

.form-row textarea {
    min-height: 200px;
    resize: vertical;
    font-family: ui-monospace, SF Mono, Menlo, monospace;
    line-height: 1.5;
    font-size: 15px;
}

.form-row .hint {
    color: var(--text-tertiary);
    font-size: 12px;
}

.form-row--inline {
    flex-direction: row;
    align-items: center;
    gap: 10px;
}

.form-row--inline input[type="checkbox"] {
    width: 22px;
    height: 22px;
    margin: 0;
    accent-color: var(--accent);
}

.form-row--inline label {
    font-size: 15px;
    color: var(--text);
    margin: 0;
}

/* Two-column layout for short/related fields (date+duration, capacity+photo). */
.form-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 18px;
}

@media (max-width: 600px) {
    .form-grid { grid-template-columns: 1fr; gap: 18px; }
}

/* Cancelled-registration row in admin tables — visually de-emphasised. */
.row--muted td { color: var(--text-tertiary); }
.row--muted a { color: var(--text-secondary); }

/* ---------- Buttons ---------- */

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 10px 20px;
    min-height: 40px;
    border-radius: 999px;
    font-size: 15px;
    font-weight: 500;
    background: var(--accent);
    color: #fff;
    border: 1px solid var(--accent);
    cursor: pointer;
    transition: background-color 0.12s ease, transform 0.1s ease;
    text-decoration: none;
    line-height: 1;
    white-space: nowrap;
}

.btn:hover {
    background: var(--accent-hover);
    text-decoration: none;
    color: #fff;
}

.btn:active {
    background: var(--accent-active);
    transform: translateY(1px);
}

.btn--secondary {
    background: var(--surface);
    color: var(--text);
    border: 1px solid var(--border);
}

.btn--secondary:hover {
    background: var(--surface-2);
    color: var(--text);
}

.btn--danger {
    background: var(--surface);
    color: var(--danger);
    border: 1px solid var(--border);
}

.btn--danger:hover {
    background: var(--danger-soft);
    color: var(--danger);
}

.btn--small {
    padding: 7px 14px;
    font-size: 13px;
    min-height: 34px;
}

.btn--block {
    width: 100%;
}

.actions {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
    align-items: center;
}

.actions--end {
    justify-content: flex-end;
}

/* ---------- Auth ---------- */

.auth {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 24px;
}

.auth__card {
    width: 100%;
    max-width: 380px;
    background: var(--surface);
    border-radius: var(--radius-lg);
    border: 1px solid var(--border);
    box-shadow: var(--shadow-lg);
    padding: 32px;
}

.auth__title {
    font-size: 24px;
    font-weight: 600;
    margin: 0 0 4px;
}

.auth__subtitle {
    color: var(--text-secondary);
    font-size: 14px;
    margin: 0 0 24px;
}

/* ---------- Pills / chips ---------- */

.chip {
    display: inline-flex;
    align-items: center;
    padding: 3px 10px;
    border-radius: 999px;
    font-size: 12px;
    font-weight: 500;
    border: 1px solid var(--border);
    background: var(--surface-2);
    color: var(--text-secondary);
    white-space: nowrap;
}

.chip--accent { background: var(--accent-soft); color: var(--accent); border-color: var(--accent-soft); }
.chip--success { background: var(--success-soft); color: #1d8c3f; border-color: var(--success-soft); }
.chip--muted { color: var(--text-tertiary); }

.alert {
    border-radius: var(--radius);
    padding: 12px 16px;
    font-size: 14px;
    border: 1px solid transparent;
    margin-bottom: 16px;
}

.alert--error { background: var(--danger-soft); color: var(--danger); border-color: #ffcfcc; }
.alert--success { background: var(--success-soft); color: #1d8c3f; border-color: #c6ecd0; }

/* ---------- Utilities ---------- */

.muted { color: var(--text-secondary); }
.tiny { font-size: 12px; }
.gap-md { gap: 16px; }
.flex { display: flex; }
.flex--col { flex-direction: column; }
.flex--between { justify-content: space-between; }
.flex--center { align-items: center; }

.toolbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 16px;
    flex-wrap: wrap;
}

.search {
    display: flex;
    gap: 8px;
    flex: 1;
    min-width: 0;
    max-width: 600px;
    align-items: center;
    flex-wrap: wrap;
}

.search input,
.search select {
    flex: 1;
    min-width: 0;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 10px 14px;
    font-size: 16px;
    background: var(--surface);
    color: var(--text);
    font-family: inherit;
    -webkit-appearance: none;
    appearance: none;
}

.pagination {
    display: flex;
    gap: 4px;
    margin-top: 16px;
    align-items: center;
    color: var(--text-secondary);
    font-size: 14px;
    flex-wrap: wrap;
}

.pagination a, .pagination span {
    padding: 8px 14px;
    border-radius: var(--radius-sm);
    min-height: 36px;
    display: inline-flex;
    align-items: center;
}

.pagination a:hover {
    background: var(--surface);
    text-decoration: none;
}

.pagination .current {
    background: var(--accent-soft);
    color: var(--accent);
    font-weight: 500;
}

code {
    font-family: ui-monospace, SF Mono, Menlo, monospace;
    font-size: 12px;
    background: var(--surface-2);
    border: 1px solid var(--border-soft);
    padding: 2px 6px;
    border-radius: 6px;
    word-break: break-all;
}

/* ---------- Rich-text editor (text content) ---------- */

.rte {
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--surface);
    overflow: hidden;
}

.rte__toolbar {
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
    padding: 8px 8px 0;
    background: var(--surface-2);
    border-bottom: 1px solid var(--border);
    align-items: center;
}

.rte__sep {
    display: inline-block;
    width: 1px;
    height: 20px;
    background: var(--border);
    margin: 0 4px;
}

.rte__btn {
    min-width: 36px;
    height: 36px;
    padding: 0 10px;
    border: 1px solid transparent;
    background: var(--surface);
    border-radius: 7px;
    cursor: pointer;
    font-family: inherit;
    font-size: 14px;
    color: var(--text);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 8px;
    transition: background-color 0.12s, color 0.12s, border-color 0.12s;
}

.rte__btn:hover {
    background: var(--accent-soft);
    color: var(--accent);
    border-color: var(--accent-soft);
}

.rte__btn:active {
    transform: translateY(1px);
}

.rte__btn--text {
    padding: 0 14px;
    font-size: 13px;
    font-weight: 500;
}

.rte textarea {
    width: 100%;
    border: none !important;
    border-radius: 0 !important;
    background: var(--surface);
    padding: 14px 16px !important;
    font-size: 15px !important;
    font-family: ui-monospace, SF Mono, Menlo, monospace !important;
    line-height: 1.55;
    min-height: 240px;
    resize: vertical;
    box-shadow: none !important;
}

.rte textarea:focus {
    outline: none;
    box-shadow: none !important;
}

.rte:focus-within {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(0, 113, 227, 0.18);
}

.rte__preview {
    margin-top: 16px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    overflow: hidden;
}

.rte__preview-label {
    padding: 8px 14px;
    background: var(--surface-2);
    border-bottom: 1px solid var(--border);
    font-size: 12px;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.04em;
}

.rte__preview-body {
    padding: 14px 16px;
    font-size: 15px;
    line-height: 1.5;
    color: var(--text);
    background: var(--surface);
    white-space: pre-wrap;
    word-wrap: break-word;
}

.rte__preview-body a { color: var(--accent); }

.tg-spoiler {
    background: var(--text);
    color: var(--text);
    border-radius: 4px;
    padding: 0 2px;
    cursor: pointer;
    transition: background-color 0.2s;
}

.tg-spoiler:hover {
    background: transparent;
    color: inherit;
}

/* ---------- User detail page ---------- */

.meta-strip {
    display: flex;
    flex-wrap: wrap;
    gap: 6px 24px;
    padding: 12px 16px;
    margin: -8px 0 16px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow-sm);
    font-size: 13px;
}

.meta-strip__item {
    display: inline-flex;
    align-items: baseline;
    gap: 8px;
    min-width: 0;
}

.meta-strip__label {
    color: var(--text-tertiary);
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    font-weight: 500;
    white-space: nowrap;
}

.meta-strip__value {
    color: var(--text);
    font-weight: 500;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

@media (max-width: 800px) {
    .meta-strip {
        gap: 8px;
        flex-direction: column;
    }
    .meta-strip__item {
        justify-content: space-between;
        gap: 12px;
    }
}

/* Activity feed */

.feed {
    list-style: none;
    margin: 0;
    padding: 0;
}

.feed__item {
    display: flex;
    gap: 12px;
    padding: 14px 20px;
    border-bottom: 1px solid var(--border-soft);
}

.feed__item:last-child { border-bottom: none; }

.feed__icon {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    background: var(--surface-2);
    color: var(--text-secondary);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    font-weight: 600;
    flex-shrink: 0;
}

.feed__item--message_in .feed__icon { background: var(--accent-soft); color: var(--accent); }
.feed__item--message_out .feed__icon { background: var(--success-soft); color: #1d8c3f; }
.feed__item--callback .feed__icon { background: #fff3d6; color: #b86b00; }

.feed__body {
    flex: 1;
    min-width: 0;
}

.feed__meta {
    font-size: 12px;
    color: var(--text-secondary);
    margin-bottom: 4px;
    text-transform: uppercase;
    letter-spacing: 0.04em;
}

.feed__payload {
    font-size: 14px;
    color: var(--text);
    white-space: pre-wrap;
    word-wrap: break-word;
    line-height: 1.45;
}

.feed__item--callback .feed__payload {
    font-family: ui-monospace, SF Mono, Menlo, monospace;
    font-size: 13px;
    color: var(--text-secondary);
}

/* ---------- Responsive ---------- */

@media (max-width: 800px) {
    body { font-size: 16px; }
    h1 { font-size: 26px; }
    h2 { font-size: 19px; }

    .app {
        flex-direction: column;
    }

    .sidebar {
        width: 100%;
        height: auto;
        position: sticky;
        top: 0;
        z-index: 50;
        border-right: none;
        border-bottom: 1px solid var(--border);
        backdrop-filter: saturate(180%) blur(20px);
        -webkit-backdrop-filter: saturate(180%) blur(20px);
        background-color: rgba(255, 255, 255, 0.85);
    }

    .sidebar__header {
        padding: 12px 16px;
    }

    .sidebar__toggle {
        display: flex;
    }

    .sidebar__panel {
        display: none;
        padding: 0 16px 16px;
        max-height: calc(100vh - 70px);
        overflow-y: auto;
    }

    .sidebar.is-open .sidebar__panel {
        display: flex;
    }

    .sidebar__nav a {
        padding: 14px 12px;
        font-size: 16px;
    }

    .sidebar__footer {
        padding-top: 16px;
        margin-top: 16px;
    }

    .main {
        padding: 20px 16px calc(48px + var(--safe-bottom));
    }

    .page-header {
        margin-bottom: 16px;
    }

    .page-header__title { font-size: 24px; }
    .page-header__subtitle { font-size: 14px; }

    .card__body { padding: 16px; }
    .card__header { padding: 14px 16px; }

    .kpi { padding: 16px; }
    .kpi__value { font-size: 26px; }
    .kpi-grid { gap: 12px; margin-bottom: 16px; }

    .table th, .table td {
        padding: 10px 12px;
        font-size: 14px;
    }

    .auth { padding: 16px; align-items: flex-start; padding-top: 48px; }
    .auth__card { padding: 24px; }

    .btn { min-height: 44px; padding: 12px 20px; }
    .btn--small { min-height: 36px; padding: 8px 14px; }

    .actions { gap: 8px; }
    .actions--end { justify-content: stretch; }
    .actions--end .btn { flex: 1; }

    .toolbar { gap: 10px; }
    .search { max-width: 100%; }
    .search input { font-size: 16px; }

    code { font-size: 11px; }
}

@media (max-width: 420px) {
    .main { padding: 16px 12px 48px; }
    .card__body { padding: 14px; }
    .auth__card { padding: 20px; border-radius: var(--radius); }
    .kpi-grid { grid-template-columns: 1fr; }
}
