:root {
    --bg-primary: #0f1117;
    --bg-secondary: #1a1d2e;
    --bg-tertiary: #242838;
    --bg-hover: #2a2f42;
    --text-primary: #e8eaed;
    --text-secondary: #9ca3af;
    --text-muted: #6b7280;
    --border: #2d3148;
    --accent: #3b82f6;
    --accent-hover: #2563eb;
    --green: #10b981;
    --green-bg: rgba(16, 185, 129, 0.1);
    --red: #ef4444;
    --red-bg: rgba(239, 68, 68, 0.1);
    --yellow: #f59e0b;
    --sidebar-width: 220px;
    --header-height: 56px;
    --radius: 8px;
    --shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
    --font-sans: 'Google Sans', 'IBM Plex Sans Thai', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    --font-mono: 'IBM Plex Mono', 'Google Sans', Consolas, monospace;
}

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

body {
    font-family: var(--font-sans);
    background: var(--bg-primary);
    color: var(--text-primary);
    min-height: 100vh;
    overflow-x: hidden;
}

/* Header */
.app-header {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: var(--header-height);
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 16px;
    z-index: 100;
}

.header-left {
    display: flex;
    align-items: center;
    gap: 12px;
}

.header-right {
    display: flex;
    align-items: center;
    gap: 8px;
}

.menu-toggle {
    display: none;
    background: none;
    border: none;
    color: var(--text-primary);
    cursor: pointer;
    padding: 4px;
}

.logo {
    font-size: 1.3rem;
    font-weight: 700;
    color: var(--text-primary);
}

.logo span {
    color: var(--accent);
}

.icon-btn {
    background: none;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    padding: 8px;
    border-radius: var(--radius);
    transition: all 0.2s;
}

.icon-btn:hover {
    background: var(--bg-hover);
    color: var(--text-primary);
}

/* Sidebar */
.sidebar {
    position: fixed;
    top: var(--header-height);
    left: 0;
    bottom: 0;
    width: var(--sidebar-width);
    background: var(--bg-secondary);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    padding: 12px 8px;
    z-index: 90;
    transition: transform 0.3s ease;
}

.sidebar-section {
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.sidebar-footer {
    margin-top: auto;
}

.nav-item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 12px;
    border-radius: var(--radius);
    color: var(--text-secondary);
    text-decoration: none;
    font-size: 0.9rem;
    transition: all 0.2s;
}

.nav-item:hover {
    background: var(--bg-hover);
    color: var(--text-primary);
}

.nav-item.active {
    background: var(--accent);
    color: white;
}

/* Main Content */
.main-content {
    margin-left: var(--sidebar-width);
    margin-top: var(--header-height);
    padding: 20px;
    min-height: calc(100vh - var(--header-height));
}

.tab-content {
    display: none;
}

.tab-content.active {
    display: block;
}

/* Section Header */
.section-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 20px;
}

.section-header h2 {
    font-size: 1.3rem;
    font-weight: 600;
}

/* Buttons */
.btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 8px 16px;
    border: none;
    border-radius: var(--radius);
    font-family: inherit;
    font-size: 0.875rem;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s;
    white-space: nowrap;
}

.btn-primary {
    background: var(--accent);
    color: white;
}

.btn-primary:hover {
    background: var(--accent-hover);
}

.btn-secondary {
    background: var(--bg-tertiary);
    color: var(--text-primary);
    border: 1px solid var(--border);
}

.btn-secondary:hover {
    background: var(--bg-hover);
}

.btn-danger {
    background: var(--red);
    color: white;
}

.btn-danger:hover {
    background: #dc2626;
}

.btn-sm {
    padding: 5px 10px;
    font-size: 0.8rem;
}

.btn-icon {
    padding: 6px;
    background: none;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    border-radius: 4px;
    transition: all 0.2s;
}

.btn-icon:hover {
    background: var(--bg-hover);
    color: var(--text-primary);
}

.btn-icon.danger:hover {
    color: var(--red);
}

/* Portfolio Summary */
.portfolio-summary {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 12px;
    margin-bottom: 20px;
}

.summary-card {
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.summary-label {
    font-size: 0.8rem;
    color: var(--text-secondary);
}

.summary-value {
    font-size: 1.2rem;
    font-weight: 600;
    font-family: 'IBM Plex Mono', monospace;
}

.summary-value.positive {
    color: var(--green);
}

.summary-value.negative {
    color: var(--red);
}

/* Table */
.table-container {
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    overflow-x: auto;
}

.stock-table {
    width: 100%;
    border-collapse: collapse;
}

.stock-table th {
    padding: 12px 14px;
    text-align: left;
    font-size: 0.8rem;
    font-weight: 500;
    color: var(--text-secondary);
    border-bottom: 1px solid var(--border);
    white-space: nowrap;
    user-select: none;
}

.stock-table td {
    padding: 12px 14px;
    font-size: 0.9rem;
    border-bottom: 1px solid var(--border);
    white-space: nowrap;
}

.stock-table tbody tr:hover {
    background: var(--bg-hover);
    cursor: pointer;
}

.stock-table tbody tr:last-child td {
    border-bottom: none;
}

.text-right {
    text-align: right;
}

.mono {
    font-family: 'IBM Plex Mono', monospace;
}

.text-green {
    color: var(--green);
}

.text-red {
    color: var(--red);
}

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

.empty-state td {
    text-align: center;
    padding: 40px 20px;
}

.empty-message {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 12px;
    color: var(--text-muted);
}

.empty-message p {
    font-size: 0.9rem;
}

/* Chart */
.chart-controls {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 16px;
    margin-bottom: 16px;
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 12px 16px;
}

.chart-stock-selector {
    display: flex;
    align-items: center;
    gap: 8px;
}

.chart-stock-selector label {
    font-size: 0.85rem;
    color: var(--text-secondary);
}

.chart-stock-selector select,
.chart-controls select {
    background: var(--bg-tertiary);
    color: var(--text-primary);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 6px 10px;
    font-family: inherit;
    font-size: 0.85rem;
}

.chart-indicators {
    display: flex;
    flex-wrap: wrap;
    gap: 12px;
}

.chart-status {
    font-size: 0.75rem;
    color: var(--text-secondary);
    background: rgba(107, 114, 128, 0.12);
    border-radius: 6px;
    padding: 4px 10px;
    margin-top: 8px;
    font-family: var(--font-mono);
    word-break: break-all;
    line-height: 1.5;
}

.chart-indicators label {
    display: flex;
    align-items: center;
    gap: 4px;
    font-size: 0.8rem;
    color: var(--text-secondary);
    cursor: pointer;
}

.chart-indicators input[type="checkbox"] {
    accent-color: var(--accent);
}

.chart-wrapper {
    display: flex;
    gap: 16px;
    height: calc(100vh - var(--header-height) - 170px);
    min-height: 400px;
}

.chart-container {
    flex: 1;
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    overflow: hidden;
    min-width: 0;
    position: relative;
}

.chart-sidebar {
    width: 280px;
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 16px;
    overflow-y: auto;
    flex-shrink: 0;
}

.chart-sidebar h3 {
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--text-secondary);
    margin-bottom: 10px;
    margin-top: 16px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.chart-sidebar h3:first-child {
    margin-top: 0;
}

.placeholder-text {
    color: var(--text-muted);
    font-size: 0.85rem;
}

.price-level {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 6px 0;
    border-bottom: 1px solid var(--border);
}

.level-label {
    font-size: 0.8rem;
    color: var(--text-secondary);
}

.level-value {
    font-family: 'IBM Plex Mono', monospace;
    font-size: 0.85rem;
    font-weight: 500;
}

.analysis-row {
    display: flex;
    flex-direction: column;
    gap: 4px;
    margin-bottom: 8px;
}

.analysis-row label {
    font-size: 0.75rem;
    color: var(--text-secondary);
}

.analysis-row input {
    background: var(--bg-tertiary);
    color: var(--text-primary);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 6px 8px;
    font-family: 'IBM Plex Mono', monospace;
    font-size: 0.85rem;
}

.analysis-row input:focus {
    outline: none;
    border-color: var(--accent);
}

/* Strategy */
.strategy-tabs {
    display: flex;
    gap: 4px;
    margin-bottom: 20px;
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 4px;
}

.strategy-tab {
    flex: 1;
    padding: 10px 16px;
    border: none;
    background: none;
    color: var(--text-secondary);
    font-family: inherit;
    font-size: 0.9rem;
    font-weight: 500;
    border-radius: 6px;
    cursor: pointer;
    transition: all 0.2s;
}

.strategy-tab:hover {
    color: var(--text-primary);
    background: var(--bg-hover);
}

.strategy-tab.active {
    background: var(--accent);
    color: white;
}

.strategy-panel {
    display: none;
}

.strategy-panel.active {
    display: block;
}

.strategy-header {
    margin-bottom: 20px;
}

.strategy-header h2 {
    font-size: 1.2rem;
    margin-bottom: 6px;
}

.strategy-header p {
    color: var(--text-secondary);
    font-size: 0.9rem;
}

.strategy-content {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 20px;
}

.checklist {
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 16px;
}

.checklist h3 {
    font-size: 0.9rem;
    margin-bottom: 12px;
    color: var(--text-primary);
}

.checklist-item {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 0;
    border-bottom: 1px solid var(--border);
}

.checklist-item:last-child {
    border-bottom: none;
}

.checklist-item input[type="checkbox"] {
    accent-color: var(--green);
    width: 16px;
    height: 16px;
}

.checklist-item label {
    font-size: 0.85rem;
    cursor: pointer;
    color: var(--text-primary);
}

.notes-section {
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 16px;
}

.notes-section h3 {
    font-size: 0.9rem;
    margin-bottom: 12px;
}

.notes-section textarea {
    width: 100%;
    min-height: 120px;
    background: var(--bg-tertiary);
    color: var(--text-primary);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 10px;
    font-family: inherit;
    font-size: 0.85rem;
    resize: vertical;
    margin-bottom: 8px;
}

.notes-section textarea:focus {
    outline: none;
    border-color: var(--accent);
}

.position-sizing {
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 16px;
    grid-column: 1 / -1;
}

.position-sizing h3 {
    font-size: 0.9rem;
    margin-bottom: 12px;
}

.calc-row {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 8px;
}

.calc-row label {
    min-width: 140px;
    font-size: 0.85rem;
    color: var(--text-secondary);
}

.calc-row input {
    background: var(--bg-tertiary);
    color: var(--text-primary);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 6px 10px;
    font-family: 'IBM Plex Mono', monospace;
    font-size: 0.85rem;
    width: 160px;
}

.calc-row input:focus {
    outline: none;
    border-color: var(--accent);
}

.calc-result {
    margin-top: 12px;
    padding: 12px;
    background: var(--bg-tertiary);
    border-radius: 6px;
    font-family: 'IBM Plex Mono', monospace;
    font-size: 0.9rem;
    display: none;
}

.calc-result.visible {
    display: block;
}

.swing-rules {
    grid-column: 1 / -1;
}

.swing-rules h3 {
    font-size: 0.9rem;
    margin-bottom: 12px;
}

.rules-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 12px;
}

.rule-card {
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 16px;
    text-align: center;
}

.rule-card h4 {
    font-size: 0.8rem;
    color: var(--text-secondary);
    margin-bottom: 6px;
}

.rule-card p {
    font-size: 1rem;
    font-weight: 600;
    color: var(--accent);
}

/* Settings */
.settings-content {
    max-width: 600px;
}

.settings-content h2 {
    margin-bottom: 24px;
}

.settings-group {
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 20px;
    margin-bottom: 16px;
}

.settings-group h3 {
    font-size: 0.95rem;
    margin-bottom: 16px;
}

/* Form */
.form-group {
    margin-bottom: 14px;
}

.form-group label {
    display: block;
    font-size: 0.8rem;
    color: var(--text-secondary);
    margin-bottom: 6px;
}

.form-group input,
.form-group select {
    width: 100%;
    background: var(--bg-tertiary);
    color: var(--text-primary);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 8px 12px;
    font-family: inherit;
    font-size: 0.9rem;
}

.form-group input:focus {
    outline: none;
    border-color: var(--accent);
}

.form-row {
    display: flex;
    gap: 12px;
}

.form-row .form-group {
    flex: 1;
}

.help-text {
    margin-top: 10px;
    font-size: 0.8rem;
    color: var(--text-muted);
}

.help-text a {
    color: var(--accent);
    text-decoration: none;
}

.help-text code {
    background: var(--bg-tertiary);
    padding: 1px 5px;
    border-radius: 4px;
    font-family: var(--font-mono);
    font-size: 0.75rem;
}

.form-actions {
    display: flex;
    gap: 10px;
    margin-top: 4px;
}

#connectionStatus {
    margin-top: 12px;
    font-size: 0.88rem;
}

#connectionStatus .status-loading {
    color: var(--text-secondary);
}

#connectionStatus .status-ok {
    color: var(--green);
}

#connectionStatus .status-error {
    color: var(--red);
}

.btn:disabled {
    opacity: 0.6;
    cursor: not-allowed;
}

/* Modal */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.6);
    display: none;
    align-items: center;
    justify-content: center;
    z-index: 200;
    padding: 20px;
}

.modal-overlay.active {
    display: flex;
}

.modal {
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: 12px;
    width: 100%;
    max-width: 480px;
    box-shadow: var(--shadow);
}

.modal-sm {
    max-width: 380px;
}

.modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 20px;
    border-bottom: 1px solid var(--border);
}

.modal-header h3 {
    font-size: 1.05rem;
}

.modal-close {
    background: none;
    border: none;
    color: var(--text-secondary);
    font-size: 1.5rem;
    cursor: pointer;
    padding: 0 4px;
    line-height: 1;
}

.modal-close:hover {
    color: var(--text-primary);
}

.modal-body {
    padding: 20px;
}

.modal-footer {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
    padding: 16px 20px;
    border-top: 1px solid var(--border);
}

/* Stock info panel */
.info-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 6px;
}

.info-item {
    display: flex;
    flex-direction: column;
    padding: 4px 0;
}

.info-label {
    font-size: 0.7rem;
    color: var(--text-muted);
}

.info-value {
    font-family: 'IBM Plex Mono', monospace;
    font-size: 0.8rem;
    font-weight: 500;
}

/* File upload */
.file-upload-label {
    cursor: pointer;
}

/* Toast notification */
.toast {
    position: fixed;
    bottom: 20px;
    right: 20px;
    padding: 12px 20px;
    border-radius: var(--radius);
    font-size: 0.85rem;
    z-index: 300;
    animation: slideUp 0.3s ease;
    box-shadow: var(--shadow);
}

.toast.success {
    background: var(--green);
    color: white;
}

.toast.error {
    background: var(--red);
    color: white;
}

.toast.info {
    background: var(--accent);
    color: white;
}

@keyframes slideUp {
    from { transform: translateY(20px); opacity: 0; }
    to { transform: translateY(0); opacity: 1; }
}

/* Responsive - Tablet */
@media (max-width: 1024px) {
    .sidebar {
        transform: translateX(-100%);
    }

    .sidebar.open {
        transform: translateX(0);
    }

    .menu-toggle {
        display: block;
    }

    .main-content {
        margin-left: 0;
    }

    .portfolio-summary {
        grid-template-columns: repeat(2, 1fr);
    }

    .chart-wrapper {
        flex-direction: column;
        height: auto;
    }

    .chart-container {
        height: 450px;
    }

    .chart-sidebar {
        width: 100%;
    }

    .strategy-content {
        grid-template-columns: 1fr;
    }

    .rules-grid {
        grid-template-columns: repeat(2, 1fr);
    }
}

/* Responsive - Mobile */
@media (max-width: 640px) {
    .app-header {
        padding: 0 12px;
    }

    .main-content {
        padding: 12px;
    }

    .portfolio-summary {
        grid-template-columns: 1fr 1fr;
        gap: 8px;
    }

    .summary-card {
        padding: 12px;
    }

    .summary-value {
        font-size: 1rem;
    }

    .section-header {
        flex-direction: column;
        align-items: flex-start;
        gap: 10px;
    }

    .stock-table th,
    .stock-table td {
        padding: 8px 10px;
        font-size: 0.8rem;
    }

    .chart-controls {
        flex-direction: column;
        align-items: flex-start;
    }

    .chart-indicators {
        gap: 8px;
    }

    .chart-container {
        height: 350px;
    }

    .strategy-tabs {
        flex-direction: column;
    }

    .form-row {
        flex-direction: column;
        gap: 0;
    }

    .calc-row {
        flex-direction: column;
        align-items: flex-start;
    }

    .calc-row input {
        width: 100%;
    }

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

/* 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);
}

/* Loading spinner */
.spinner {
    display: inline-block;
    width: 16px;
    height: 16px;
    border: 2px solid var(--text-muted);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 0.6s linear infinite;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

.loading-overlay {
    position: absolute;
    inset: 0;
    background: rgba(15, 17, 23, 0.8);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 10;
    border-radius: var(--radius);
}

/* Badge */
.badge {
    display: inline-block;
    padding: 2px 8px;
    border-radius: 12px;
    font-size: 0.7rem;
    font-weight: 600;
}

.badge-set {
    background: rgba(59, 130, 246, 0.2);
    color: #60a5fa;
}

.badge-mai {
    background: rgba(139, 92, 246, 0.2);
    color: #a78bfa;
}

/* Stock symbol styling */
.stock-symbol {
    font-family: 'IBM Plex Mono', monospace;
    font-weight: 600;
    font-size: 0.95rem;
}

.stock-name-cell {
    color: var(--text-secondary);
    font-size: 0.8rem;
    max-width: 160px;
    overflow: hidden;
    text-overflow: ellipsis;
}
