:root {
    --accent: #0f9d8a;
    --accent-dark: #0b7d6e;
    --accent-soft: #e3f5f2;
    --amber: #f2a03d;
    --amber-soft: #fdf1e1;
    --blue: #3d7bf2;
    --blue-soft: #e6effd;
    --danger: #e0475b;
    --danger-soft: #fdecee;
    --text: #1d2733;
    --muted: #6b7785;
    --border: #e4e9ee;
    --bg: #f4f7f9;
    --card: #ffffff;
    --radius: 14px;
    --radius-sm: 10px;
    --shadow: 0 1px 2px rgba(16, 24, 40, .04), 0 4px 16px rgba(16, 24, 40, .06);
    --sidebar-width: 248px;
    font-size: 15px;
}

* { box-sizing: border-box; }

html, body { margin: 0; }

body {
    font-family: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    color: var(--text);
    background: var(--bg);
    line-height: 1.45;
    -webkit-font-smoothing: antialiased;
}

h1, h2, h3, p { margin: 0; }

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

.muted { color: var(--muted); font-size: .9rem; }

/* Layout */
.layout { display: flex; min-height: 100vh; }

.sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    width: var(--sidebar-width);
    background: var(--card);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    padding: 20px 14px;
    z-index: 40;
    transition: transform .25s ease;
}

.brand { display: flex; align-items: center; gap: 10px; padding: 4px 10px 22px; }
.brand-name { font-weight: 700; font-size: 1.05rem; }
.brand-logo {
    display: inline-flex; align-items: center; justify-content: center;
    width: 36px; height: 36px; border-radius: 10px;
    background: var(--accent); color: #fff;
}
.brand-logo-lg { width: 52px; height: 52px; border-radius: 14px; }

.nav { display: flex; flex-direction: column; gap: 4px; flex: 1; }
.nav-link {
    display: flex; align-items: center; gap: 12px;
    padding: 10px 12px; border-radius: var(--radius-sm);
    color: var(--muted); font-weight: 500;
    transition: background .15s, color .15s;
}
.nav-link:hover { background: var(--bg); color: var(--text); }
.nav-link.active { background: var(--accent-soft); color: var(--accent-dark); }
.nav-logout { margin-top: auto; }
.nav-logout:hover { background: var(--danger-soft); color: var(--danger); }

.main { flex: 1; margin-left: var(--sidebar-width); min-width: 0; display: flex; flex-direction: column; }

.topbar {
    position: sticky; top: 0; z-index: 20;
    display: flex; align-items: center; gap: 12px;
    padding: 14px 28px;
    background: rgba(244, 247, 249, .9);
    backdrop-filter: blur(8px);
    border-bottom: 1px solid var(--border);
}
.topbar-title { font-size: 1.25rem; font-weight: 700; flex: 1; }
.topbar-user { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: .9rem; }
.avatar {
    width: 32px; height: 32px; border-radius: 50%;
    display: inline-flex; align-items: center; justify-content: center;
    background: var(--accent); color: #fff; font-weight: 600;
}
.menu-btn { display: none; }

.content { padding: 24px 28px 40px; max-width: 1200px; width: 100%; }

.sidebar-backdrop { display: none; }

/* Cards */
.card {
    background: var(--card);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 22px;
    min-width: 0;
}
.card-head { margin-bottom: 16px; }
.card-head h2 { font-size: 1.05rem; font-weight: 650; margin-bottom: 4px; }

.stack { display: flex; flex-direction: column; gap: 20px; position: relative; }
.grid-2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 20px; }

/* Stats */
.stat-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 20px; margin-bottom: 20px; }
.stat-card { display: flex; align-items: center; gap: 16px; }
.stat-icon {
    width: 48px; height: 48px; border-radius: 12px; flex-shrink: 0;
    display: inline-flex; align-items: center; justify-content: center;
}
.stat-icon-teal { background: var(--accent-soft); color: var(--accent); }
.stat-icon-amber { background: var(--amber-soft); color: var(--amber); }
.stat-icon-blue { background: var(--blue-soft); color: var(--blue); }
.stat-label { color: var(--muted); font-size: .88rem; }
.stat-value { font-size: 1.75rem; font-weight: 700; }
.stat-grid-compact .stat-value { font-size: 1.4rem; }

.breakdown-bar {
    display: flex; height: 14px; border-radius: 99px; overflow: hidden;
    background: var(--border); margin: 8px 0 16px;
}
.breakdown-bar span { transition: width .5s ease; }
.breakdown-organic { background: var(--accent); }
.breakdown-marketing { background: var(--amber); }

.legend { list-style: none; padding: 0; margin: 0; display: flex; gap: 18px; flex-wrap: wrap; color: var(--muted); font-size: .9rem; }
.legend li { display: flex; align-items: center; gap: 6px; }
.dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; }
.dot-teal { background: var(--accent); }
.dot-amber { background: var(--amber); }

.bar-chart { display: flex; align-items: flex-end; gap: 10px; height: 160px; margin-bottom: 14px; }
.bar-col { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 6px; height: 100%; }
.bar-stack { flex: 1; width: 100%; max-width: 34px; display: flex; flex-direction: column-reverse; border-radius: 6px; overflow: hidden; background: var(--bg); }
.bar-stack .seg-organic { background: var(--accent); }
.bar-stack .seg-marketing { background: var(--amber); }
.bar-label { font-size: .75rem; color: var(--muted); }

/* Forms */
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; margin-top: 12px; }
.field { display: flex; flex-direction: column; gap: 6px; }
.field-full { grid-column: 1 / -1; }
.field label { font-size: .85rem; font-weight: 600; color: #3a4654; }
.field-hint { font-size: .8rem; color: var(--muted); }

input[type="text"], input[type="email"], input[type="password"], input[type="url"],
input[type="number"], input[type="search"], select, textarea {
    width: 100%;
    font: inherit;
    color: var(--text);
    background: #fff;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 10px 12px;
    transition: border-color .15s, box-shadow .15s;
}
textarea { resize: vertical; }
input:focus, select:focus, textarea:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(15, 157, 138, .15);
}
.is-invalid { border-color: var(--danger) !important; }
.is-invalid:focus { box-shadow: 0 0 0 3px rgba(224, 71, 91, .15) !important; }
.field-error { color: var(--danger); font-size: .8rem; min-height: 0; }
.field-error:empty { display: none; }

.input-group { display: flex; }
.input-group input { border-top-right-radius: 0; border-bottom-right-radius: 0; }
.input-addon {
    border: 1px solid var(--border); border-left: 0;
    background: var(--bg); color: var(--muted); font: inherit; font-size: .85rem;
    padding: 0 14px; border-radius: 0 var(--radius-sm) var(--radius-sm) 0; cursor: pointer;
}

.checkbox { display: inline-flex; align-items: center; gap: 8px; margin-top: 14px; color: var(--muted); font-size: .9rem; cursor: pointer; }

/* Toggle */
.toggle-row {
    display: flex; align-items: center; justify-content: space-between; gap: 16px;
    padding: 12px 0; border-bottom: 1px solid var(--border);
}
.toggle-row:last-child { border-bottom: 0; }
.toggle-title { font-weight: 550; }
.toggle-hint { color: var(--muted); font-size: .82rem; }

.switch { position: relative; display: inline-block; width: 44px; height: 24px; flex-shrink: 0; }
.switch input { opacity: 0; width: 0; height: 0; position: absolute; }
.slider {
    position: absolute; inset: 0; cursor: pointer;
    background: #cfd8e0; border-radius: 99px; transition: background .2s;
}
.slider::before {
    content: ""; position: absolute; width: 18px; height: 18px; left: 3px; top: 3px;
    background: #fff; border-radius: 50%; box-shadow: 0 1px 3px rgba(0, 0, 0, .2);
    transition: transform .2s;
}
.switch input:checked + .slider { background: var(--accent); }
.switch input:checked + .slider::before { transform: translateX(20px); }
.switch input:focus-visible + .slider { box-shadow: 0 0 0 3px rgba(15, 157, 138, .25); }

/* Buttons */
.btn {
    display: inline-flex; align-items: center; justify-content: center; gap: 8px;
    font: inherit; font-weight: 600; font-size: .92rem;
    border: 1px solid transparent; border-radius: var(--radius-sm);
    padding: 10px 18px; cursor: pointer; transition: background .15s, opacity .15s, border-color .15s;
}
.btn:disabled { opacity: .65; cursor: not-allowed; }
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--accent-dark); }
.btn-ghost { background: #fff; border-color: var(--border); color: var(--text); }
.btn-ghost:hover:not(:disabled) { border-color: var(--accent); color: var(--accent-dark); }
.btn-danger-ghost { background: transparent; border-color: var(--border); color: var(--danger); }
.btn-danger-ghost:hover { background: var(--danger-soft); border-color: var(--danger-soft); }
.btn-sm { padding: 6px 12px; font-size: .85rem; }
.btn-block { width: 100%; }
.btn .spinner { width: 16px; height: 16px; border-width: 2px; border-color: rgba(255, 255, 255, .4); border-top-color: #fff; }

.icon-btn {
    border: 0; background: transparent; color: var(--text); cursor: pointer;
    width: 38px; height: 38px; border-radius: 10px; display: inline-flex; align-items: center; justify-content: center;
}
.icon-btn:hover { background: var(--border); }

.save-bar {
    position: sticky; bottom: 0; z-index: 10;
    display: flex; justify-content: flex-end;
    padding: 14px 0;
    background: linear-gradient(to top, var(--bg) 70%, rgba(244, 247, 249, 0));
}

/* Spinner + loading */
.spinner {
    display: inline-block; width: 28px; height: 28px; border-radius: 50%;
    border: 3px solid var(--accent-soft); border-top-color: var(--accent);
    animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.loading-overlay {
    position: absolute; inset: 0; z-index: 15;
    display: none; align-items: flex-start; justify-content: center; padding-top: 80px;
    background: rgba(244, 247, 249, .7); border-radius: var(--radius);
}
.is-loading > .loading-overlay { display: flex; }

.skeleton-text {
    display: inline-block; width: 64px; height: 1em; border-radius: 6px;
    background: linear-gradient(90deg, #eef2f5 25%, #e2e8ed 50%, #eef2f5 75%);
    background-size: 200% 100%; animation: shimmer 1.2s infinite;
}
@keyframes shimmer { from { background-position: 200% 0; } to { background-position: -200% 0; } }

/* Alerts */
.alert { padding: 10px 14px; border-radius: var(--radius-sm); font-size: .9rem; margin-bottom: 16px; }
.alert-success { background: var(--accent-soft); color: var(--accent-dark); }

/* Toasts */
.toast-stack { position: fixed; top: 18px; right: 18px; z-index: 100; display: flex; flex-direction: column; gap: 10px; max-width: calc(100vw - 36px); }
.toast {
    min-width: 260px; max-width: 380px;
    padding: 12px 16px; border-radius: var(--radius-sm);
    background: #fff; box-shadow: var(--shadow); border-left: 4px solid var(--accent);
    font-size: .92rem; animation: toast-in .2s ease;
}
.toast-error { border-left-color: var(--danger); }
.toast.hide { opacity: 0; transform: translateX(16px); transition: opacity .2s, transform .2s; }
@keyframes toast-in { from { opacity: 0; transform: translateX(16px); } to { opacity: 1; transform: none; } }

/* Ad unit rows */
.unit-row { display: grid; grid-template-columns: 120px 44px minmax(0, 1fr); align-items: center; gap: 12px; padding: 8px 0; }
.unit-row + .unit-row { border-top: 1px solid var(--border); }
.unit-label { font-weight: 550; }
.unit-row .field-error { grid-column: 3; }

/* Organic / Marketing */
.source-bar { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.segmented { display: inline-flex; border: 1px solid var(--border); border-radius: 99px; padding: 3px; background: var(--bg); }
.segmented-btn {
    border: 0; background: transparent; color: var(--muted); font: inherit; font-size: .9rem; font-weight: 600;
    padding: 7px 20px; border-radius: 99px; cursor: pointer;
}
.segmented-btn.is-active { background: var(--accent); color: #fff; }
.segmented-btn.has-error:not(.is-active) { color: var(--danger); }
.source-badge {
    display: inline-block; vertical-align: middle; margin-left: 8px; padding: 2px 10px; border-radius: 99px;
    font-size: .72rem; font-weight: 600; letter-spacing: .02em;
}
.source-organic { background: var(--accent-soft); color: var(--accent-dark); }
.source-marketing { background: #fff3e0; color: #b45309; }
[hidden] { display: none !important; }

/* Priority */
.priority-legend { list-style: none; padding: 0; margin: 0 0 16px; display: flex; flex-wrap: wrap; gap: 18px; font-size: .9rem; font-weight: 550; }
.priority-legend li { display: flex; align-items: center; gap: 8px; }
.priority-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 14px 16px; }

/* ADX */
.adx-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }
.adx-group { border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 16px; }
.adx-group-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; }
.adx-group-head h3 { font-size: .95rem; }
.adx-row { display: grid; grid-template-columns: minmax(0, 1fr) 90px auto; gap: 8px; align-items: start; margin-bottom: 8px; }
.adx-row .share { grid-column: 1 / -1; font-size: .75rem; color: var(--muted); margin-top: -4px; }
.adx-empty { color: var(--muted); font-size: .85rem; margin-bottom: 8px; }

/* Sequence chips */
.chip-palette { display: flex; flex-wrap: wrap; gap: 10px; margin-bottom: 14px; }
.chip {
    display: inline-flex; align-items: center; gap: 8px;
    border: 1px solid var(--border); background: #fff; color: var(--text);
    border-radius: 99px; padding: 7px 14px; font: inherit; font-size: .88rem; font-weight: 550; cursor: pointer;
}
.chip:hover { border-color: var(--accent); color: var(--accent-dark); }
.chip-num {
    width: 22px; height: 22px; border-radius: 50%;
    display: inline-flex; align-items: center; justify-content: center;
    background: var(--accent-soft); color: var(--accent-dark); font-size: .75rem;
}
.sequence {
    display: flex; flex-wrap: wrap; gap: 8px;
    min-height: 54px; padding: 10px; margin-bottom: 12px;
    border: 1px dashed var(--border); border-radius: var(--radius-sm); background: var(--bg);
}
.sequence-empty { color: var(--muted); font-size: .88rem; align-self: center; }
.chip-active { background: var(--accent); border-color: var(--accent); color: #fff; }
.chip-active .chip-num { background: rgba(255, 255, 255, .25); color: #fff; }
.chip-active:hover { background: var(--danger); border-color: var(--danger); color: #fff; }

/* Tables */
.table-wrap { overflow-x: auto; }
.table { width: 100%; border-collapse: collapse; font-size: .92rem; }
.table th {
    text-align: left; font-weight: 600; color: var(--muted); font-size: .8rem; text-transform: uppercase; letter-spacing: .03em;
    padding: 10px 12px; border-bottom: 1px solid var(--border); white-space: nowrap;
}
.table td { padding: 12px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.table tbody tr:last-child td { border-bottom: 0; }
.screen-table td:not(:first-child), .screen-table th:not(:first-child) { text-align: center; }
.screen-key { display: block; font-size: .78rem; color: var(--muted); font-family: ui-monospace, Menlo, monospace; }
.mono { font-family: ui-monospace, Menlo, monospace; font-size: .85rem; word-break: break-all; }
.badge { display: inline-block; padding: 3px 10px; border-radius: 99px; font-size: .78rem; font-weight: 600; }
.badge-organic { background: var(--accent-soft); color: var(--accent-dark); }
.badge-marketing { background: var(--amber-soft); color: #b06a12; }
.table-empty { text-align: center; color: var(--muted); padding: 28px !important; }

.add-screen { display: grid; grid-template-columns: 1fr 1fr auto; gap: 12px; align-items: end; margin-top: 16px; padding-top: 16px; border-top: 1px solid var(--border); }

.toolbar { display: flex; gap: 12px; margin-bottom: 16px; }
.toolbar input { flex: 1; }
.toolbar select { width: 180px; }
.pagination { display: flex; align-items: center; justify-content: flex-end; gap: 12px; margin-top: 16px; }

/* Login */
.login-body { display: flex; min-height: 100vh; align-items: center; justify-content: center; background: linear-gradient(135deg, #e3f5f2 0%, #f4f7f9 60%); }
.login-wrap { width: 100%; max-width: 400px; padding: 20px; }
.login-card { padding: 32px; display: flex; flex-direction: column; gap: 16px; }
.login-brand { text-align: center; display: flex; flex-direction: column; align-items: center; gap: 8px; margin-bottom: 6px; }
.login-brand h1 { font-size: 1.35rem; }

/* Responsive */
@media (max-width: 1024px) {
    .adx-grid { grid-template-columns: 1fr; }
}

@media (max-width: 900px) {
    .sidebar { transform: translateX(-100%); box-shadow: none; }
    .sidebar-open .sidebar { transform: none; box-shadow: 0 0 40px rgba(0, 0, 0, .15); }
    .sidebar-open .sidebar-backdrop { display: block; position: fixed; inset: 0; background: rgba(16, 24, 40, .35); z-index: 30; }
    .main { margin-left: 0; }
    .menu-btn { display: inline-flex; }
    .topbar { padding: 12px 16px; }
    .topbar-email { display: none; }
    .content { padding: 18px 16px 32px; }
    .grid-2 { grid-template-columns: 1fr; }
}

@media (max-width: 720px) {
    .stat-grid { grid-template-columns: 1fr; gap: 12px; }
    .stat-grid-compact { grid-template-columns: repeat(3, minmax(0, 1fr)); }
    .stat-grid-compact .stat-card { padding: 14px; }
    .form-grid { grid-template-columns: 1fr; }
    .priority-grid { grid-template-columns: 1fr; }
    .add-screen { grid-template-columns: 1fr; }
    .unit-row { grid-template-columns: 1fr 44px; }
    .unit-row input, .unit-row .field-error { grid-column: 1 / -1; }
    .toolbar { flex-direction: column; }
    .toolbar select { width: 100%; }
    .card { padding: 18px; }

    /* Tables become stacked cards */
    .screen-table thead, .responsive-table thead { display: none; }
    .screen-table tr, .responsive-table tr {
        display: block; border: 1px solid var(--border); border-radius: var(--radius-sm);
        padding: 6px 12px; margin-bottom: 12px;
    }
    .screen-table td, .responsive-table td {
        display: flex; justify-content: space-between; align-items: center; gap: 12px;
        text-align: right !important; padding: 8px 0; border-bottom: 1px solid var(--border);
    }
    .screen-table td:last-child, .responsive-table td:last-child { border-bottom: 0; }
    .screen-table td::before, .responsive-table td::before {
        content: attr(data-label); font-weight: 600; color: var(--muted); font-size: .82rem; text-align: left;
    }
    .screen-table td:first-child { text-align: left !important; }
    .table-wrap { overflow-x: visible; }
}
