/* App Logo Styles */
.app-logo {
    display: flex;
    align-items: center;
    gap: 10px;
    font-size: 1.8rem;
    font-weight: 700;
    color: #2c3e50;
    text-shadow: 1px 1px 2px rgba(0,0,0,0.1);
}

.logo-text {
    background: linear-gradient(135deg, #f39c12 0%, #d35400 100%);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    letter-spacing: -0.5px;
}

body.dark-mode .app-logo {
    color: #ecf0f1;
}

body.dark-mode .logo-text {
    background: linear-gradient(135deg, #f1c40f 0%, #e67e22 100%);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    text-shadow: 0 0 10px rgba(243, 156, 18, 0.3);
}

/* Common styles */
body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
    margin: 0;
}

/* Ensure padding and border are included in element's total width and height */
*, *::before, *::after {
    box-sizing: border-box;
}

/* Dark mode styles */
body.dark-mode {
    background-color: #1a1a1a;
    color: #e0e0e0;
}

/* Hello page styles */
.hello-body {
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
}

.hello-container {
    text-align: center;
    padding: 2rem;
}

.hello-container h1 {
    font-size: 3rem;
    margin: 0;
    text-shadow: 2px 2px 4px rgba(0,0,0,0.2);
}

.hello-container p {
    font-size: 1.2rem;
    margin-top: 1rem;
    opacity: 0.9;
}

.hello-link {
    display: inline-block;
    margin-top: 2rem;
    padding: 12px 24px;
    background: rgba(255, 255, 255, 0.2);
    color: white;
    text-decoration: none;
    border-radius: 6px;
    font-size: 1rem;
    transition: background 0.3s;
}

.hello-link:hover {
    background: rgba(255, 255, 255, 0.3);
}

/* Tickers main page styles */
.tickers-body {
    padding: 1rem;
    background: #f5f5f5;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}

body.dark-mode .tickers-body {
    background: #0d0d0d;
}

.footer-version {
    margin-left: 6px;
    font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
    font-size: 0.95em;
    opacity: 0.75;
    white-space: nowrap;
}

.tickers-container {
    max-width: 100%;
    width: 100%;
    margin: 0 auto;
    background: white;
    border-radius: 8px;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    padding: 1rem;
    display: flex;
    flex-direction: column;
    flex: 1;
    min-height: 0; /* allow the iframe to shrink/grow within the viewport */
}

.app-footer {
    flex-shrink: 0;
    text-align: center;
    margin-top: 12px;
    font-size: 0.8rem;
    color: #888;
    padding-bottom: 20px;
}

body.dark-mode .tickers-container {
    background: #2a2a2a;
    box-shadow: 0 2px 4px rgba(0,0,0,0.5);
}

.tickers-container h1 {
    margin-top: 0;
    margin-bottom: 1rem;
    color: #333;
    display: flex;
    justify-content: space-between;
    align-items: center; /* Changed from flex-start to center for better logo alignment */
    flex-wrap: wrap;
    gap: 1rem;
    flex-shrink: 0;
}

body.dark-mode .tickers-container h1 {
    color: #e0e0e0;
}

.controls {
    display: flex;
    gap: 0.5rem;
    align-items: center;
    flex-wrap: wrap;
}

.controls form {
    display: flex;
    margin: 0;
    padding: 0;
}

.interval-control {
    display: flex;
    gap: 0.5rem;
    align-items: center;
}

.interval-control input {
    width: 80px;
    padding: 6px 8px;
    border: 1px solid #ddd;
    border-radius: 4px;
    font-size: 0.9rem;
}

.interval-control label {
    font-size: 0.9rem;
    color: #666;
}

body.dark-mode .interval-control label {
    color: #b0b0b0;
}

.interval-control input {
    background: white;
    color: #333;
}

body.dark-mode .interval-control input {
    background: #3a3a3a;
    color: #e0e0e0;
    border-color: #555;
}

.status {
    font-size: 0.9rem;
    color: #95a5a6;
}

.status.active {
    color: #27ae60;
}

.status.paused {
    color: #e74c3c;
}

button {
    padding: 8px 16px;
    background-color: #667eea;
    color: white;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    font-size: 0.9rem;
    font-weight: 500;
}

button:hover {
    background-color: #5568d3;
}

button.pause {
    background-color: #e74c3c;
}

button.pause:hover {
    background-color: #c0392b;
}

button.dark-mode-toggle {
    background-color: #555;
    font-size: 0.85rem;
}

button.dark-mode-toggle:hover {
    background-color: #666;
}

button.refresh-btn {
    background-color: #27ae60;
}

button.refresh-btn:hover {
    background-color: #229954;
}

/* Auto-refresh button: keep existing colors but avoid template conditional CSS in inline styles */
.auto-refresh-toggle-btn.on {
    background-color: #0ea5e9 !important;
}

.auto-refresh-toggle-btn.off {
    background-color: #64748b !important;
}

#ticker-table {
    width: 100%;
    border: none;
    flex: 1;
    min-height: 0;
}

/* Table styles */
.table-body {
    margin: 0;
    padding: 0;
    background: white;
    overflow-x: auto; /* Allow horizontal scrolling if table is too wide */
}

body.dark-mode .table-body {
    background: #2a2a2a;
}

.filter-buttons {
    display: flex;
    gap: 8px;
    margin-bottom: 16px;
    padding: 8px 0;
    border-bottom: 2px solid #e0e0e0;
}

/* Table page: give the first tab a bit of breathing room from the left edge */
.table-body > .filter-buttons {
    padding-left: 10px;
    padding-right: 10px;
}

body.dark-mode .filter-buttons {
    border-bottom-color: #444;
}

.filter-btn {
    padding: 8px 16px;
    background-color: #f5f5f5;
    color: #333;
    text-decoration: none;
    border-radius: 4px;
    font-weight: 500;
    transition: all 0.2s ease;
    border: 1px solid #ddd;
}

.filter-btn:hover {
    background-color: #e8e8e8;
    border-color: #bbb;
}

.filter-btn.active {
    background-color: #667eea;
    color: white;
    border-color: #667eea;
}

.filter-btn.active:hover {
    background-color: #5568d3;
    border-color: #5568d3;
}

body.dark-mode .filter-btn {
    background-color: #3a3a3a;
    color: #e0e0e0;
    border-color: #555;
}

body.dark-mode .filter-btn:hover {
    background-color: #4a4a4a;
    border-color: #666;
}

body.dark-mode .filter-btn.active {
    background-color: #667eea;
    color: white;
    border-color: #667eea;
}

body.dark-mode .filter-btn.active:hover {
    background-color: #5568d3;
    border-color: #5568d3;
}

/* Table styles (scoped to the main tickers table iframe only)
   NOTE: Screener and other pages have their own table CSS and should not inherit
   the fixed min-width column rules below. */
.table-body table {
    width: 100%;
    border-collapse: collapse;
    table-layout: auto; /* Changed to auto to accommodate variable number of columns */
}

.table-body th,
.table-body td {
    padding: 6px 10px;
    text-align: left;
    border-bottom: 1px solid #ddd;
    white-space: nowrap; /* Prevent text wrapping */
}

/* Base columns (always present) */
.table-body th:first-child,
.table-body td:first-child {
    min-width: 120px; /* Ticker column */
}

.table-body th:nth-child(2),
.table-body td:nth-child(2) {
    min-width: 150px; /* Current Price column */
}

.table-body th:nth-child(3),
.table-body td:nth-child(3) {
    min-width: 100px; /* My Avg Price column */
}

.table-body th:nth-child(4),
.table-body td:nth-child(4) {
    min-width: 170px; /* My Avg Price Change column */
}

.table-body th:nth-child(5),
.table-body td:nth-child(5) {
    min-width: 130px; /* Daily Change column */
}

/* Stock metric columns (when stocks filter is active) */
.table-body th:nth-child(6),
.table-body td:nth-child(6),
.table-body th:nth-child(7),
.table-body td:nth-child(7),
.table-body th:nth-child(8),
.table-body td:nth-child(8),
.table-body th:nth-child(9),
.table-body td:nth-child(9),
.table-body th:nth-child(10),
.table-body td:nth-child(10),
.table-body th:nth-child(11),
.table-body td:nth-child(11),
.table-body th:nth-child(12),
.table-body td:nth-child(12),
.table-body th:nth-child(13),
.table-body td:nth-child(13) {
    min-width: 120px; /* Stock metric columns */
    text-align: center;
}

.table-body.dark-mode th,
.table-body.dark-mode td {
    border-bottom-color: #444;
}

.table-body.dark-mode td {
    color: #e0e0e0;
}

.table-body th {
    background-color: #667eea;
    color: white;
    font-weight: 600;
    cursor: pointer;
    user-select: none;
}

.table-body th:hover {
    background-color: #5568d3;
}

.table-body th a {
    color: white;
    text-decoration: none;
    display: block;
}

.sort-indicator {
    margin-left: 5px;
    font-size: 0.8em;
}

.table-body tr:hover {
    background-color: #f9f9f9;
}

.table-body.dark-mode tr:hover {
    background-color: #3a3a3a;
}

.price-cell {
    font-weight: 600;
    color: #2c3e50;
    padding: 0;
    margin: 0;
}

body.dark-mode .price-cell {
    color: #e0e0e0;
}

.price-iframe {
    width: 100%;
    height: 30px;
    border: none;
    display: block;
    overflow: hidden;
    background-color: transparent;
}

.change-cell,
.avg-change-cell {
    padding: 0;
    margin: 0;
}

.change-iframe {
    width: 100%;
    height: 30px;
    border: none;
    display: block;
    overflow: hidden;
    background-color: transparent;
}

.change-body {
    margin: 0;
    padding: 4px 8px;
    font-weight: 500;
    animation: fadeIn 0.3s ease-in;
    background-color: transparent !important;
}

body.dark-mode .change-body {
    color: #e0e0e0;
}

.metric-cell {
    padding: 0;
    margin: 0;
    text-align: center;
    min-width: 80px;
}

.metric-iframe {
    width: 100%;
    height: 30px;
    border: none;
    display: block;
    overflow: hidden;
    background-color: transparent;
}

.metric-body {
    margin: 0;
    padding: 4px 6px;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    font-size: 0.9rem;
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100%;
    background: transparent !important;
}

body.dark-mode .metric-body {
    background: transparent;
}

.metric-value {
    color: #333;
    font-weight: 500;
}

body.dark-mode .metric-value {
    color: #e0e0e0;
}

.no-data {
    color: #999;
    font-style: italic;
}

body.dark-mode .no-data {
    color: #666;
}

/* Price cell styles */
.price-body {
    margin: 0;
    padding: 4px 8px;
    font-weight: 600;
    color: #2c3e50;
    animation: fadeIn 0.3s ease-in;
    background-color: transparent !important;
}

body.dark-mode .price-body {
    color: #e0e0e0;
}

.price-display {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.price-value {
    font-size: 1em;
}

.price-change {
    font-size: 0.85em;
    font-weight: 500;
    display: flex;
    align-items: center;
    gap: 3px;
}

.price-change.positive {
    color: #27ae60;
}

.price-change.negative {
    color: #e74c3c;
}

.price-change .arrow {
    font-size: 0.9em;
}

@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

.error {
    color: #e74c3c;
    font-size: 0.9em;
}

.no-data {
    color: #95a5a6;
    font-style: italic;
}

.loading {
    color: #95a5a6;
    font-style: italic;
}

/* Add/Remove ticker styles */
.add-ticker-form {
    margin-bottom: 1rem;
    padding: 0.75rem;
    background: #f9f9f9;
    border-radius: 4px;
}

body.dark-mode .add-ticker-form {
    background: #333;
}

body.dark-mode .error-message {
    background: #422 !important;
    color: #f88 !important;
    border-color: #633 !important;
    border-color: #633 !important;
}

.remove-ticker-btn {
    background: #e74c3c !important; /* Changed to red */
    color: white !important;
    border: none !important;
    border-radius: 3px !important;
    padding: 2px 6px !important;
    font-size: 0.75rem !important;
    cursor: pointer !important;
    line-height: 1.2 !important;
    transition: background 0.2s;
    opacity: 0.7;
}

.remove-ticker-btn:hover {
    background: #c0392b !important; /* Darker red on hover */
    opacity: 1;
}

/* Modal styles */
.modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.7);
    display: none;
    justify-content: center;
    align-items: center;
    z-index: 1000;
    backdrop-filter: blur(5px);
}

.modal-content {
    background: white;
    padding: 0;
    border-radius: 8px;
    width: 98%; /* Increased from 95% */
    max-width: none; /* Removed max-width limit */
    height: 95vh; /* Increased from 90vh */
    position: relative;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

body.dark-mode .modal-content {
    background: #2a2a2a;
    border: 1px solid #444;
}

.modal-header {
    padding: 10px 20px;
    background: #f5f5f5;
    border-bottom: 1px solid #ddd;
    display: flex;
    justify-content: space-between; /* Changed from flex-end to space-between to align title and close button */
    align-items: center; /* Vertically center items */
    flex-shrink: 0; /* Prevent header from shrinking */
}

body.dark-mode .modal-header {
    background: #2a2a2a;
    border-bottom-color: #444;
    color: #e0e0e0;
}

/* ... existing code ... */

.modal-body {
    flex: 1;
    position: relative;
    overflow: hidden; /* Contain the iframe */
}

.modal-iframe {
    width: 100%;
    height: 100%; /* Fill the modal body */
    border: none;
    display: block; /* Remove inline spacing */
}

/* Responsive design */
@media (min-width: 768px) {
    .tickers-body {
        padding: 2rem;
    }

    .tickers-container {
        max-width: 1400px;
        padding: 2rem;
        height: calc(100vh - 4rem);
    }

    #ticker-table {
        flex: 1;
        min-height: 0;
    }

    /* Keep desktop layout unchanged even if we add mobile-only wrappers in the templates */
    .controls-row,
    .controls-meta-line,
    .time-slot-controls,
    .refresh-controls {
        display: contents;
    }
}

@media (min-width: 1200px) {
    .tickers-container {
        max-width: 1600px;
        height: calc(100vh - 4rem);
    }
}

@media (max-width: 767px) {
    .tickers-container h1 {
        flex-direction: column;
        align-items: stretch;
    }

    .tickers-container h1 > span {
        margin-bottom: 0.5rem;
    }

    /* Scope to dashboard only (Screener also has a .controls block) */
    .tickers-container .controls {
        flex-direction: column;
        align-items: stretch;
        gap: 0.5rem;
    }

    /* Mobile header layout (aligned, standard button sizes) */
    .controls-row {
        width: 100%;
    }

    .controls-row.controls-primary {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 8px;
    }

    .controls-row.controls-meta {
        display: grid;
        grid-template-columns: 1fr;
        gap: 8px;
    }

    .controls-meta-line {
        display: flex;
        justify-content: center;
        align-items: center;
        gap: 8px;
        width: 100%;
        flex-wrap: wrap;
    }

    .controls-row.controls-user {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 8px;
    }

    .controls-row.controls-user .user-label {
        grid-column: 1 / -1;
        text-align: center;
        margin: 0 !important; /* override inline margins on mobile */
    }

    .controls-row.controls-meta .last-update {
        display: block;
        text-align: center;
    }

    .tickers-container .controls button,
    .tickers-container .controls a {
        width: 100% !important;
        min-height: 40px;
        padding: 10px 12px !important;
        font-size: 0.95rem !important;
        margin: 0 !important; /* override inline margins */
        box-sizing: border-box;
        display: flex;
        align-items: center;
        justify-content: center;
        text-align: center;
        line-height: 1.1;
    }

    #dark-mode-form {
        width: 100%;
    }

    .interval-control {
        width: 100%;
    }

    .interval-control input {
        flex: 1;
    }

    .tickers-container button {
        width: 100%;
    }

    /* Mobile main page tool buttons */
    .add-ticker-form {
        flex-direction: column !important;
        align-items: stretch !important;
    }

    .add-ticker-form > div {
        width: 100%;
        margin-left: 0 !important;
    }

    #search_ticker_btn {
        width: 100% !important;
        min-height: 44px;
        padding: 12px 14px !important;
        font-size: 1rem !important;
    }

    #top-tools-buttons.top-tools-buttons {
        display: grid !important;
        grid-template-columns: 1fr 1fr;
        gap: 8px;
        width: 100%;
    }

    #top-tools-buttons.top-tools-buttons button {
        width: 100% !important;
        min-height: 44px;
        padding: 10px 12px !important;
        font-size: 0.95rem !important;
    }

    .filter-buttons {
        width: 100%;
    }

    .filter-buttons .time-slot-controls {
        display: grid;
        grid-template-columns: repeat(4, minmax(0, 1fr));
        gap: 8px;
        width: 100%;
    }

    .filter-buttons .time-slot-controls > span {
        grid-column: 1 / -1;
        text-align: center;
    }

    .filter-buttons .time-slot-controls .filter-btn {
        min-width: 0;
        padding: 10px 0 !important;
        font-size: 0.95rem !important;
        display: flex;
        align-items: center;
        justify-content: center;
        min-height: 38px;
        box-sizing: border-box;
    }

    .filter-buttons .refresh-controls {
        display: grid;
        grid-template-columns: 1fr;
        gap: 8px;
        width: 100%;
        margin-top: 4px;
    }

    .filter-buttons .refresh-controls form {
        width: 100%;
    }

    .filter-buttons .refresh-controls button {
        width: 100% !important;
        min-height: 44px;
        padding: 10px 12px !important;
        font-size: 0.95rem !important;
    }

    .filter-btn {
        flex: 1;
        min-width: calc(50% - 4px);
        text-align: center;
    }

    .table-body table {
        font-size: 0.9rem;
    }

    .table-body th,
    .table-body td {
        padding: 8px;
    }

    /* Mobile: make the tickers table iframe take more vertical space */
    #ticker-table {
        flex: 1 1 auto;
        min-height: 70vh;
    }
}

@media (max-width: 480px) {
    .tickers-body {
        padding: 0.5rem;
    }

    .tickers-container {
        padding: 0.75rem;
        border-radius: 4px;
        height: auto;
        min-height: calc(100vh - 1rem);
    }

    /* Small mobile: even more room for the table */
    #ticker-table {
        min-height: 75vh;
    }

    .tickers-container h1 {
        font-size: 1.25rem;
    }

    .filter-btn {
        min-width: calc(50% - 4px);
        padding: 6px 12px;
        font-size: 0.85rem;
    }

    .table-body table {
        font-size: 0.8rem;
    }

    .table-body th,
    .table-body td {
        padding: 6px;
    }

    .table-body th:first-child,
    .table-body td:first-child {
        width: 30%;
    }

    .table-body th:nth-child(2),
    .table-body td:nth-child(2) {
        width: 35%;
    }

    .table-body th:nth-child(3),
    .table-body td:nth-child(3) {
        width: 35%;
    }
}

/* Ensure chart modal appears above screener modal */
#chart-modal {
    z-index: 1001;
}

/* If a child page inside the chart iframe opens its own modal, hide parent close actions.
   This avoids having two competing "Close" buttons on screen (parent + child). */
#chart-modal.child-modal-open #close-chart-modal-btn,
#chart-modal.child-modal-open #refresh-chart-modal-btn {
    display: none !important;
}
