/* ===== Reset & Variables ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
    --bg-primary: #0f1117;
    --bg-secondary: #1a1d27;
    --bg-card: #1e2130;
    --bg-hover: #262a3a;
    --border: #2a2e3f;
    --text-primary: #e8eaf0;
    --text-secondary: #8b8fa3;
    --text-muted: #5c6078;
    --accent: #6c5ce7;
    --accent-hover: #7f71ef;
    --green: #00d68f;
    --green-bg: rgba(0,214,143,.12);
    --red: #ff6b6b;
    --red-bg: rgba(255,107,107,.12);
    --orange: #ffa94d;
    --blue: #4dabf7;
    --purple: #9775fa;
    --sidebar-w: 250px;
    --radius: 10px;
    --radius-sm: 6px;
    --shadow: 0 2px 12px rgba(0,0,0,.3);
}

html { font-size: 14px; }
body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Noto Sans JP', sans-serif;
    background: var(--bg-primary);
    color: var(--text-primary);
    line-height: 1.5;
    overflow-x: hidden;
}

/* ===== Layout ===== */
#app {
    display: flex;
    min-height: 100vh;
}

#sidebar {
    position: fixed;
    top: 0; left: 0;
    width: var(--sidebar-w);
    height: 100vh;
    background: var(--bg-secondary);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    z-index: 100;
    overflow-y: auto;
}
.sidebar-header {
    padding: 24px 20px 16px;
    border-bottom: 1px solid var(--border);
}
.sidebar-header h1 {
    font-size: 1.25rem;
    font-weight: 700;
    color: var(--accent);
}
.sidebar-sub {
    font-size: .8rem;
    color: var(--text-muted);
}
.nav-links {
    list-style: none;
    padding: 12px 0;
    flex: 1;
}
.nav-link {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 20px;
    color: var(--text-secondary);
    text-decoration: none;
    font-size: .92rem;
    transition: all .15s;
    border-left: 3px solid transparent;
}
.nav-link:hover {
    color: var(--text-primary);
    background: var(--bg-hover);
}
.nav-link.active {
    color: var(--accent);
    background: rgba(108,92,231,.08);
    border-left-color: var(--accent);
    font-weight: 600;
}
.nav-icon { font-size: 1.1rem; }
.nav-section {
    font-size: .7rem;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: var(--text-muted);
    padding: 16px 20px 4px;
    font-weight: 700;
}
.sidebar-footer {
    padding: 16px 20px;
    border-top: 1px solid var(--border);
}

#main-content {
    margin-left: var(--sidebar-w);
    flex: 1;
    padding: 28px 32px;
    min-width: 0;
}

/* ===== Page Header ===== */
.page-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 24px;
}
.page-header h2 {
    font-size: 1.5rem;
    font-weight: 700;
}
.header-meta {
    color: var(--text-muted);
    font-size: .9rem;
}
.header-actions {
    display: flex;
    gap: 8px;
}

/* ===== Cards ===== */
.card {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 20px;
    margin-bottom: 20px;
}
.card h3 {
    font-size: 1rem;
    font-weight: 600;
    margin-bottom: 16px;
    color: var(--text-secondary);
}

.summary-cards {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
    gap: 16px;
    margin-bottom: 24px;
}
.summary-card {
    position: relative;
    overflow: hidden;
}
.summary-card::before {
    content: '';
    position: absolute;
    top: 0; left: 0;
    width: 100%;
    height: 3px;
}
.summary-card.revenue::before { background: var(--green); }
.summary-card.expense::before { background: var(--red); }
.summary-card.profit::before { background: var(--accent); }
.summary-card.margin::before { background: var(--orange); }
.card-label {
    font-size: .8rem;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: .5px;
    margin-bottom: 8px;
}
.card-value {
    font-size: 1.6rem;
    font-weight: 700;
    font-variant-numeric: tabular-nums;
}
.card-delta {
    font-size: .82rem;
    margin-top: 6px;
}
.card-delta.positive { color: var(--green); }
.card-delta.negative { color: var(--red); }
.card-sub {
    font-size: .82rem;
    color: var(--text-muted);
    margin-top: 6px;
}

/* ===== Charts ===== */
.chart-row {
    display: grid;
    grid-template-columns: 2fr 1fr;
    gap: 20px;
    margin-bottom: 20px;
}
.chart-card { min-height: 320px; }
.chart-card canvas { max-height: 280px; }

/* ===== Sub-business grid ===== */
.sub-business-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 16px;
    margin-bottom: 20px;
}
.sub-stats > div {
    display: flex;
    justify-content: space-between;
    padding: 8px 0;
    border-bottom: 1px solid var(--border);
}
.stat-label { color: var(--text-muted); font-size: .85rem; }
.stat-value { font-weight: 600; font-variant-numeric: tabular-nums; }

/* ===== BQ Status ===== */
.bq-status {
    margin-bottom: 16px;
    font-size: .85rem;
    min-height: 0;
}
.bq-status.loading { color: var(--orange); }
.bq-status.success { color: var(--green); }
.bq-status.error { color: var(--red); }

/* ===== Funnel ===== */
#funnel-container {
    display: flex;
    flex-direction: column;
    gap: 10px;
}
.funnel-stage {
    display: flex;
    align-items: center;
    gap: 16px;
}
.funnel-bar {
    height: 42px;
    background: var(--accent);
    border-radius: var(--radius-sm);
    display: flex;
    align-items: center;
    padding: 0 16px;
    font-weight: 600;
    font-size: .9rem;
    min-width: 80px;
    transition: width .4s ease;
}
.funnel-meta {
    display: flex;
    gap: 12px;
    font-size: .9rem;
    white-space: nowrap;
}
.funnel-count { font-weight: 600; }
.funnel-rate { color: var(--text-muted); }

/* Funnel gradient */
.funnel-stage:nth-child(1) .funnel-bar { background: #6c5ce7; }
.funnel-stage:nth-child(2) .funnel-bar { background: #a29bfe; }
.funnel-stage:nth-child(3) .funnel-bar { background: #74b9ff; }
.funnel-stage:nth-child(4) .funnel-bar { background: #55efc4; }
.funnel-stage:nth-child(5) .funnel-bar { background: #00d68f; }

/* Tier dots */
.dot {
    display: inline-block;
    width: 10px; height: 10px;
    border-radius: 50%;
    margin-right: 6px;
}
.dot.tier1 { background: #ffa94d; }
.dot.tier2 { background: #9775fa; }
.dot.tier3 { background: #ff6b6b; }
.tier-legend {
    display: flex;
    flex-direction: column;
    gap: 6px;
    font-size: .85rem;
    margin-top: 12px;
}

/* ===== Tables ===== */
.table-wrapper { overflow-x: auto; }
table {
    width: 100%;
    border-collapse: collapse;
    font-size: .88rem;
}
th, td {
    padding: 10px 14px;
    text-align: right;
    border-bottom: 1px solid var(--border);
    white-space: nowrap;
}
th {
    color: var(--text-muted);
    font-weight: 600;
    text-transform: uppercase;
    font-size: .75rem;
    letter-spacing: .5px;
    background: var(--bg-secondary);
    position: sticky;
    top: 0;
}
th:first-child, td:first-child { text-align: left; }
tr:hover td { background: var(--bg-hover); }
.text-green { color: var(--green); }
.text-red { color: var(--red); }

/* ===== Buttons ===== */
.btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 8px 18px;
    border: none;
    border-radius: var(--radius-sm);
    font-size: .88rem;
    font-weight: 600;
    cursor: pointer;
    transition: all .15s;
}
.btn-primary {
    background: var(--accent);
    color: #fff;
}
.btn-primary:hover { background: var(--accent-hover); }
.btn-accent {
    background: linear-gradient(135deg, #4dabf7, #6c5ce7);
    color: #fff;
    border: none;
}
.btn-accent:hover { opacity: .85; }
.btn-secondary {
    background: var(--bg-hover);
    color: var(--text-primary);
    border: 1px solid var(--border);
}
.btn-secondary:hover { background: var(--border); }
.btn-danger {
    background: transparent;
    color: var(--red);
    border: 1px solid var(--red);
    font-size: .78rem;
    padding: 6px 12px;
}
.btn-danger:hover { background: var(--red-bg); }
.btn-sm { font-size: .78rem; padding: 5px 10px; }
.btn-icon {
    background: none;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    padding: 4px;
    font-size: 1rem;
}
.btn-icon:hover { color: var(--red); }
.btn:disabled {
    opacity: .5;
    cursor: not-allowed;
}

/* ===== Modal ===== */
.modal {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,.6);
    z-index: 200;
    align-items: center;
    justify-content: center;
}
.modal.open { display: flex; }
.modal-content {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 28px;
    width: 100%;
    max-width: 480px;
    box-shadow: var(--shadow);
    max-height: 90vh;
    overflow-y: auto;
}
.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
}
.modal-header h3 { font-size: 1.1rem; }
.modal-close {
    background: none;
    border: none;
    color: var(--text-muted);
    font-size: 1.5rem;
    cursor: pointer;
    line-height: 1;
}
.modal-close:hover { color: var(--text-primary); }

/* ===== Forms ===== */
.form-row {
    margin-bottom: 14px;
}
.form-row label {
    display: block;
    font-size: .82rem;
    color: var(--text-muted);
    margin-bottom: 4px;
    font-weight: 600;
}
.form-row input,
.form-row select {
    width: 100%;
    padding: 9px 12px;
    background: var(--bg-primary);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    color: var(--text-primary);
    font-size: .9rem;
    outline: none;
    transition: border .15s;
}
.form-row input:focus,
.form-row select:focus {
    border-color: var(--accent);
}
.form-actions {
    display: flex;
    justify-content: flex-end;
    gap: 10px;
    margin-top: 20px;
}

/* ===== Tabs ===== */
.tab-content { display: none; }
.tab-content.active { display: block; }

/* ===== Toast ===== */
#toast-container {
    position: fixed;
    bottom: 24px;
    right: 24px;
    z-index: 300;
    display: flex;
    flex-direction: column;
    gap: 8px;
}
.toast {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-left: 4px solid var(--green);
    padding: 12px 20px;
    border-radius: var(--radius-sm);
    font-size: .88rem;
    box-shadow: var(--shadow);
    animation: toastIn .3s ease;
}
.toast.error { border-left-color: var(--red); }
@keyframes toastIn {
    from { opacity: 0; transform: translateY(12px); }
    to { opacity: 1; transform: translateY(0); }
}

/* ===== Scrollbar ===== */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: var(--bg-primary); }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-muted); }

/* ===== Responsive ===== */
@media (max-width: 1024px) {
    .chart-row { grid-template-columns: 1fr; }
    .sub-business-grid { grid-template-columns: 1fr; }
}
@media (max-width: 768px) {
    #sidebar { width: 60px; }
    #sidebar .sidebar-header h1,
    #sidebar .sidebar-sub,
    #sidebar .nav-link span:not(.nav-icon),
    #sidebar .sidebar-footer,
    #sidebar .nav-section { display: none; }
    .nav-link { justify-content: center; padding: 12px; }
    #main-content { margin-left: 60px; padding: 16px; }
    .summary-cards { grid-template-columns: repeat(2, 1fr); }
}
