:root {
    --primary: #0057A8;
    --primary-hover: #004785;
    --primary-muted: #E8F2FC;
    --accent: #0E7490;
    --bg: #EEF4FB;
    --card: #FFFFFF;
    --text: #1B2838;
    --muted: #4A5D73;
    --faint: #6B7C93;
    --border: #D8E2EE;
    --success: #047857;
    --danger: #C81E1E;
    --warning: #B45309;
    --shadow: 0 10px 28px rgba(27, 40, 56, 0.08);
    --radius: 18px;
    --header-h: 64px;
    --safe-top: env(safe-area-inset-top, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
    --safe-left: env(safe-area-inset-left, 0px);
    --safe-right: env(safe-area-inset-right, 0px);
    --frame: #2563eb;
    --shutter: #ea580c;
    --glass: #0891b2;
    --extra: #7c3aed;
    --door: #db2777;
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body {
    font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
    background: var(--bg);
    color: var(--text);
    -webkit-tap-highlight-color: transparent;
}

button, input, select { font: inherit; }
button { cursor: pointer; }
[hidden] { display: none !important; }

.login-screen {
    min-height: 100dvh;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: calc(24px + var(--safe-top)) 20px calc(24px + var(--safe-bottom));
    background:
        radial-gradient(circle at 12% 18%, #7dd3fc 0%, transparent 42%),
        radial-gradient(circle at 88% 8%, #c4b5fd 0%, transparent 38%),
        radial-gradient(circle at 70% 90%, #86efac 0%, transparent 40%),
        linear-gradient(180deg, #dbeafe 0%, #eef4fb 55%);
}
.login-card {
    width: min(440px, 100%);
    background: var(--card);
    border: 1px solid rgba(255,255,255,0.8);
    border-radius: 22px;
    padding: 28px 24px 22px;
    box-shadow: var(--shadow);
}
.login-logo { width: 72px; height: auto; display: block; margin: 0 auto 12px; }
.login-card h1 { margin: 0 0 6px; text-align: center; font-size: 1.45rem; }
.login-sub, .login-hint { color: var(--muted); text-align: center; margin: 0 0 18px; font-size: 0.92rem; }
.login-hint { margin: 14px 0 8px; font-size: 0.82rem; }
.login-card label { display: block; font-size: 0.82rem; font-weight: 600; margin: 10px 0 6px; }
.login-card input {
    width: 100%;
    padding: 12px 14px;
    border: 1px solid var(--border);
    border-radius: 12px;
    background: #fff;
}
.login-web-link {
    display: block;
    text-align: center;
    margin-top: 10px;
    color: var(--primary);
    font-size: 0.88rem;
}

.app-shell { min-height: 100dvh; display: flex; flex-direction: column; }
.app-header {
    min-height: calc(var(--header-h) + var(--safe-top));
    padding: var(--safe-top) max(12px, var(--safe-right)) 10px max(12px, var(--safe-left));
    display: flex;
    align-items: center;
    gap: 8px;
    background: linear-gradient(135deg, #0057A8 0%, #0E7490 58%, #2563eb 100%);
    color: #fff;
    position: sticky;
    top: 0;
    z-index: 20;
}
.header-titles { flex: 1; min-width: 0; }
.header-kicker { margin: 0; font-size: 0.7rem; color: rgba(255,255,255,0.78); text-transform: uppercase; letter-spacing: 0.04em; }
.header-titles h1 { margin: 0; font-size: 1.15rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.header-actions { display: flex; align-items: center; gap: 6px; }

.icon-btn, .avatar-btn {
    width: 42px;
    height: 42px;
    border: 1px solid rgba(255,255,255,0.28);
    background: rgba(255,255,255,0.14);
    border-radius: 12px;
    display: grid;
    place-items: center;
    color: #fff;
}
.avatar-btn {
    background: #fff;
    color: var(--primary);
    border-color: #fff;
    font-weight: 800;
    font-size: 0.85rem;
}
.sync-pill {
    font-size: 0.72rem;
    font-weight: 700;
    padding: 4px 8px;
    border-radius: 999px;
    background: #d1fae5;
    color: var(--success);
    max-width: 92px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.sync-pill.is-busy { background: #fef3c7; color: #92400e; }
.sync-pill.is-err { background: #fee2e2; color: var(--danger); }

.workspace {
    flex: 1;
    display: flex;
    min-height: 0;
}
.pane { display: flex; flex-direction: column; min-width: 0; min-height: 0; flex: 1; }
.pane-toolbar {
    padding: 12px 14px 8px;
    background: transparent;
}
.pane-toolbar-wrap { display: flex; flex-direction: column; gap: 8px; }
.search-input, .filter-select {
    width: 100%;
    padding: 12px 14px;
    border: 1px solid var(--border);
    border-radius: 12px;
    background: #fff;
    box-shadow: 0 1px 0 rgba(255,255,255,0.8);
}
.filter-row { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.pane-scroll {
    flex: 1;
    overflow: auto;
    padding: 4px 14px calc(20px + var(--safe-bottom));
    -webkit-overflow-scrolling: touch;
}

.card {
    background: #fff;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 14px 14px 14px 16px;
    margin-bottom: 10px;
    text-align: left;
    width: 100%;
    color: inherit;
    box-shadow: 0 4px 14px rgba(27, 40, 56, 0.05);
    position: relative;
    overflow: hidden;
}
.card::before {
    content: "";
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    width: 6px;
    background: linear-gradient(180deg, #0057A8, #22c55e);
}
.card.is-selected { border-color: #93c5fd; box-shadow: 0 0 0 3px #dbeafe; }
.card-title { margin: 0 0 4px; font-size: 1.05rem; }
.card-meta, .muted { color: var(--muted); font-size: 0.86rem; margin: 0; }
.progress-bar {
    margin-top: 10px;
    height: 8px;
    background: #e8eef5;
    border-radius: 99px;
    overflow: hidden;
}
.progress-bar > span {
    display: block;
    height: 100%;
    background: linear-gradient(90deg, #22c55e, #0057A8);
    border-radius: inherit;
}
.unit-code { font-weight: 800; letter-spacing: 0.02em; }
.chip-row { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.chip {
    font-size: 0.74rem;
    font-weight: 700;
    padding: 3px 8px;
    border-radius: 999px;
    background: #dbeafe;
    color: #1d4ed8;
}

.empty {
    text-align: center;
    color: var(--muted);
    padding: 36px 16px;
}
.empty strong { display: block; color: var(--text); margin-bottom: 6px; }

.unit-hero {
    background: linear-gradient(135deg, #0f3d73 0%, #0057A8 48%, #0ea5e9 100%);
    color: #fff;
    border: 0;
    border-radius: 20px;
    padding: 18px 16px 16px;
    margin-bottom: 14px;
    box-shadow: 0 12px 24px rgba(0, 87, 168, 0.22);
}
.unit-hero h2 { margin: 0 0 6px; font-size: 1.7rem; color: #fff; }
.unit-hero .card-meta { color: rgba(255,255,255,0.88); }
.hero-stats {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
    margin-top: 12px;
}
.hero-stat {
    background: rgba(255,255,255,0.16);
    border: 1px solid rgba(255,255,255,0.22);
    border-radius: 12px;
    padding: 8px 10px;
    min-width: 72px;
}
.hero-stat b { display: block; font-size: 1.1rem; }
.hero-stat span { font-size: 0.72rem; opacity: 0.86; }
.hero-date {
    margin-top: 12px;
}
.hero-date label { display: block; font-size: 0.75rem; font-weight: 700; margin-bottom: 4px; color: rgba(255,255,255,0.86); }
.hero-date input[type="date"] {
    width: 100%;
    padding: 10px 12px;
    border: 0;
    border-radius: 10px;
    background: #fff;
    color: var(--text);
}

.comp-card {
    background: #fff;
    border: 1px solid var(--border);
    border-radius: 18px;
    padding: 13px 13px 12px;
    margin-bottom: 12px;
    box-shadow: 0 6px 16px rgba(27, 40, 56, 0.05);
    border-left: 6px solid var(--frame);
}
.comp-card[data-kind="frame"] { border-left-color: var(--frame); background: linear-gradient(180deg, #eff6ff 0%, #fff 42%); }
.comp-card[data-kind="shutter"] { border-left-color: var(--shutter); background: linear-gradient(180deg, #fff7ed 0%, #fff 42%); }
.comp-card[data-kind="glass"] { border-left-color: var(--glass); background: linear-gradient(180deg, #ecfeff 0%, #fff 42%); }
.comp-card[data-kind="door"] { border-left-color: var(--door); background: linear-gradient(180deg, #fdf2f8 0%, #fff 42%); }
.comp-card[data-kind="extra"] { border-left-color: var(--extra); background: linear-gradient(180deg, #f5f3ff 0%, #fff 42%); }
.comp-card.is-selected { box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.16); }
.comp-head { display: flex; justify-content: space-between; gap: 8px; align-items: flex-start; }
.comp-head h3 { margin: 0; font-size: 1.05rem; }
.comp-kind {
    font-size: 0.68rem;
    font-weight: 800;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    padding: 3px 8px;
    border-radius: 999px;
    color: #fff;
}
.comp-card[data-kind="frame"] .comp-kind { background: var(--frame); }
.comp-card[data-kind="shutter"] .comp-kind { background: var(--shutter); }
.comp-card[data-kind="glass"] .comp-kind { background: var(--glass); }
.comp-card[data-kind="door"] .comp-kind { background: var(--door); }
.comp-card[data-kind="extra"] .comp-kind { background: var(--extra); }
.comp-progress {
    margin: 8px 0 2px;
    font-size: 0.8rem;
    font-weight: 700;
    color: var(--muted);
}
.comp-stages { display: flex; flex-wrap: wrap; gap: 7px; margin-top: 10px; }
.stage-pill {
    border: 1px solid transparent;
    background: #fff;
    border-radius: 999px;
    padding: 7px 11px;
    font-size: 0.76rem;
    font-weight: 700;
    color: var(--text);
    box-shadow: 0 1px 0 rgba(27,40,56,0.04);
}
.stage-pill.is-todo { opacity: 0.92; }
.stage-pill.is-done { color: #fff; }
.stage-pill.is-locked { opacity: 0.55; }
.comp-actions {
    display: grid;
    grid-template-columns: 1fr 88px;
    gap: 8px;
    margin-top: 12px;
}
.btn-update {
    background: linear-gradient(180deg, #16a34a 0%, #15803d 100%);
    color: #fff;
    border: 0;
    border-radius: 12px;
    padding: 12px 14px;
    font-weight: 800;
    letter-spacing: 0.02em;
    box-shadow: 0 6px 14px rgba(22, 163, 74, 0.28);
}
.btn-update:disabled { opacity: 0.45; box-shadow: none; }
.btn-undo {
    background: #fff;
    color: var(--danger);
    border: 1px solid #fecaca;
    border-radius: 12px;
    padding: 12px 10px;
    font-weight: 800;
}
.btn-undo:disabled { opacity: 0.45; }
.comp-hint {
    margin: 8px 0 0;
    font-size: 0.75rem;
    color: var(--faint);
}

.btn-primary, .btn-secondary, .btn-ghost, .btn-danger {
    border: 0;
    border-radius: 12px;
    padding: 12px 14px;
    font-weight: 700;
    width: 100%;
}
.btn-primary { background: linear-gradient(180deg, #0066CC 0%, #0057A8 100%); color: #fff; }
.btn-primary:disabled { opacity: 0.5; }
.btn-secondary { background: #fff; color: var(--text); border: 1px solid var(--border); }
.btn-ghost { background: transparent; color: var(--muted); }
.btn-danger { background: #fef2f2; color: var(--danger); border: 1px solid #fecaca; }

.error { color: var(--danger); min-height: 1.2em; font-size: 0.88rem; }
.toast {
    position: fixed;
    left: 50%;
    bottom: calc(18px + var(--safe-bottom));
    transform: translateX(-50%);
    background: #065f46;
    color: #fff;
    padding: 11px 16px;
    border-radius: 999px;
    font-size: 0.88rem;
    font-weight: 700;
    z-index: 50;
    max-width: min(92vw, 480px);
    box-shadow: 0 8px 20px rgba(6, 95, 70, 0.25);
}

.sheet { position: fixed; inset: 0; z-index: 40; }
.sheet-overlay { position: absolute; inset: 0; background: rgba(27, 40, 56, 0.35); }
.sheet-card {
    position: absolute;
    left: 50%;
    bottom: 0;
    transform: translateX(-50%);
    width: min(480px, 100%);
    background: #fff;
    border-radius: 20px 20px 0 0;
    padding: 20px 20px calc(20px + var(--safe-bottom));
}
.sheet-card h2 { margin: 0 0 4px; }
.sheet-kicker { margin: 0; color: var(--faint); font-size: 0.75rem; text-transform: uppercase; }
.sheet-card .btn-secondary, .sheet-card .btn-ghost { margin-top: 10px; }

.view-only-banner {
    background: #fff7ed;
    color: var(--warning);
    border: 1px solid #fed7aa;
    border-radius: 12px;
    padding: 10px 12px;
    margin-bottom: 12px;
    font-size: 0.88rem;
}

@media (max-width: 899px) {
    .workspace { flex-direction: column; }
}

@media (min-width: 900px) {
    .workspace {
        display: grid;
        grid-template-columns: minmax(320px, 38%) 1fr;
        min-height: calc(100dvh - var(--header-h) - var(--safe-top));
    }
    .pane-projects, .pane-units { border-right: 1px solid var(--border); background: #f3f8ff; }
    .pane-detail { background: linear-gradient(180deg, #eef6ff 0%, #f8fafc 30%); }
    .pane-detail .pane-scroll { padding: 16px 22px calc(24px + var(--safe-bottom)); }
}

@media (min-width: 1180px) {
    .workspace.is-project-open {
        grid-template-columns: minmax(260px, 22%) minmax(320px, 30%) 1fr;
    }
    .workspace.is-project-open .pane-projects,
    .workspace.is-project-open .pane-units { display: flex !important; }
}
