/* ===== Roast Chart Theme Variables ===== */
:root {
    --chart-bg: #ffffff;
    --chart-text: #333333;
    --chart-text-muted: #888888;
    --chart-grid: #e5e5e5;
    --chart-card-bg: #f8f9fa;
    --chart-border: #dee2e6;
    --chart-card-shadow: 0 1px 3px rgba(0,0,0,0.06);
}

html[data-coffee-dark="true"] {
    --chart-bg: #1a1a2e;
    --chart-text: #e0e0e0;
    --chart-text-muted: #8888aa;
    --chart-grid: #333355;
    --chart-card-bg: #16213e;
    --chart-border: #2a2a4a;
    --chart-card-shadow: 0 1px 4px rgba(0,0,0,0.3);
}

/* ===== Page Layout ===== */
.roast-chart-page {
    padding: 0.5rem 0.75rem;
    display: flex;
    flex-direction: column;
    gap: 0.35rem;
    width: 100%;
    max-width: 100%;
    box-sizing: border-box;
    overflow: hidden;
    /* AppBar 64px + MudMainContent padding top ~16px + buffer 8px = 88px.
       Trước đây trừ 120px dẫn tới dưới trống ~32px. */
    height: calc(100vh - 88px);
}

/* Main body: chart left + info panel right */
.roast-chart-body {
    display: flex;
    gap: 0.5rem;
    flex: 1;
    min-height: 0;
    overflow: hidden;
}

.roast-chart-main {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 0.35rem;
    overflow: hidden;
}

/* ===== Header ===== */
.roast-chart-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 0.4rem;
    padding: 0.35rem 0.75rem;
    background: var(--chart-card-bg);
    border-radius: 0.5rem;
    border: 1px solid var(--chart-border);
    box-shadow: var(--chart-card-shadow);
    min-height: 2rem;
    flex-shrink: 0;
}

.roast-chart-header h4 {
    margin: 0;
    font-weight: 600;
    font-size: 1rem;
    color: var(--chart-text);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    min-width: 0;
    flex: 1;
}

/* ===== Chart Container =====
   Container là flex-column: custom-legend trên (auto height), ApexChart dưới (flex:1). */
.roast-chart-container {
    width: 100%;
    flex: 1;
    min-height: 0;
    background: var(--chart-card-bg);
    border-radius: 0.5rem;
    border: 1px solid var(--chart-border);
    box-shadow: var(--chart-card-shadow);
    padding: 0.25rem;
    box-sizing: border-box;
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

/* ApexChart wrapper (div đầu tiên không phải custom-chart-legend) fill phần còn lại */
.roast-chart-container > div:not(.custom-chart-legend) {
    flex: 1;
    min-height: 0;
    height: 100% !important;
}

.roast-chart-container .apexcharts-canvas {
    max-width: 100% !important;
    height: 100% !important;
}

.roast-chart-container .apexcharts-svg {
    max-width: 100% !important;
}

/* ── Dark mode: ApexCharts SVG text mặc định đen/xám → không thấy trên nền tối.
   Dùng `fill` (SVG) thay cho `color` (HTML). */
html[data-coffee-dark="true"] .apexcharts-xaxis text,
html[data-coffee-dark="true"] .apexcharts-yaxis text,
html[data-coffee-dark="true"] .apexcharts-xaxis-label,
html[data-coffee-dark="true"] .apexcharts-yaxis-label {
    fill: #c8c8d8 !important;
}

html[data-coffee-dark="true"] .apexcharts-xaxis-title text,
html[data-coffee-dark="true"] .apexcharts-yaxis-title text {
    fill: #e0e0ee !important;
    font-weight: 600;
}

/* Tick marks (short lines at axis) — cũng màu sáng hơn */
html[data-coffee-dark="true"] .apexcharts-xaxis line,
html[data-coffee-dark="true"] .apexcharts-yaxis line {
    stroke: #555570 !important;
}

/* Grid dashed lines — mờ nhẹ cho dark */
html[data-coffee-dark="true"] .apexcharts-gridline {
    stroke: #3a3a55 !important;
}

/* Custom chart legend — thay cho ApexCharts legend. 1 hàng ngang tuyệt đối,
   scroll ngang khi hẹp (ẩn scrollbar), không bao giờ xếp cột. */
.roast-chart-container .custom-chart-legend {
    display: flex;
    flex-direction: row;
    flex-wrap: nowrap;
    align-items: center;
    justify-content: center;
    gap: 0;
    padding: 2px 6px;
    overflow-x: auto;
    overflow-y: hidden;
    scrollbar-width: none;
    font-size: 11px;
    line-height: 1.3;
    color: var(--chart-text);
    flex-shrink: 0;
}
.roast-chart-container .custom-chart-legend::-webkit-scrollbar { display: none; }

.roast-chart-container .ccl-item {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    margin: 0 6px;
    white-space: nowrap;
    flex: 0 0 auto;
    font-weight: 500;
}

.roast-chart-container .ccl-item.ccl-bg {
    opacity: 0.7;
    font-style: italic;
}

.roast-chart-container .ccl-dot {
    display: inline-block;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    flex-shrink: 0;
}

/* ===== Phase Panel (MachinePhaseBar — cột độc lập giữa chart và info) =====
   Hẹp ngang để không chiếm chart, cao kéo đầy cột để 5 phase giãn đều. */
.roast-phase-panel {
    flex: 0 0 130px;
    width: 130px;
    min-width: 130px;
    display: flex;
    flex-direction: column;
    overflow-y: auto;
    scrollbar-width: thin;
}

/* ===== Right Panel (Info + Controls) ===== */
.roast-right-panel {
    width: 260px;
    min-width: 260px;
}

.roast-info-panel {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    overflow-y: auto;
    scrollbar-width: thin;
    flex: 1;
}

.info-section {
    background: var(--chart-card-bg);
    border: 1px solid var(--chart-border);
    border-radius: 0.5rem;
    padding: 0.6rem 0.75rem;
    box-shadow: var(--chart-card-shadow);
}

.info-section-title {
    font-size: 0.7rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--chart-text-muted);
    margin-bottom: 0.4rem;
    padding-bottom: 0.3rem;
    border-bottom: 1px solid var(--chart-border);
}

/* Timer */
.timer-section {
    text-align: center;
    padding: 0.75rem;
}

.info-icon {
    font-size: 1.2rem;
    margin-bottom: 0.15rem;
}

.timer-display {
    font-size: 1.8rem;
    font-weight: 800;
    font-variant-numeric: tabular-nums;
    color: var(--chart-text);
    letter-spacing: 0.02em;
    line-height: 1.1;
}

.info-sublabel {
    font-size: 0.65rem;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--chart-text-muted);
    margin-top: 0.15rem;
}

/* Temp cards */
.temp-row {
    display: flex;
    gap: 0.4rem;
    margin-bottom: 0.4rem;
}

.temp-row:last-child {
    margin-bottom: 0;
}

.temp-card {
    flex: 1;
    padding: 0.4rem 0.5rem;
    border-radius: 0.4rem;
    text-align: center;
}

.temp-card .temp-label {
    font-size: 0.65rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    opacity: 0.9;
}

.temp-card .temp-value {
    font-size: 1.15rem;
    font-weight: 800;
    font-variant-numeric: tabular-nums;
    line-height: 1.2;
}

.temp-card .temp-unit {
    font-size: 0.6rem;
    font-weight: 500;
    opacity: 0.7;
    margin-left: 1px;
}

.et-card {
    background: rgba(204, 15, 80, 0.1);
    border: 1px solid rgba(204, 15, 80, 0.25);
    color: #cc0f50;
}

.bt-card {
    background: rgba(10, 92, 144, 0.1);
    border: 1px solid rgba(10, 92, 144, 0.25);
    color: #0a5c90;
}

.delta-et-card {
    background: rgba(255, 140, 0, 0.1);
    border: 1px solid rgba(255, 140, 0, 0.25);
    color: #ff8c00;
}

.delta-bt-card {
    background: rgba(44, 160, 44, 0.1);
    border: 1px solid rgba(44, 160, 44, 0.25);
    color: #2ca02c;
}

html[data-coffee-dark="true"] .et-card {
    background: rgba(204, 15, 80, 0.15);
    border-color: rgba(204, 15, 80, 0.35);
}

html[data-coffee-dark="true"] .bt-card {
    background: rgba(10, 92, 144, 0.15);
    border-color: rgba(10, 92, 144, 0.35);
}

html[data-coffee-dark="true"] .delta-et-card {
    background: rgba(255, 140, 0, 0.15);
    border-color: rgba(255, 140, 0, 0.35);
}

html[data-coffee-dark="true"] .delta-bt-card {
    background: rgba(44, 160, 44, 0.15);
    border-color: rgba(44, 160, 44, 0.35);
}

/* Events list */
.events-list {
    display: flex;
    flex-direction: column;
    gap: 0.2rem;
}

.event-row {
    display: flex;
    align-items: center;
    gap: 0.4rem;
    font-size: 0.75rem;
    padding: 0.2rem 0;
    border-bottom: 1px solid var(--chart-border);
}

.event-row:last-child {
    border-bottom: none;
}

.event-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    flex-shrink: 0;
}

.event-name {
    font-weight: 600;
    color: var(--chart-text);
    flex: 1;
}

.event-time {
    font-variant-numeric: tabular-nums;
    color: var(--chart-text-muted);
    font-size: 0.72rem;
}

.event-temp {
    font-weight: 600;
    color: var(--chart-text);
    font-variant-numeric: tabular-nums;
    min-width: 32px;
    text-align: right;
}

/* Phase rows */
.phase-rows {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
}

.phase-row {
    display: flex;
    align-items: center;
    gap: 0.4rem;
    font-size: 0.75rem;
    padding: 0.2rem 0;
}

.phase-dot {
    width: 10px;
    height: 10px;
    border-radius: 3px;
    flex-shrink: 0;
}

.phase-name {
    font-weight: 600;
    color: var(--chart-text);
    flex: 1;
}

.phase-info {
    font-variant-numeric: tabular-nums;
    color: var(--chart-text-muted);
}

.phase-pct {
    font-weight: 700;
    color: var(--chart-text);
    min-width: 38px;
    text-align: right;
}

/* Info rows */
.info-rows {
    display: flex;
    flex-direction: column;
    gap: 0.15rem;
}

.info-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 0.75rem;
    padding: 0.15rem 0;
}

.info-key {
    color: var(--chart-text-muted);
}

.info-val {
    font-weight: 700;
    color: var(--chart-text);
    font-variant-numeric: tabular-nums;
}

/* ===== Phase Statistics Bar ===== */
.phase-stats-bar {
    display: flex;
    border-radius: 0.5rem;
    overflow: hidden;
    height: 1.75rem;
    flex-shrink: 0;
    font-size: 0.78rem;
    font-weight: 600;
    border: 1px solid var(--chart-border);
    box-shadow: var(--chart-card-shadow);
    width: 100%;
}

.phase-segment {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.4rem;
    color: #fff;
    padding: 0 0.5rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
}

.phase-segment.drying { background: #4CAF50; }
.phase-segment.maillard { background: #E8A317; }
.phase-segment.development { background: #795548; }
.phase-segment .phase-label { font-weight: 700; }
.phase-segment .phase-time { opacity: 0.9; }
.phase-segment .phase-percent { opacity: 0.85; font-size: 0.72rem; }

/* ===== RoR Config Panel ===== */
.ror-config-panel {
    background: var(--chart-card-bg);
    border-radius: 0.5rem;
    border: 1px solid var(--chart-border);
    box-shadow: var(--chart-card-shadow);
    padding: 0.75rem;
    width: 100%;
    box-sizing: border-box;
}

.ror-config-panel .config-title {
    font-weight: 600;
    font-size: 0.85rem;
    color: var(--chart-text);
    margin-bottom: 0.5rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    user-select: none;
}

.ror-config-panel .config-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
    gap: 0.5rem 1rem;
}

.config-item {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.config-item label {
    font-size: 0.78rem;
    color: var(--chart-text);
    min-width: 75px;
}

.config-item input[type="range"] {
    flex: 1;
    min-width: 60px;
}

.config-item .value-badge {
    font-size: 0.72rem;
    background: var(--chart-border);
    padding: 0.1rem 0.4rem;
    border-radius: 0.25rem;
    min-width: 1.5rem;
    text-align: center;
}

/* ===== Theme Toggle ===== */
.theme-toggle {
    background: none;
    border: 1px solid var(--chart-border);
    border-radius: 0.375rem;
    padding: 0.25rem 0.5rem;
    cursor: pointer;
    font-size: 1.1rem;
    color: var(--chart-text);
    transition: all 0.2s;
}

.theme-toggle:hover {
    background: var(--chart-border);
}

/* ===== Responsive ===== */

/* Tablet landscape & small desktop (<=1024px) */
@media (max-width: 1024px) {
    .roast-right-panel {
        width: 220px;
        min-width: 220px;
    }
}

/* Tablet portrait (<=900px): stack chart + info vertically */
@media (max-width: 900px) {
    .roast-chart-page {
        height: auto;
        min-height: calc(100vh - 120px);
        overflow-y: auto;
    }

    .roast-chart-body {
        flex-direction: column;
    }

    .roast-right-panel {
        width: 100%;
        min-width: unset;
    }

    .roast-info-panel {
        width: 100%;
        min-width: unset;
        flex-direction: row;
        flex-wrap: wrap;
        overflow-y: visible;
    }

    .roast-info-panel .info-section {
        flex: 1;
        min-width: 200px;
    }

    .roast-info-panel .timer-section {
        min-width: 140px;
        flex: 0;
    }

    .roast-chart-container {
        min-height: 300px;
    }

    .roast-chart-header {
        gap: 0.3rem;
        padding: 0.3rem 0.5rem;
    }
}

/* Small tablet / large phone (<=600px) */
@media (max-width: 600px) {
    .roast-chart-page {
        padding: 0.25rem;
        gap: 0.25rem;
    }

    .roast-chart-header {
        flex-direction: column;
        align-items: stretch;
    }

    .roast-chart-header h4 {
        font-size: 0.9rem;
    }

    .roast-chart-container {
        min-height: 250px;
    }

    .roast-info-panel .info-section {
        min-width: 150px;
    }

    .timer-display {
        font-size: 1.4rem;
    }

    .temp-card .temp-value {
        font-size: 1rem;
    }

    .phase-stats-bar {
        height: 1.5rem;
        font-size: 0.68rem;
    }

    .info-section-title {
        font-size: 0.65rem;
    }

    .event-row, .phase-row, .info-row {
        font-size: 0.7rem;
    }
}

/* ====================================================================
   Mobile "chart-only" mode
   Triggers when EITHER:
     • viewport width ≤ 767px (phone portrait, iPad < portrait)     → narrow
     • viewport height ≤ 500px (phone landscape — iPhone 12-15 Pro
       Max landscape is 932×430, height ≤ 500 catches all phones)   → short
   iPad in any orientation has height ≥ 768px, so the full UI stays.
==================================================================== */
@media (max-width: 767px), (max-height: 500px) {
    /* Hide everything except chart + minimal header */
    .roast-chart-page .chart-header-actions,
    .roast-chart-page .roast-right-panel,
    .roast-chart-page .phase-stats-bar,
    .roast-chart-page .chart-extras {
        display: none !important;
    }

    /* Also hide ApexCharts legend (9 series) + toolbar icons — they eat ~80-100px of
       precious vertical space on phones. The curves' colors are already distinctive
       enough (red=ET, blue=BT, green=ΔBT, etc.) for quick visual reading. */
    .roast-chart-page .roast-chart-container .apexcharts-legend,
    .roast-chart-page .roast-chart-container .apexcharts-toolbar {
        display: none !important;
    }

    /* Chart takes all available vertical space */
    .roast-chart-body {
        flex-direction: column;
        gap: 0;
    }

    .roast-chart-main {
        height: 100%;
    }

    .roast-chart-container {
        flex: 1;
        min-height: unset;
        height: 100%;
    }

    /* Allow page to breathe a bit; header keeps title + recording/user chip */
    .roast-chart-header {
        gap: 0.25rem;
        padding: 0.3rem 0.5rem;
    }

    .roast-chart-header h4 {
        font-size: 0.95rem;
    }
}

/* Phone (<=480px) */
@media (max-width: 480px) {
    .roast-chart-page {
        padding: 0.2rem;
    }

    .roast-chart-header h4 {
        font-size: 0.85rem;
    }

    .roast-chart-container {
        min-height: 220px;
    }

    .ror-config-panel .config-grid {
        grid-template-columns: 1fr;
    }

    .roast-info-panel {
        flex-direction: column;
    }

    .roast-info-panel .info-section {
        min-width: unset;
    }

    .temp-row {
        gap: 0.25rem;
    }

    .temp-card {
        padding: 0.3rem 0.4rem;
    }

    .temp-card .temp-value {
        font-size: 0.9rem;
    }

    .timer-display {
        font-size: 1.2rem;
    }
}



/* ── Machine Phase Bar — VERTICAL stepper ──
   Layout: mỗi phase = row [accent bar left | label trên value dưới].
   Giữa 2 phase: connector vertical line + round icon centered trên line. */

.machine-phase-bar {
    display: flex;
    flex-direction: column;
    padding: 0.75rem 0.5rem;
    background: var(--chart-card-bg);
    border: 1px solid var(--chart-border);
    border-radius: 0.5rem;
    box-shadow: var(--chart-card-shadow);
    /* Fill chiều dọc cột → 5 phase + 4 connector giãn đều theo không gian */
    flex: 1;
    min-height: 0;
    justify-content: space-between;
}

/* Step: CSS grid [3px accent | content column] */
.mphase-step {
    display: grid;
    grid-template-columns: 3px 1fr;
    grid-template-rows: auto auto;
    column-gap: 10px;
    row-gap: 2px;
    padding: 0.4rem 0.3rem;
    border-radius: 5px;
    transition: background 0.2s;
    flex: 0 0 auto;
}

.mphase-step::before {
    content: "";
    grid-column: 1;
    grid-row: 1 / span 2;
    border-radius: 2px;
    background: var(--mud-palette-lines-default);
    transition: background 0.2s;
}

.mphase-step.past::before,
.mphase-step.current::before {
    background: #7fbb5c;
}

.mphase-step.current {
    background: color-mix(in srgb, #7fbb5c 8%, transparent);
}

.mphase-label {
    grid-column: 2;
    grid-row: 1;
    font-size: 0.78rem;
    font-weight: 600;
    color: var(--chart-text);
    line-height: 1.1;
    white-space: nowrap;
    letter-spacing: 0.01em;
}

.mphase-label .mphase-unit {
    font-weight: 400;
    font-size: 0.72rem;
    color: var(--chart-text-muted);
    opacity: 0.8;
    margin-left: 2px;
}

.mphase-value {
    grid-column: 2;
    grid-row: 2;
    display: flex;
    align-items: baseline;
    gap: 7px;
    font-family: ui-monospace, "SF Mono", Menlo, monospace;
    font-variant-numeric: tabular-nums;
    font-size: 1.25rem;
    font-weight: 700;
    line-height: 1.15;
    color: #7fbb5c;
}

.mphase-value .mphase-sep {
    color: var(--chart-text-muted);
    font-weight: 400;
    font-size: 1.05rem;
}

.mphase-value .mphase-target {
    background: rgba(88, 166, 217, 0.16);
    color: #3a88b5;
    padding: 1px 9px;
    border-radius: 4px;
    font-weight: 700;
    font-size: 1.05rem;
}

/* State variations */
.mphase-step.past .mphase-elapsed { color: var(--chart-text); }
.mphase-step.future .mphase-elapsed { color: var(--chart-text-muted); opacity: 0.65; }
.mphase-step.future .mphase-target { opacity: 0.5; }
.mphase-step.current .mphase-elapsed { color: #5a9e3a; }

/* Dark mode target pill */
html[data-coffee-dark="true"] .mphase-value .mphase-target {
    background: rgba(88, 166, 217, 0.22);
    color: #7ec5e5;
}

/* ── Connector vertical ── line center + icon on top ──
   flex: 1 cho connector tự kéo dài để 5 phase + 4 connector giãn đầy cột dọc.
   min-height giữ icon còn nhìn thấy, max-height tránh line quá dài khi cột siêu cao. */
.mphase-connector {
    display: flex;
    justify-content: center;
    align-items: center;
    position: relative;
    flex: 1 1 auto;
    min-height: 32px;
    max-height: 80px;
    margin: 0;
}

.mphase-connector::before {
    content: "";
    position: absolute;
    left: 50%;
    top: 0; bottom: 0;
    width: 2px;
    transform: translateX(-50%);
    background: var(--mud-palette-lines-default);
    border-radius: 2px;
    transition: background 0.3s;
}

.mphase-connector.past::before {
    background: #7fbb5c;
}

.mphase-transition-icon {
    position: relative;
    z-index: 1;
    width: 28px;
    height: 28px;
    border-radius: 50%;
    background: var(--chart-card-bg);
    border: 2px solid var(--mud-palette-lines-default);
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--mud-palette-text-disabled);
    transition: all 0.2s;
}

.mphase-connector.past .mphase-transition-icon {
    background: #7fbb5c;
    border-color: #7fbb5c;
    color: #fff;
}

/* Responsive ≤ 900px — shrink fonts */
@media (max-width: 900px) {
    .machine-phase-bar { padding: 0.5rem 0.4rem; }
    .mphase-label { font-size: 0.7rem; }
    .mphase-value { font-size: 1rem; }
    .mphase-value .mphase-target { font-size: 0.88rem; }
    .mphase-connector { height: 28px; }
    .mphase-transition-icon { width: 22px; height: 22px; }
}

/* ── Recipe/Control tabs: cố định kích thước 2 tab bằng nhau ─────────────
   recipe-wrapper là div bao MudTabs.
   Dùng flex-basis cố định (không phải min-height) để Control tab nội dung to
   KHÔNG nở ra đẩy chart lên. Chart area tự nhận phần flex:1 còn lại.
   305px: header 20px + 4 rows + params row + buttons + MudTab bar + pa-2.
   Sát content Recipe để chart chiếm nhiều chiều dọc nhất có thể. */
.roast-chart-page .recipe-wrapper {
    flex: 0 0 305px;
    min-height: 0;
    display: flex;
    flex-direction: column;
}
.roast-chart-page .recipe-wrapper > .mud-tabs {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-height: 0;
}
.roast-chart-page .recipe-wrapper .mud-tabs-panels {
    flex: 1;
    min-height: 0;
}
.roast-chart-page .recipe-wrapper .mud-tab-panel {
    height: 100%;
}

/* ── Quick Control widget — fill tab, scroll nếu content dài hơn ────────── */
.quick-control-widget {
    height: 100%;
    overflow-y: auto;
    scrollbar-width: thin;
    padding-right: 4px;
}

/* Grid trong widget — các MudPaper con (components/valves/aux/speed) bằng nhau
   dọc nhờ Style="height:100%" trên mỗi MudPaper + align-items:stretch mặc định */
.quick-control-widget .mud-grid {
    align-items: stretch;
}
