:root {
    --bg: #f7f7f5;
    --surface: #ffffff;
    --border: #e5e3de;
    --text: #1f1e1c;
    --text-muted: #6b6a66;
    --accent: #8a5a3a;
    --accent-contrast: #ffffff;
    --red: #c4453a;
    --red-bg: #fbe9e7;
    --yellow: #b8860b;
    --yellow-bg: #fbf3dc;
    --green: #3f7a4f;
    --green-bg: #e6f2e8;
    --gray: #9a9892;
    --gray-bg: #eeede9;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.04), 0 1px 8px rgba(0, 0, 0, 0.03);
    --radius: 10px;
}

@media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) {
        --bg: #171614;
        --surface: #211f1c;
        --border: #35322c;
        --text: #ece9e3;
        --text-muted: #9a978f;
        --accent: #d99a6c;
        --accent-contrast: #171614;
        --red: #e57468;
        --red-bg: #3a201d;
        --yellow: #e0b34d;
        --yellow-bg: #3a331a;
        --green: #7cbf8e;
        --green-bg: #1e2f22;
        --gray: #7a776f;
        --gray-bg: #2a2824;
        --shadow: 0 1px 2px rgba(0, 0, 0, 0.2), 0 1px 8px rgba(0, 0, 0, 0.2);
    }
}

:root[data-theme="dark"] {
    --bg: #171614;
    --surface: #211f1c;
    --border: #35322c;
    --text: #ece9e3;
    --text-muted: #9a978f;
    --accent: #d99a6c;
    --accent-contrast: #171614;
    --red: #e57468;
    --red-bg: #3a201d;
    --yellow: #e0b34d;
    --yellow-bg: #3a331a;
    --green: #7cbf8e;
    --green-bg: #1e2f22;
    --gray: #7a776f;
    --gray-bg: #2a2824;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.2), 0 1px 8px rgba(0, 0, 0, 0.2);
}

* { box-sizing: border-box; }

body {
    margin: 0;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
    font-size: 14.5px;
    line-height: 1.5;
}

a { color: inherit; }

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

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

.brand {
    font-weight: 600;
    font-size: 16px;
    padding: 0 10px 18px;
    letter-spacing: -0.01em;
}
.brand span { color: var(--accent); }

.nav a {
    display: block;
    padding: 9px 10px;
    border-radius: 8px;
    text-decoration: none;
    color: var(--text-muted);
    font-size: 13.5px;
    margin-bottom: 2px;
}
.nav a:hover { background: var(--bg); color: var(--text); }
.nav a.active { background: var(--accent); color: var(--accent-contrast); font-weight: 600; }

.nav .section-gap { height: 14px; }

.sidebar-footer {
    margin-top: auto;
    padding: 10px;
    display: flex;
    align-items: center;
    gap: 10px;
}
.sidebar-footer form { margin: 0; }
.sidebar-footer form button {
    background: none;
    border: none;
    color: var(--text-muted);
    font-size: 12.5px;
    cursor: pointer;
    padding: 4px 0;
}
.sidebar-footer form button:hover { color: var(--text); }

.theme-toggle {
    background: none;
    border: 1px solid var(--border);
    border-radius: 8px;
    width: 30px;
    height: 30px;
    flex-shrink: 0;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    color: var(--text-muted);
    padding: 0;
}
.theme-toggle:hover { color: var(--text); border-color: var(--text-muted); }
.theme-toggle svg { width: 16px; height: 16px; display: none; }
:root[data-theme="light"] .theme-toggle .icon-moon { display: block; }
:root[data-theme="dark"] .theme-toggle .icon-sun { display: block; }

.login-card { position: relative; }
.login-card .theme-toggle { position: absolute; top: 14px; right: 14px; }

.main { flex: 1; padding: 28px 36px; max-width: 1280px; }

.page-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 22px;
}
.page-header h1 { font-size: 20px; margin: 0; letter-spacing: -0.01em; }
.page-header .sub { color: var(--text-muted); font-size: 13px; margin-top: 2px; }

.btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    background: var(--accent);
    color: var(--accent-contrast);
    border: none;
    padding: 8px 14px;
    border-radius: 8px;
    font-size: 13.5px;
    font-weight: 600;
    cursor: pointer;
    text-decoration: none;
}
.btn:hover { opacity: 0.9; }
.btn.secondary {
    background: transparent;
    color: var(--text);
    border: 1px solid var(--border);
}
.btn.small { padding: 4px 9px; font-size: 12.5px; }

.kpi-row {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
    gap: 12px;
    margin-bottom: 24px;
}
.kpi {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 14px 16px;
    box-shadow: var(--shadow);
}
.kpi .value { font-size: 24px; font-weight: 700; letter-spacing: -0.02em; }
.kpi .label { font-size: 12px; color: var(--text-muted); margin-top: 2px; }
.kpi.warn .value { color: var(--red); }

.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 18px 20px;
    margin-bottom: 20px;
    box-shadow: var(--shadow);
}
.card h2 { font-size: 14.5px; margin: 0 0 12px; }

.grid-2 {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 20px;
}
@media (max-width: 980px) { .grid-2 { grid-template-columns: 1fr; } }

table { width: 100%; border-collapse: collapse; }
th {
    text-align: left;
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 0.03em;
    color: var(--text-muted);
    font-weight: 600;
    padding: 6px 10px;
    border-bottom: 1px solid var(--border);
}
td {
    padding: 9px 10px;
    border-bottom: 1px solid var(--border);
    vertical-align: middle;
}
tr:last-child td { border-bottom: none; }
tr.editing-row td { background: var(--bg); }

.dot {
    display: inline-block;
    width: 9px;
    height: 9px;
    border-radius: 50%;
    flex-shrink: 0;
}
.dot.red { background: var(--red); }
.dot.yellow { background: var(--yellow); }
.dot.green { background: var(--green); }
.dot.gray { background: var(--gray); }

.pill {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 3px 9px;
    border-radius: 999px;
    font-size: 12px;
    font-weight: 600;
    white-space: nowrap;
}
.pill.red { background: var(--red-bg); color: var(--red); }
.pill.yellow { background: var(--yellow-bg); color: var(--yellow); }
.pill.green { background: var(--green-bg); color: var(--green); }
.pill.gray { background: var(--gray-bg); color: var(--gray); }

.muted { color: var(--text-muted); }
.overdue { color: var(--red); font-weight: 600; }

select, input[type=text], input[type=date], input[type=number], input[type=email], textarea {
    font-family: inherit;
    font-size: 13px;
    padding: 6px 8px;
    border-radius: 6px;
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text);
}
select.inline, input.inline {
    border: 1px solid transparent;
    background: transparent;
    padding: 4px 6px;
    cursor: pointer;
}
select.inline:hover, input.inline:hover { border-color: var(--border); }
textarea { width: 100%; resize: vertical; min-height: 60px; }

.form-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 10px 14px;
}
.form-grid label {
    display: flex;
    flex-direction: column;
    gap: 4px;
    font-size: 12px;
    color: var(--text-muted);
}
.form-grid label.full { grid-column: 1 / -1; }
.form-grid label > input, .form-grid label > select, .form-grid label > textarea {
    width: 100%;
    min-width: 0;
}
.form-actions { display: flex; gap: 8px; margin-top: 12px; }

.edit-panel {
    background: var(--bg);
    border-radius: 8px;
    padding: 14px;
}

.new-form-toggle { margin-bottom: 14px; }
.new-form {
    display: none;
    margin-bottom: 14px;
}
.new-form.open { display: block; }

.stage-bar {
    display: flex;
    align-items: center;
    gap: 10px;
    font-size: 12.5px;
    padding: 5px 0;
}
.stage-bar .name { width: 190px; flex-shrink: 0; color: var(--text-muted); }
.stage-bar .bar-track { flex: 1; background: var(--bg); border-radius: 4px; height: 8px; overflow: hidden; }
.stage-bar .bar-fill { background: var(--accent); height: 100%; }
.stage-bar .count { width: 90px; text-align: right; flex-shrink: 0; }

.focus-list { list-style: none; margin: 0; padding: 0; }
.focus-list li {
    display: flex;
    justify-content: space-between;
    gap: 10px;
    padding: 8px 0;
    border-bottom: 1px solid var(--border);
    font-size: 13px;
}
.focus-list li:last-child { border-bottom: none; }

.progress-row { margin-bottom: 12px; }
.progress-row .top { display: flex; justify-content: space-between; font-size: 12.5px; margin-bottom: 4px; }
.progress-track { background: var(--bg); border-radius: 5px; height: 9px; overflow: hidden; }
.progress-fill { background: var(--accent); height: 100%; }
.progress-fill.over { background: var(--green); }

.login-wrap {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    background: var(--bg);
}
.login-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 32px;
    width: 320px;
    box-shadow: var(--shadow);
}
.login-card h1 { font-size: 17px; margin: 0 0 4px; }
.login-card p.sub { color: var(--text-muted); font-size: 13px; margin: 0 0 18px; }
.login-card input { width: 100%; margin-bottom: 12px; }
.login-card .btn { width: 100%; justify-content: center; }
.error { color: var(--red); font-size: 12.5px; margin-bottom: 10px; }

.section-title { font-size: 12px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--text-muted); margin: 22px 0 8px; }

.table-scroll { overflow-x: auto; }

.rules-table td, .rules-table th { vertical-align: top; }
.checklist { padding-left: 18px; }
.checklist li { margin-bottom: 6px; }
.ladder-item { padding: 10px 0; border-bottom: 1px solid var(--border); }
.ladder-item:last-child { border-bottom: none; }
.ladder-item .name { font-weight: 600; }
.ladder-item .price { color: var(--accent); font-weight: 600; float: right; }

/* ---- responsive / mobile ---- */

.table-scroll table { min-width: 640px; }

@media (max-width: 720px) {
    body { font-size: 14px; }

    .layout { flex-direction: column; min-height: 0; }

    .sidebar {
        width: 100%;
        height: auto;
        position: sticky;
        top: 0;
        z-index: 20;
        flex-direction: row;
        align-items: center;
        gap: 4px;
        padding: 8px 10px;
        border-right: none;
        border-bottom: 1px solid var(--border);
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }
    .brand { padding: 0 8px 0 2px; flex-shrink: 0; font-size: 14px; }

    .nav {
        display: flex;
        flex-direction: row;
        gap: 2px;
        overflow-x: auto;
        flex: 1;
    }
    .nav a {
        white-space: nowrap;
        margin-bottom: 0;
        padding: 7px 10px;
    }
    .nav .section-gap { display: none; }

    .sidebar-footer {
        margin-top: 0;
        padding: 0 4px;
        flex-shrink: 0;
    }

    .main { padding: 16px; max-width: 100%; }

    .page-header { flex-wrap: wrap; gap: 10px; margin-bottom: 16px; }
    .page-header h1 { font-size: 18px; }

    .kpi-row { grid-template-columns: repeat(2, 1fr); gap: 8px; margin-bottom: 16px; }
    .kpi { padding: 10px 12px; }
    .kpi .value { font-size: 19px; }

    .card { padding: 14px; margin-bottom: 14px; }

    .form-grid { grid-template-columns: 1fr; }

    .stage-bar .name { width: 110px; font-size: 11.5px; }
    .stage-bar .count { width: 70px; font-size: 11.5px; }

    .table-scroll {
        margin: 0 -14px;
        padding: 0 14px;
        width: calc(100% + 28px);
    }

    .login-card { width: 100%; max-width: 320px; }
}
