/* --- General Resets & Typography --- */
:root {
    --primary-color: #4f46e5; /* Indigo 600 */
    --primary-hover-color: #4338ca; /* Indigo 700 */
    --danger-color: #ef4444; /* Red 500 */
    --danger-hover-color: #dc2626; /* Red 600 */

    /* Light Mode Colors */
    --background-primary: #ffffff;
    --background-secondary: #f8fafc; /* Slate 50 */
    --background-tertiary-hover: #f1f5f9; /* Slate 100 */
    --text-primary: #0f172a; /* Slate 900 */
    --text-secondary: #64748b; /* Slate 500 */
    --border-color: #e2e8f0; /* Slate 200 */
    --shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.05), 0 4px 6px -2px rgba(0, 0, 0, 0.025);
    --shadow-hover: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
    --non-expiring-background: #f3f4f6; /* Gray 100 */
    --non-expiring-background-hover: #e5e7eb; /* Gray 200 */

    --border-radius: 0.5rem;
}

body.dark-mode {
    --background-primary: #0f172a; /* Slate 900 */
    --background-secondary: #1e293b; /* Slate 800 */
    --background-tertiary-hover: #334155; /* Slate 700 */
    --text-primary: #f8fafc; /* Slate 50 */
    --text-secondary: #94a3b8; /* Slate 400 */
    --border-color: #334155; /* Slate 700 */
    --shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.4), 0 4px 6px -2px rgba(0, 0, 0, 0.2);
    --shadow-hover: 0 20px 25px -5px rgba(0, 0, 0, 0.5), 0 10px 10px -5px rgba(0, 0, 0, 0.3);
    --non-expiring-background: #374151; /* Gray 700 */
    --non-expiring-background-hover: #4b5563; /* Gray 600 */
}

body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    line-height: 1.5;
    background-color: var(--background-secondary);
    color: var(--text-primary);
    margin: 0;
    display: grid;
    grid-template-rows: 64px 1fr; /* Top nav height and the rest of the content */
    grid-template-columns: 1fr;
    height: 100vh;
    overflow: hidden;
}

/* --- Main Layout --- */
.top-nav {
    grid-row: 1 / 2;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 1.5rem;
    background-color: rgba(255, 255, 255, 0.7);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border-bottom: 1px solid var(--border-color);
    position: relative; /* This is needed for z-index to work */
    z-index: 10; /* Keep it above other content */
}

body.dark-mode .top-nav {
    background-color: rgba(15, 23, 42, 0.7);
}

.nav-left {
    display: flex;
    align-items: center;
    gap: 1.5rem;
}

.nav-right {
    display: flex;
    align-items: center;
    gap: 1.5rem;
}

.top-nav .app-title {
    font-size: 1.5rem;
    font-weight: 700;
    letter-spacing: -0.025em;
    margin: 0;
    border: none;
    background: linear-gradient(90deg, var(--primary-color), #818cf8);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
}

.top-nav-menu {
    display: flex;
    align-items: center;
    height: 100%;
    margin-left: 1rem;
}

.top-nav-menu .nav-item {
    padding: 0.4rem 1rem;
    color: var(--text-secondary);
    text-decoration: none;
    font-weight: 500;
    display: flex;
    align-items: center;
    border-radius: var(--border-radius);
    transition: all 0.2s ease;
    margin-right: 0.25rem;
}

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

.top-nav-menu .nav-item.active {
    color: var(--primary-color);
    background-color: rgba(79, 70, 229, 0.1);
}

body.dark-mode .top-nav-menu .nav-item.active {
    background-color: rgba(79, 70, 229, 0.2);
}

.top-nav-menu .nav-item.disabled {
    color: #adb5bd; /* A lighter gray for disabled items */
    cursor: not-allowed;
}

.app-layout {
    grid-row: 2 / 3;
    display: grid;
    grid-template-columns: 210px 1fr;
    overflow: hidden; /* Prevent the whole layout from scrolling */
}

.sidebar {
    background-color: var(--background-primary);
    padding: 1.5rem;
    border-right: 1px solid var(--border-color);
    overflow-y: auto; /* Make sidebar scrollable */
}

.main-content {
    padding: 2rem;
    overflow-y: auto; /* Make main content scrollable */
}

.main-content-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1.5rem;
}

.main-content-header h2 {
    margin-bottom: 0;
    border-bottom: none;
}

.positions-filter-container {
    display: flex;
    align-items: center;
    gap: 1rem;
    margin-left: 1.5rem;
    margin-right: auto;
}

/* Main container */
.container {
    max-width: 100%;
    margin: auto;
    background: var(--background-primary);
    padding: 2rem;
    border-radius: var(--border-radius);
    box-shadow: var(--shadow);
}

h1, h2 {
    color: var(--text-primary);
    border-bottom: 1px solid var(--border-color);
    padding-bottom: 0.5rem;
    margin-bottom: 1.5rem;
}

h1 {
    font-size: 2.5rem;
}

h2 {
    font-size: 1.75rem;
}

/* --- Sidebar Header & Context --- */
.profile-container {
    position: relative;
}

.profile-icon {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background-color: var(--primary-color);
    color: var(--background-primary);
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 1.25rem;
    font-weight: bold;
    cursor: pointer;
    user-select: none; /* Prevents text selection on click */
}

.profile-icon:focus {
    outline: 2px solid var(--primary-hover-color);
    outline-offset: 2px;
}

.profile-dropdown {
    position: absolute;
    top: 100%;
    right: 0;
    background-color: var(--background-primary);
    border: 1px solid var(--border-color);
    border-radius: var(--border-radius);
    box-shadow: var(--shadow);
    min-width: 150px;
    z-index: 1000;
    margin-top: 0.5rem;
}

.profile-dropdown a {
    display: block;
    padding: 0.75rem 1rem;
    color: var(--text-primary);
    text-decoration: none;
    transition: background-color 0.2s;
}

.profile-dropdown a:hover {
    background-color: var(--background-secondary);
}

.profile-dropdown .dropdown-divider {
    height: 1px;
    margin: 0.5rem 0;
    overflow: hidden;
    background-color: var(--border-color);
}

.theme-toggle-container {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.75rem 1rem;
}

.theme-toggle-container label {
    color: var(--text-primary);
    font-weight: 500;
    cursor: pointer;
}

.theme-switch {
    position: relative;
    display: inline-block;
    width: 50px;
    height: 26px;
}

/* Hide default HTML checkbox */
.theme-switch input {
    opacity: 0;
    width: 0;
    height: 0;
}

/* The slider */
.slider {
    position: absolute;
    cursor: pointer;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: var(--background-tertiary-hover);
    transition: .4s;
}

.slider:before {
    position: absolute;
    content: "";
    height: 20px;
    width: 20px;
    left: 3px;
    bottom: 3px;
    background-color: white;
    transition: .4s;
}

input:checked + .slider {
    background-color: var(--primary-color);
}

input:checked + .slider:before {
    transform: translateX(24px);
}

/* Rounded sliders */
.slider.round {
    border-radius: 26px;
}

.slider.round:before {
    border-radius: 50%;
}

.add-account-form {
    display: flex;
    gap: 0.5rem;
}

.add-account-form input {
    flex-grow: 1;
    min-width: 0; /* Prevents input from overflowing flex container */
}

.add-account-form button {
    flex-shrink: 0;
}

/* --- Top Nav Account Selector --- */
.account-nav-section {
    position: relative;
}

.account-display {
    display: flex;
    align-items: center;
    gap: 1rem;
    background-color: var(--background-secondary);
    padding: 0.25rem 1rem;
    border-radius: var(--border-radius);
    cursor: pointer;
    border: 1px solid var(--border-color);
    transition: background-color 0.2s;
}

.account-display-summary {
    display: flex;
    align-items: center;
    gap: 1rem;
}

.summary-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    line-height: 1.2;
}

.summary-label {
    font-size: 0.7rem;
    font-weight: 500;
    color: var(--text-secondary);
    text-transform: uppercase;
}

body.dark-mode .summary-label {
    color: #ccc; /* A lighter gray for dark mode, as requested */
}

.summary-value {
    font-size: 1.1rem;
    font-weight: bold;
    color: #28a745; /* Green */
}

.summary-divider {
    width: 1px;
    align-self: stretch;
    background-color: var(--border-color);
}

.account-display-name-container {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding-left: 1rem;
    border-left: 1px solid var(--border-color);
}

.account-display:hover {
    background-color: var(--background-tertiary-hover);
}

.account-display:focus {
    outline: 2px solid var(--primary-hover-color);
    outline-offset: 2px;
}

#current-account-name {
    font-weight: 500;
}

.dropdown-arrow {
    font-size: 0.7rem;
}

.account-dropdown {
    position: absolute;
    top: 100%;
    right: 0;
    background-color: var(--background-primary);
    border: 1px solid var(--border-color);
    border-radius: var(--border-radius);
    box-shadow: var(--shadow);
    min-width: 250px;
    z-index: 1000;
    margin-top: 0.5rem;
    padding-top: 0.5rem;
    padding-bottom: 0.5rem;
}

.account-dropdown .account-item {
    display: block;
    padding: 0.75rem 1rem;
    color: var(--text-primary);
    text-decoration: none;
    cursor: pointer;
    transition: background-color 0.2s;
}

.account-dropdown .account-item:hover {
    background-color: var(--background-secondary);
}

.account-dropdown .add-account-container {
    padding: 0.75rem 1rem;
    border-top: 1px solid var(--border-color);
    margin-top: 0.5rem;
}

/* --- Sidebar Navigation --- */
.sidebar ul {
    list-style: none;
    padding: 0;
    margin: 0;
}

.sidebar li a {
    display: block;
    padding: 0.6rem 1rem;
    color: var(--text-secondary);
    text-decoration: none;
    border-radius: 0.5rem;
    font-weight: 500;
    transition: all 0.2s ease;
    margin-bottom: 0.25rem;
}

.sidebar li a:hover {
    background-color: var(--background-secondary);
    transform: translateX(4px);
}

.sidebar li a.active {
    background-color: var(--primary-color);
    color: #ffffff;
    box-shadow: var(--shadow);
}

/* --- Form Styling --- */
.form-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
    gap: 1rem;
    align-items: end;
    margin-bottom: 2rem;
}

.form-grid .form-group {
    display: flex;
    flex-direction: column;
}

.form-grid label {
    margin-bottom: 0.25rem;
    font-weight: 500;
    font-size: 0.875rem;
}

input, select, button {
    font-family: inherit;
    font-size: 0.9rem;
    padding: 0.6rem 1rem;
    border-radius: var(--border-radius);
    border: 1px solid var(--border-color);
    transition: all 0.2s ease-in-out;
}

input:focus, select:focus {
    border-color: var(--primary-color);
    outline: 0;
    box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.2);
}

button {
    cursor: pointer;
    color: #ffffff;
    background-color: var(--primary-color);
    border-color: var(--primary-color);
    font-weight: 500;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

button:hover {
    background-color: var(--primary-hover-color);
    border-color: var(--primary-hover-color);
    transform: translateY(-1px);
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

button:active {
    transform: translateY(0);
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}

.form-grid .submit-button {
    grid-column: -2 / -1; /* Place button at the end */
}

/* --- Position Type Selector Modal --- */
.position-type-selector {
    display: flex;
    justify-content: center;
    gap: 1rem;
    margin-top: 2rem;
}

.position-type-selector button {
    padding: 1rem 1.5rem;
    font-size: 1.1rem;
}

/* --- Table Styling --- */
#positions-table {
    width: 100%;
    border-collapse: collapse;
    margin-top: 1rem;
}

#positions-table th, #positions-table td {
    padding: 0.4rem 1rem;
    text-align: left;
    vertical-align: middle;
    border-bottom: 1px solid var(--border-color);
    font-size: 0.875rem;
}

#positions-table thead th {
    background-color: var(--background-secondary);
    font-weight: 600;
    color: var(--text-secondary);
    text-transform: uppercase;
    font-size: 0.75rem;
    letter-spacing: 0.05em;
    border-top: 1px solid var(--border-color);
    border-bottom: 2px solid var(--border-color);
}

.sort-btn {
    background: none;
    border: none;
    color: inherit;
    font: inherit;
    padding: 0;
    margin: 0;
    cursor: pointer;
    text-align: left;
    width: 100%;
}

.sort-btn:hover {
    color: var(--primary-color);
}

.sort-btn::after {
    content: '';
    display: inline-block;
    width: 0;
    height: 0;
    border-left: 5px solid transparent;
    border-right: 5px solid transparent;
    margin-left: 0.5em;
    opacity: 0.4;
}

.sort-btn.asc::after {
    border-bottom: 5px solid var(--text-primary);
    opacity: 1;
}

.sort-btn.desc::after {
    border-top: 5px solid var(--text-primary);
    opacity: 1;
}

#positions-table .expand-cell {
    width: 24px;
    padding-right: 0.5rem;
    padding-left: 0.5rem;
}

.expand-btn {
    background: transparent;
    border: 1px solid var(--border-color);
    color: var(--text-secondary);
    width: 22px;
    height: 22px;
    border-radius: 50%;
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 0;
    font-size: 1.1rem;
    font-weight: bold;
    line-height: 1;
    cursor: pointer;
    transition: background-color 0.2s, color 0.2s, border-color 0.2s;
}

.expand-btn:hover {
    background-color: var(--background-tertiary-hover);
    border-color: var(--text-secondary);
    color: var(--text-primary);
}

/* --- Context Menu Styles --- */
.context-menu {
    position: fixed;
    z-index: 1100; /* Above other elements */
    background-color: var(--background-primary);
    border: 1px solid var(--border-color);
    border-radius: var(--border-radius);
    box-shadow: var(--shadow);
    min-width: 160px;
    padding: 0.5rem 0;
}

.context-menu-item {
    display: block;
    padding: 0.75rem 1rem;
    color: var(--text-primary);
    text-decoration: none;
    cursor: pointer;
    transition: background-color 0.2s;
    border: none;
    background: none;
    width: 100%;
    text-align: left;
    font-size: 0.84rem;
}

.context-menu-item:hover {
    background-color: var(--background-secondary);
}

.context-menu-item.danger {
    color: var(--danger-color);
}

/* --- Status Badge Styling --- */
.status-badge {
    padding: 0.25em 0.6em;
    font-size: 0.8em;
    font-weight: 600;
    border-radius: 1rem;
    color: var(--background-primary);
    text-transform: uppercase;
}

.status-open {
    background-color: #28a745; /* Green */
}

.status-closed {
    background-color: var(--danger-color); /* Red */
}

/* --- Row Styling for Expiration --- */
/* Default style for non-expiring rows is a light teal/dark teal */
#positions-table tbody tr:not(.transaction-detail-row) {
    background-color: var(--non-expiring-background);
    transition: all 0.2s ease;
}

/* Hover for non-expiring rows */
#positions-table tbody tr:not(.transaction-detail-row):hover {
    background-color: var(--non-expiring-background-hover);
    transform: translateY(-1px);
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}

/* Override for rows expiring this week to make them the primary background color (white/black) */
#positions-table tbody tr.expiring-this-week {
    background-color: var(--background-primary);
}

/* Hover for expiring rows uses the standard secondary background color */
#positions-table tbody tr.expiring-this-week:hover {
    background-color: var(--background-secondary);
}

/* Override for all rows in the closed positions view */
.view-closed #positions-table tbody tr:not(.transaction-detail-row) {
    background-color: var(--background-primary);
}

.view-closed #positions-table tbody tr:not(.transaction-detail-row):hover {
    background-color: var(--background-secondary);
}

/* Highlighting for selected rows in any table */
#positions-table tbody tr.selected,
#transactions-table tbody tr.selected {
    background-color: var(--primary-hover-color) !important; /* Use important to override other background rules */
    color: var(--background-primary);
}

#positions-table tbody tr.selected:hover,
#transactions-table tbody tr.selected:hover {
    background-color: var(--primary-color) !important;
}

/* Styling for Call or Stock position rows in the open view */
#positions-table tbody tr.call-or-stock-position {
    color: #0b7e7c; /* A standard blue color */
}
/* Hide the status column header, as the column has been removed */
#positions-table #status-header {
    display: none;
}

.live-price-cell .green { color: #28a745; }
.live-price-cell .skyblue { color: #87CEEB; }
.live-price-cell .red { color: var(--danger-color); }

.itm-cell .green { color: #28a745; }
.itm-cell .skyblue { color: #87CEEB; }
.itm-cell .peach { color: #FFDAB9; }
.itm-cell .orange { color: #FFA500; }
.itm-cell .red { color: var(--danger-color); }

.basis-cell {
    text-align: right;
}
/* --- Modal Styles --- */
.modal {
    display: none;
    position: fixed;
    z-index: 1050;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    overflow: auto;
    background-color: rgba(0, 0, 0, 0.5);
}

.modal-content {
    background-color: var(--background-primary);
    margin: 5% auto;
    padding: 2rem;
    border: 1px solid rgba(0, 0, 0, 0.2);
    width: 90%;
    max-width: 600px;
    border-radius: var(--border-radius);
    position: relative;
    box-shadow: var(--shadow);
}

.close-btn {
    color: #aaa;
    position: absolute;
    top: 1rem;
    right: 1.5rem;
    font-size: 2rem;
    font-weight: bold;
    line-height: 1;
}

.close-btn:hover,
.close-btn:focus {
    color: var(--text-primary);
    text-decoration: none;
    cursor: pointer;
}

#edit-position-form {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1rem;
}

#edit-position-form .form-group {
    display: flex;
    flex-direction: column;
}

#edit-position-form .checkbox-wrapper {
    grid-column: 1 / -1;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.checkbox-input {
    width: auto;
}

#edit-position-form #edit_close_date_wrapper {
    grid-column: 1 / -1;
}

#edit-position-form button[type="submit"] {
    grid-column: 1 / -1;
    justify-self: end;
    width: auto;
    margin-top: 1rem;
    padding: 0.5rem 1.5rem;
}

/* --- Roll Modal Specific Styles --- */
.roll-info {
    background-color: var(--background-secondary);
    padding: 0.75rem 1rem;
    border-radius: var(--border-radius);
    margin-bottom: 1.5rem;
    border: 1px solid var(--border-color);
}

.form-section-header {
    margin-top: 1.5rem;
    margin-bottom: 1rem;
    padding-bottom: 0.5rem;
    border-bottom: 1px solid var(--border-color);
}

.form-section-header h3 {
    margin: 0;
    font-size: 1.2rem;
}

.form-actions {
    display: flex;
    justify-content: flex-end;
    gap: 0.5rem;
    margin-top: 1.5rem;
}

button.btn-secondary {
    background-color: var(--background-secondary);
    color: var(--text-primary);
    border-color: var(--border-color);
}

button.btn-secondary:hover {
    background-color: var(--background-tertiary-hover);
}

button.button-danger {
    background-color: var(--danger-color);
    color: var(--background-primary);
    border-color: var(--danger-color);
}

button.button-danger:hover {
    background-color: var(--danger-hover-color);
    border-color: var(--danger-hover-color);
}

button.button-danger:disabled {
    background-color: var(--text-secondary);
    border-color: var(--text-secondary);
}

/* --- Utility Classes --- */
.hidden {
    display: none !important;
}

#transactions-table th, #transactions-table td {
    padding: 0.4rem 0.75rem;
    border-bottom: 1px solid var(--border-color);
}

/* Sizing for checkbox columns */
#positions-table th:first-child, #positions-table td:first-child {
    width: 2rem;
    text-align: center;
}

/* --- Login Page Styles --- */
.login-container {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
}

.login-box {
    background: var(--background-primary);
    padding: 2rem;
    border-radius: var(--border-radius);
    box-shadow: var(--shadow);
    width: 100%;
    max-width: 400px;
}

.login-box h1, .login-box h2 {
    text-align: center;
    margin-top: 0;
    border-bottom: none;
}

.login-box form {
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.login-box .form-group {
    display: flex;
    flex-direction: column;
}

.form-switch-link {
    text-align: center;
    margin-top: 1.5rem;
    font-size: 0.84rem;
    color: var(--text-secondary);
}

.form-switch-link a {
    color: var(--primary-color);
    text-decoration: none;
    font-weight: 500;
}

.form-switch-link a:hover {
    text-decoration: underline;
}

.error-message, .message.error {
    color: var(--danger-color);
    text-align: center;
    min-height: 1.2em;
}

.message.success {
    color: #28a745;
    text-align: center;
    min-height: 1.2em;
}

.transaction-detail-row {
    background-color: var(--background-secondary);
}

.transaction-detail-row.hidden {
    display: none;
}

.transaction-detail-row td {
    border-bottom: 1px dashed var(--border-color);
    color: var(--text-secondary);
}

/* For the last detail row of a group */
.transaction-detail-row:last-of-type td {
    border-bottom: 1px solid var(--border-color);
}

.detail-indicator-cell {
    text-align: center;
    padding-right: 1rem;
    font-size: 1.2em;
    font-weight: bold;
    color: var(--text-secondary);
}

.transaction-detail-info td {
    text-align: center;
    font-style: italic;
    color: var(--text-secondary);
}

/* For report tables, align the expand button and date text on the same line */
.report-table .expand-cell-with-date {
    display: flex;
    align-items: center;
    gap: 8px; /* Adjust spacing as needed */
}
