/* Kraken Bot Flow Dashboard — Dark Theme (xyOps inspired) */

:root {
    --bg-primary: #0f0f1a;
    --bg-secondary: #161625;
    --bg-card: #1c1c32;
    --bg-card-hover: #24243e;
    --border: #2a2a4a;
    --border-light: #3a3a5c;
    --text-primary: #e0e0ef;
    --text-secondary: #8888aa;
    --text-muted: #5a5a7a;
    --accent-blue: #4fc3f7;
    --accent-green: #4caf50;
    --accent-red: #f44336;
    --accent-orange: #ff9800;
    --accent-yellow: #ffc107;
    --accent-purple: #ab47bc;
    --glow-blue: rgba(79,195,247,0.3);
    --glow-green: rgba(76,175,80,0.3);
    --glow-red: rgba(244,67,54,0.3);
}

* { margin: 0; padding: 0; box-sizing: border-box; }

body {
    font-family: 'Inter', 'Segoe UI', -apple-system, sans-serif;
    background: var(--bg-primary);
    color: var(--text-primary);
    overflow: hidden;
    height: 100vh;
}

/* === HEADER === */
#header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 8px 20px;
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border);
    height: 48px;
    z-index: 100;
}
#header .logo {
    font-size: 16px;
    font-weight: 700;
    color: var(--accent-blue);
    letter-spacing: 0.5px;
}
#header .status-bar {
    display: flex;
    gap: 20px;
    align-items: center;
    font-size: 13px;
}
.status-bar .item { color: var(--text-secondary); }
.status-bar .item span { color: var(--text-primary); font-weight: 600; }

/* Bot selector dropdown */
#bot-selector {
    background: var(--bg-card);
    color: var(--text-primary);
    border: 1px solid var(--border-light);
    border-radius: 6px;
    padding: 4px 10px;
    font-family: 'Inter', sans-serif;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    outline: none;
}
#bot-selector:hover { border-color: var(--accent-blue); }
#bot-selector:focus { border-color: var(--accent-blue); box-shadow: 0 0 0 2px var(--glow-blue); }
.status-bar .badge {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 2px 8px;
    border-radius: 10px;
    font-size: 11px;
    font-weight: 600;
}
.badge.regime {
    background: rgba(79,195,247,0.15);
    color: var(--accent-blue);
    border: 1px solid rgba(79,195,247,0.3);
}
.badge.errors {
    background: rgba(244,67,54,0.15);
    color: var(--accent-red);
    border: 1px solid rgba(244,67,54,0.3);
}
.badge.warnings {
    background: rgba(255,193,7,0.15);
    color: var(--accent-yellow);
    border: 1px solid rgba(255,193,7,0.3);
}
.badge.bot-status {
    background: rgba(76,175,80,0.15);
    color: var(--accent-green);
    border: 1px solid rgba(76,175,80,0.3);
}
.badge.bot-status.offline {
    background: rgba(244,67,54,0.15);
    color: var(--accent-red);
    border: 1px solid rgba(244,67,54,0.3);
}
.badge.bot-status.idle {
    background: rgba(136,136,170,0.15);
    color: var(--text-secondary);
    border: 1px solid rgba(136,136,170,0.3);
}

/* === PHASE TABS === */
#phase-tabs {
    display: flex;
    gap: 2px;
    padding: 4px 20px;
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border);
    height: 32px;
    align-items: center;
}
.phase-tab {
    padding: 3px 12px;
    border-radius: 4px;
    font-size: 11px;
    font-weight: 600;
    cursor: pointer;
    color: var(--text-muted);
    transition: all 0.2s;
    white-space: nowrap;
}
.phase-tab:hover { background: var(--bg-card); color: var(--text-primary); }
.phase-tab.active { background: rgba(79,195,247,0.15); color: var(--accent-blue); }

/* === MAIN LAYOUT === */
#main {
    height: calc(100vh - 80px);
    position: relative;
}

/* Graph chiếm toàn bộ (sidebar overlay) */
#main-left {
    width: 100%;
    height: 100%;
    position: relative;
}

/* === GRAPH AREA === */
#graph-container {
    width: 100%;
    height: 100%;
    position: relative;
    background: var(--bg-primary);
    background-image: radial-gradient(circle, rgba(58,58,92,0.3) 1px, transparent 1px);
    background-size: 24px 24px;
    overflow: hidden;
}
#cy {
    width: 100%;
    height: 100%;
}

/* === GRAPH TOOLBAR === */
#graph-toolbar {
    position: absolute;
    bottom: 16px;
    left: 16px;
    display: flex;
    gap: 6px;
    z-index: 50;
}
#graph-toolbar button {
    padding: 6px 12px;
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: 6px;
    color: var(--text-secondary);
    font-size: 12px;
    cursor: pointer;
    transition: all 0.2s;
}
#graph-toolbar button:hover {
    background: var(--bg-card-hover);
    color: var(--text-primary);
    border-color: var(--accent-blue);
}

/* === SIDEBAR (auto-hide) === */

/* Vùng hover trigger bên cạnh phải — chỉ desktop */
#sidebar-trigger {
    position: fixed;
    top: 80px;
    right: 0;
    width: 18px;
    height: calc(100vh - 80px);
    z-index: 149;
    background: transparent;
}
#sidebar-trigger::after {
    content: '';
    position: absolute;
    top: 50%;
    right: 4px;
    transform: translateY(-50%);
    width: 4px;
    height: 48px;
    border-radius: 2px;
    background: var(--border-light);
    opacity: 0.4;
    transition: opacity 0.2s;
}
#sidebar-trigger:hover::after { opacity: 1; }

/* Nút toggle (luôn hiện) */
#sidebar-toggle {
    position: fixed;
    top: 84px;
    right: 8px;
    z-index: 160;
    width: 36px;
    height: 36px;
    border-radius: 50%;
    background: var(--bg-card);
    border: 1px solid var(--border);
    color: var(--text-secondary);
    font-size: 16px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
    box-shadow: 0 2px 8px rgba(0,0,0,0.3);
}
#sidebar-toggle:hover {
    background: var(--bg-card-hover);
    border-color: var(--accent-blue);
    color: var(--text-primary);
}

/* Sidebar chính — trượt từ bên phải */
#sidebar {
    position: fixed;
    top: 80px;
    right: 0;
    width: 280px;
    height: calc(100vh - 80px);
    background: var(--bg-secondary);
    border-left: 1px solid var(--border);
    overflow-y: auto;
    padding: 12px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    z-index: 150;
    transform: translateX(100%);
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    box-shadow: -4px 0 24px rgba(0,0,0,0);
}

/* Desktop: hover trigger → mở sidebar */
@media (hover: hover) and (pointer: fine) {
    #sidebar-trigger:hover ~ #sidebar,
    #sidebar:hover {
        transform: translateX(0);
        box-shadow: -4px 0 24px rgba(0,0,0,0.4);
    }
}

/* JS class: khi toggle bằng nút (cả desktop lẫn mobile) */
#sidebar.pinned {
    transform: translateX(0);
    box-shadow: -4px 0 24px rgba(0,0,0,0.4);
}
.sidebar-section {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 10px 12px;
}
.sidebar-section h3 {
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: var(--text-muted);
    margin-bottom: 8px;
}
.sidebar-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 3px 0;
    font-size: 13px;
}
.sidebar-row .label { color: var(--text-secondary); }
.sidebar-row .value { color: var(--text-primary); font-weight: 600; }
.value.positive { color: var(--accent-green); }
.value.negative { color: var(--accent-red); }

/* === DETAIL PANEL (bottom) === */
#detail-panel {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    max-height: 45%;
    background: var(--bg-secondary);
    border-top: 2px solid var(--accent-blue);
    transform: translateY(100%);
    transition: transform 0.3s ease;
    z-index: 60;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}
#detail-panel.open { transform: translateY(0); }

#detail-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px 16px;
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}
#detail-header h2 {
    font-size: 15px;
    font-weight: 700;
    color: var(--text-primary);
}
#detail-header .phase-label {
    font-size: 11px;
    padding: 2px 8px;
    border-radius: 4px;
    background: rgba(79,195,247,0.15);
    color: var(--accent-blue);
}
#detail-close {
    background: none;
    border: none;
    color: var(--text-muted);
    font-size: 18px;
    cursor: pointer;
    padding: 4px 8px;
}
#detail-close:hover { color: var(--text-primary); }

#detail-body {
    display: flex;
    gap: 0;
    overflow: hidden;
    flex: 1;
}
.detail-col {
    flex: 1;
    padding: 12px 16px;
    overflow-y: auto;
    border-right: 1px solid var(--border);
}
.detail-col:last-child { border-right: none; }

.detail-col h4 {
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: var(--text-muted);
    margin-bottom: 8px;
}

/* Data table */
.data-table { width: 100%; font-size: 12px; }
.data-table td {
    padding: 3px 4px;
    border-bottom: 1px solid rgba(42,42,74,0.5);
}
.data-table td:first-child { color: var(--text-secondary); width: 35%; }
.data-table td:last-child { color: var(--text-primary); font-weight: 500; }

/* Log viewer */
.log-viewer {
    font-family: 'JetBrains Mono', 'Fira Code', monospace;
    font-size: 11px;
    line-height: 1.6;
    max-height: 200px;
    overflow-y: auto;
    background: var(--bg-primary);
    border-radius: 4px;
    padding: 8px;
}
.log-line { white-space: pre-wrap; word-break: break-all; }
.log-line.error { color: var(--accent-red); }
.log-line.warning { color: var(--accent-yellow); }
.log-line.info { color: var(--text-secondary); }

/* Suggestion box */
.suggestion-box {
    margin-top: 8px;
    padding: 8px 12px;
    background: rgba(79,195,247,0.08);
    border: 1px solid rgba(79,195,247,0.2);
    border-radius: 6px;
    font-size: 12px;
    color: var(--accent-blue);
    line-height: 1.5;
}
.suggestion-box .icon { margin-right: 4px; }

/* === CONNECTION STATUS === */
#ws-status {
    position: fixed;
    top: 56px;
    right: 50px;
    padding: 4px 10px;
    font-size: 11px;
    border-radius: 0 0 6px 6px;
    z-index: 200;
    font-weight: 600;
}
#ws-status.connected { background: var(--accent-green); color: #fff; }
#ws-status.disconnected { background: var(--accent-red); color: #fff; }
#ws-status.connecting { background: var(--accent-yellow); color: #000; }

/* === CYCLE HISTORY (sidebar) === */
.history-item {
    display: flex;
    justify-content: space-between;
    padding: 3px 0;
    font-size: 11px;
    color: var(--text-muted);
}
.history-item .cycle-num { color: var(--text-secondary); }
.history-item .cycle-time { color: var(--text-primary); }
.history-item.has-errors .cycle-time { color: var(--accent-red); }

/* === TELEGRAM BUBBLES (floating, draggable) === */
#tg-bubble-bar {
    position: fixed;
    bottom: 16px;
    right: 16px;
    z-index: 200;
    display: flex;
    flex-direction: row-reverse;
    gap: 8px;
    align-items: center;
    user-select: none;
    touch-action: none;
}
#tg-bubble-bar.dragging { cursor: grabbing; }

/* Master bubble (📨) — luôn hiện */
.tg-bubble.master {
    width: 50px;
    height: 50px;
    background: var(--bg-card);
    border: 2px solid var(--accent-blue);
    cursor: grab;
    font-size: 22px;
    box-shadow: 0 4px 16px rgba(0,0,0,0.4);
    flex-shrink: 0;
}
.tg-bubble.master:hover {
    border-color: #fff;
}
#tg-bubble-bar.dragging .tg-bubble.master { cursor: grabbing; }

/* Count badge trên master */
.tg-count-badge {
    position: absolute;
    top: -4px;
    right: -4px;
    background: var(--accent-red);
    color: #fff;
    font-size: 10px;
    font-weight: 700;
    min-width: 18px;
    height: 18px;
    border-radius: 9px;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0 4px;
    font-family: 'Inter', sans-serif;
}
.tg-count-badge[data-count="0"] { display: none; }

/* Child bubbles container */
#tg-bubbles {
    display: flex;
    gap: 8px;
    align-items: center;
    overflow: hidden;
    transition: max-width 0.3s ease, opacity 0.3s ease;
    max-width: 600px;
    opacity: 1;
}
#tg-bubbles.collapsed {
    max-width: 0;
    opacity: 0;
    gap: 0;
}

.tg-bubble {
    width: 42px;
    height: 42px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    cursor: pointer;
    border: 2px solid transparent;
    transition: transform 0.2s, box-shadow 0.3s, border-color 0.3s;
    position: relative;
    flex-shrink: 0;
}
.tg-bubble:hover {
    transform: scale(1.15);
}
.tg-bubble.active {
    border-color: #fff;
    transform: scale(1.1);
}
.tg-bubble.read {
    opacity: 0.45;
}
.tg-bubble.read:hover {
    opacity: 0.8;
}
.tg-bubble.read.active {
    opacity: 1;
}

/* Glow khi message mới */
.tg-bubble.fresh {
    animation: bubble-glow 1.5s ease-in-out 3;
}
.tg-bubble.master.fresh {
    animation: bubble-glow 1.5s ease-in-out 3;
    --bubble-glow: rgba(79,195,247,0.6);
}
@keyframes bubble-glow {
    0%, 100% { box-shadow: 0 0 6px rgba(255,255,255,0.2); }
    50% { box-shadow: 0 0 18px var(--bubble-glow, rgba(255,255,255,0.5)), 0 0 36px var(--bubble-glow, rgba(255,255,255,0.2)); }
}

/* Bubble time label */
.tg-bubble-time {
    position: absolute;
    bottom: -15px;
    left: 50%;
    transform: translateX(-50%);
    font-size: 9px;
    font-family: 'JetBrains Mono', monospace;
    color: var(--text-muted);
    white-space: nowrap;
}

/* Bubble type-specific backgrounds */
.tg-bubble.position { background: rgba(79,195,247,0.2); --bubble-glow: rgba(79,195,247,0.6); }
.tg-bubble.ml_prediction { background: rgba(171,71,188,0.2); --bubble-glow: rgba(171,71,188,0.6); }
.tg-bubble.bot_report { background: rgba(102,187,106,0.2); --bubble-glow: rgba(102,187,106,0.6); }
.tg-bubble.news { background: rgba(255,152,0,0.2); --bubble-glow: rgba(255,152,0,0.6); }
.tg-bubble.prediction_info { background: rgba(141,110,99,0.2); --bubble-glow: rgba(141,110,99,0.6); }
.tg-bubble.unknown { background: rgba(90,90,122,0.25); --bubble-glow: rgba(90,90,122,0.5); }

/* === TELEGRAM POPUP (message content overlay) === */
#tg-popup {
    position: fixed;
    bottom: 76px;
    right: 16px;
    width: min(520px, calc(100vw - 32px));
    max-height: 55vh;
    background: var(--bg-secondary);
    border: 1px solid var(--border-light);
    border-radius: 12px;
    z-index: 210;
    display: flex;
    flex-direction: column;
    box-shadow: 0 8px 32px rgba(0,0,0,0.5), 0 0 0 1px rgba(79,195,247,0.1);
    transition: opacity 0.2s, transform 0.2s;
    opacity: 1;
    transform: translateY(0);
}
#tg-popup.hidden {
    opacity: 0;
    transform: translateY(12px);
    pointer-events: none;
}

#tg-popup-header {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 12px 16px;
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}
#tg-popup-icon { font-size: 18px; }
#tg-popup-type {
    font-weight: 700;
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    padding: 2px 8px;
    border-radius: 4px;
    color: var(--text-primary);
}
#tg-popup-time {
    margin-left: auto;
    font-size: 11px;
    font-family: 'JetBrains Mono', monospace;
    color: var(--text-muted);
}
#tg-popup-close {
    background: none;
    border: none;
    color: var(--text-muted);
    font-size: 16px;
    cursor: pointer;
    padding: 0 4px;
    margin-left: 8px;
}
#tg-popup-close:hover { color: var(--text-primary); }

#tg-popup-content {
    flex: 1;
    overflow-y: auto;
    padding: 14px 16px;
    font-family: 'JetBrains Mono', monospace;
    font-size: 12px;
    line-height: 1.7;
    color: var(--text-secondary);
    white-space: pre-wrap;
    word-break: break-word;
}

/* Popup type header colors */
#tg-popup.type-position #tg-popup-type { background: rgba(79,195,247,0.15); color: #4fc3f7; }
#tg-popup.type-ml_prediction #tg-popup-type { background: rgba(171,71,188,0.15); color: #ab47bc; }
#tg-popup.type-bot_report #tg-popup-type { background: rgba(102,187,106,0.15); color: #66bb6a; }
#tg-popup.type-news #tg-popup-type { background: rgba(255,152,0,0.15); color: #ff9800; }
#tg-popup.type-prediction_info #tg-popup-type { background: rgba(141,110,99,0.15); color: #8d6e63; }
#tg-popup.type-unknown #tg-popup-type { background: rgba(90,90,122,0.15); color: #5a5a7a; }

/* === SCROLLBAR === */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--border-light); }

/* === ANIMATIONS === */
@keyframes pulse-glow {
    0%, 100% { box-shadow: 0 0 8px var(--glow-blue); }
    50% { box-shadow: 0 0 20px var(--glow-blue), 0 0 40px rgba(79,195,247,0.15); }
}

/* ===========================================================
   RESPONSIVE — Mobile / Tablet / Foldable
   =========================================================== */

/* Body cho phép scroll trên mobile */
@media (max-width: 1024px) {
    body { overflow: auto; height: auto; min-height: 100vh; }
}

/* --- Tablet (768–1024px) --- */
@media (max-width: 1024px) {
    #header { padding: 6px 12px; height: auto; min-height: 44px; flex-wrap: wrap; gap: 6px; }
    #header .logo { font-size: 14px; }
    #header .status-bar { gap: 8px; font-size: 11px; flex-wrap: wrap; }

    #phase-tabs { padding: 4px 8px; overflow-x: auto; height: auto; min-height: 28px; }
    .phase-tab { font-size: 10px; padding: 2px 8px; }

    #main { height: calc(100vh - 80px); }
    #main-left { min-height: 100%; }
    #graph-container { min-height: 100%; }

    /* Sidebar overlay nhỏ hơn trên tablet */
    #sidebar { width: 260px; }

    #ws-status { right: 12px; }

    #detail-body { flex-direction: column; }
    .detail-col { border-right: none; border-bottom: 1px solid var(--border); }
    .detail-col:last-child { border-bottom: none; }

    .tg-bubble { width: 38px; height: 38px; font-size: 16px; }
    .tg-bubble.master { width: 44px; height: 44px; font-size: 20px; }
    #tg-bubbles { gap: 6px; }
    #tg-popup { width: min(480px, calc(100vw - 24px)); }
}

/* --- Mobile (<768px) --- */
@media (max-width: 768px) {
    #header { padding: 6px 10px; }
    #header .logo { font-size: 13px; }
    #header .status-bar { gap: 6px; font-size: 10px; }
    #header .status-bar .item { display: none; }
    #header .status-bar .item:first-of-type { display: block; }
    .badge { font-size: 9px; padding: 1px 6px; }

    #phase-tabs { padding: 2px 6px; gap: 1px; }
    .phase-tab { font-size: 9px; padding: 2px 6px; }

    /* Ẩn trigger zone trên touch — dùng nút toggle */
    #sidebar-trigger { display: none; }
    #sidebar-toggle { top: 52px; right: 6px; width: 32px; height: 32px; font-size: 14px; }

    /* Sidebar overlay full-height trên mobile */
    #sidebar {
        width: min(280px, 80vw);
        top: 0;
        height: 100vh;
        padding-top: 56px;
    }

    #detail-panel { max-height: 60%; }
    #detail-header { padding: 8px 10px; }
    #detail-header h2 { font-size: 13px; }
    .detail-col { padding: 8px 10px; }
    .log-viewer { max-height: 140px; font-size: 10px; }

    #graph-toolbar { bottom: 8px; left: 8px; }
    #graph-toolbar button { padding: 4px 8px; font-size: 10px; }

    .tg-bubble { width: 36px; height: 36px; font-size: 15px; }
    .tg-bubble.master { width: 42px; height: 42px; font-size: 18px; }
    .tg-bubble-time { font-size: 7px; bottom: -12px; }
    #tg-bubbles { gap: 5px; max-width: calc(100vw - 80px); }
    #tg-bubble-bar { bottom: 10px; right: 10px; }

    #tg-popup {
        width: calc(100vw - 20px);
        right: 10px !important;
        left: auto !important;
        bottom: 64px !important;
        max-height: 50vh;
    }
    #tg-popup-header { padding: 8px 12px; }
    #tg-popup-content { padding: 10px 12px; font-size: 11px; }

    #ws-status { right: 8px; font-size: 10px; padding: 2px 8px; }
}

/* --- Small mobile / Foldable (<480px) --- */
@media (max-width: 480px) {
    #header .logo { font-size: 12px; letter-spacing: 0; }
    #header .status-bar { gap: 4px; }
    #bot-selector { font-size: 11px; padding: 3px 6px; }
    .badge.regime, .badge.bot-status { font-size: 8px; padding: 1px 4px; }
    .badge.errors, .badge.warnings { display: none; }

    #phase-tabs { display: none; }

    #sidebar { width: 100vw; border-left: none; }
    #sidebar .sidebar-section { padding: 6px 8px; }
    .sidebar-section h3 { font-size: 9px; margin-bottom: 4px; }
    .sidebar-row { font-size: 11px; }

    .tg-bubble { width: 32px; height: 32px; font-size: 14px; }
    .tg-bubble.master { width: 38px; height: 38px; font-size: 16px; }
    .tg-count-badge { font-size: 8px; min-width: 14px; height: 14px; }
    #tg-bubbles { gap: 4px; max-width: calc(100vw - 60px); }

    #tg-popup {
        width: calc(100vw - 12px);
        right: 6px !important;
        left: auto !important;
        max-height: 60vh;
        border-radius: 8px;
    }
    #tg-popup-content { font-size: 10px; line-height: 1.5; }
}

/* --- Very small foldable (<360px) --- */
@media (max-width: 360px) {
    #header { padding: 4px 6px; }
    #header .logo { font-size: 11px; }
    .badge { display: none; }
    .badge.bot-status { display: inline-flex; font-size: 8px; }

    .sidebar-section h3 { font-size: 8px; }
    .sidebar-row { font-size: 10px; }

    .tg-bubble { width: 28px; height: 28px; font-size: 12px; }
    .tg-bubble.master { width: 34px; height: 34px; font-size: 14px; }
    .tg-bubble-time { display: none; }
}

/* Backdrop khi sidebar mở trên mobile */
#sidebar-backdrop {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.5);
    z-index: 140;
}
#sidebar-backdrop.visible { display: block; }
