@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap');

:root {
    /* Color Palette */
    --primary-color: #2563eb;
    --primary-hover: #1d4ed8;
    --secondary-color: #64748b;
    --success-color: #10b981;
    --success-hover: #059669;
    --danger-color: #ef4444;
    --danger-hover: #dc2626;
    --warning-color: #f59e0b;
    --background-start: #f3f4f6;
    --background-end: #e5e7eb;
    --card-bg: #ffffff;
    --text-main: #1f2937;
    --text-muted: #6b7280;
    --border-color: #e5e7eb;
    --input-bg: #f9fafb;

    /* Theme Specific Vars */
    --plot-bg: #fafafa;
    --input-focus-bg: #ffffff;
    --detail-item-bg: #ffffff;
    --option-icon-bg: #ffffff;
    
    --start-coords-bg: #fffbeb;
    --start-coords-border: #fcd34d;
    --start-coords-text: #92400e;
    
    --error-bg: #fef2f2;
    --error-border: #fee2e2;
    --remove-btn-bg: #fee2e2;
    
    /* Spacing & Radius */
    --radius-sm: 0.375rem;
    --radius-md: 0.5rem;
    --radius-lg: 1rem;
    --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
    --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
    --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
}

[data-theme="dark"] {
    --primary-color: #3b82f6;
    --primary-hover: #60a5fa;
    --secondary-color: #94a3b8;
    
    /* Cursor IDE Look */
    --background-start: #1e1e1e;
    --background-end: #1e1e1e;
    --card-bg: #252526;
    --text-main: #cccccc;
    --text-muted: #858585;
    --border-color: #454545;
    --input-bg: #3c3c3c;
    
    --plot-bg: #1e1e1e;
    --input-focus-bg: #444444;
    --detail-item-bg: #2d2d2d;
    --option-icon-bg: #2d2d2d;
    
    --start-coords-bg: #3c3c3c;
    --start-coords-border: #555555;
    --start-coords-text: #e5e5e5;
    
    --error-bg: #450a0a;
    --error-border: #7f1d1d;
    --remove-btn-bg: #450a0a;

    --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.3);
    --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.3), 0 2px 4px -1px rgba(0, 0, 0, 0.2);
    --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.3), 0 4px 6px -2px rgba(0, 0, 0, 0.2);
}

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

body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
    background: linear-gradient(135deg, var(--background-start) 0%, var(--background-end) 100%);
    color: var(--text-main);
    min-height: 100vh;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: flex-start;
    padding: 2rem;
    line-height: 1.5;
    transition: background 0.3s ease, color 0.3s ease;
}

.container {
    width: 100%;
    max-width: 1000px;
    margin: 0 auto;
}

/* Specific container width for CLOSE module */
.container-close-module {
    max-width: 1600px;
}

/* Card Styles */
.calculator-card, .start-card {
    background: var(--card-bg);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-lg);
    padding: 2.5rem;
    animation: fadeIn 0.5s ease-out;
    width: 100%;
    transition: background 0.3s ease, box-shadow 0.3s ease;
}

.start-card {
    max-width: 900px;
    margin: 0 auto;
}

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

/* Typography */
h1 {
    color: var(--text-main);
    font-size: 2rem;
    font-weight: 700;
    margin-bottom: 0.5rem;
    text-align: center;
    letter-spacing: -0.025em;
}

h2 {
    font-size: 1.5rem;
    font-weight: 600;
    color: var(--text-main);
}

h3 {
    font-size: 1.1rem;
    font-weight: 600;
    color: var(--text-main);
    margin-bottom: 1rem;
}

.subtitle {
    color: var(--text-muted);
    font-size: 1rem;
    text-align: center;
    margin-bottom: 2.5rem;
}

/* Links */
.back-link {
    display: inline-flex;
    align-items: center;
    color: var(--secondary-color);
    text-decoration: none;
    font-size: 0.9rem;
    font-weight: 500;
    margin-bottom: 1.5rem;
    transition: color 0.2s;
}

.back-link:hover {
    color: var(--primary-color);
}

/* Forms */
.form-group {
    margin-bottom: 1.5rem;
}

label {
    display: block;
    color: var(--text-main);
    font-weight: 500;
    font-size: 0.9rem;
    margin-bottom: 0.5rem;
}

input[type="text"],
input[type="password"],
input[type="number"] {
    width: 100%;
    padding: 0.75rem 1rem;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    font-size: 1rem;
    background: var(--input-bg);
    color: var(--text-main);
    transition: all 0.2s;
    font-family: inherit;
}

input[type="text"]:focus,
input[type="password"]:focus,
input[type="number"]:focus {
    outline: none;
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
    background: var(--input-focus-bg);
}

/* Buttons */
.calculate-btn, .action-btn, .add-leg-btn, .back-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0.75rem 1.5rem;
    border: none;
    border-radius: var(--radius-md);
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
    text-decoration: none;
}

.calculate-btn {
    width: 100%;
    background-color: var(--primary-color);
    color: white;
    margin-top: 1rem;
}

.calculate-btn:hover {
    background-color: var(--primary-hover);
    transform: translateY(-1px);
}

.calculate-btn:active {
    transform: translateY(0);
}

/* Start Page Options */
.options-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
    gap: 1.5rem;
}

.option-card {
    background: var(--input-bg);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    padding: 2rem;
    text-decoration: none;
    text-align: center;
    transition: all 0.3s;
    display: flex;
    flex-direction: column;
    align-items: center;
    height: 100%;
}

.option-card:hover {
    transform: translateY(-4px);
    box-shadow: var(--shadow-md);
    border-color: var(--primary-color);
    background: var(--input-focus-bg);
}

.option-icon {
    font-size: 2.5rem;
    margin-bottom: 1rem;
    background: var(--option-icon-bg);
    width: 4rem;
    height: 4rem;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    box-shadow: var(--shadow-sm);
}

.option-card h2 {
    font-size: 1.25rem;
    margin-bottom: 0.5rem;
}

.option-card p {
    color: var(--text-muted);
    font-size: 0.9rem;
    line-height: 1.5;
}

/* Results */
.result-container {
    margin-top: 2rem;
    padding: 1.5rem;
    background: var(--input-bg);
    border-radius: var(--radius-md);
    border: 1px solid var(--border-color);
    min-height: 80px;
    display: flex;
    align-items: center;
    justify-content: center;
    text-align: center;
}

.result-placeholder {
    color: var(--text-muted);
    font-style: italic;
}

.result-success {
    width: 100%;
}

.result-main {
    font-size: 1.25rem;
    font-weight: 600;
    color: var(--primary-color);
    margin-bottom: 1rem;
}

.result-details {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 1rem;
}

.detail-item {
    background: var(--detail-item-bg);
    padding: 0.75rem 1.25rem;
    border-radius: var(--radius-sm);
    border: 1px solid var(--border-color);
    color: var(--text-main);
    font-weight: 500;
    box-shadow: var(--shadow-sm);
}

.result-error {
    color: var(--danger-color);
    font-weight: 500;
    padding: 1rem;
    background: var(--error-bg);
    border-radius: var(--radius-md);
    border: 1px solid var(--error-border);
    width: 100%;
}

/* History Section (DMS Calculator) */
.history-section {
    margin-top: 3rem;
    padding-top: 2rem;
    border-top: 1px solid var(--border-color);
}

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

.clear-btn {
    background: transparent;
    border: none;
    color: var(--text-muted);
    font-size: 0.85rem;
    cursor: pointer;
    text-decoration: underline;
    padding: 0;
}

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

.history-list {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
    max-height: 300px;
    overflow-y: auto;
}

.history-item {
    background: var(--input-bg);
    padding: 1rem;
    border-radius: var(--radius-sm);
    border-left: 3px solid var(--primary-color);
    font-size: 0.9rem;
    color: var(--text-main);
}

.history-meta {
    display: flex;
    justify-content: space-between;
    color: var(--text-muted);
    font-size: 0.8rem;
    margin-bottom: 0.25rem;
}

.history-calc {
    font-family: 'Roboto Mono', monospace;
    font-weight: 500;
    color: var(--text-main);
}

/* CLOSE Module Specifics */
.legs-container {
    margin-bottom: 1.5rem;
}

.leg-item {
    background: var(--input-bg);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    padding: 1rem;
    margin-bottom: 1rem;
    display: grid;
    grid-template-columns: 1fr 1fr auto;
    gap: 1rem;
    align-items: end;
}

.leg-item label {
    font-size: 0.8rem;
    margin-bottom: 0.25rem;
    color: var(--text-muted);
}

.remove-leg-btn {
    padding: 0.75rem;
    background: var(--remove-btn-bg);
    color: var(--danger-color);
    border: none;
    border-radius: var(--radius-md);
    cursor: pointer;
    transition: all 0.2s;
}

.remove-leg-btn:hover {
    background: var(--danger-color);
    color: white;
}

.add-leg-btn {
    width: 100%;
    background-color: var(--success-color);
    color: white;
    margin-bottom: 1.5rem;
}

.add-leg-btn:hover {
    background-color: var(--success-hover);
}

.start-coords {
    background: var(--start-coords-bg);
    border: 1px solid var(--start-coords-border);
    border-radius: var(--radius-md);
    padding: 1.5rem;
    margin-bottom: 1.5rem;
}

.start-coords h3 {
    color: var(--start-coords-text);
    margin-bottom: 1rem;
}

.start-coords-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1rem;
}

.action-buttons {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
    gap: 1rem;
    margin-top: 1.5rem;
}

.action-btn {
    color: white;
}

.action-btn.misclose {
    background: var(--primary-color);
}
.action-btn.misclose:hover {
    background: var(--primary-hover);
}

.action-btn.area {
    background: var(--success-color);
}
.action-btn.area:hover {
    background: var(--success-hover);
}

.action-btn.coords {
    background: var(--secondary-color);
}
.action-btn.coords:hover {
    background: #475569;
}

.result-table {
    width: 100%;
    border-collapse: collapse;
    margin-top: 1rem;
    font-size: 0.9rem;
}

.result-table th {
    text-align: left;
    padding: 0.75rem;
    background: var(--input-bg);
    color: var(--text-muted);
    font-weight: 600;
    border-bottom: 2px solid var(--border-color);
}

.result-table td {
    padding: 0.75rem;
    border-bottom: 1px solid var(--border-color);
    color: var(--text-main);
}

/* Updated Layout for CLOSE Module */
.input-plot-container {
    display: grid;
    grid-template-columns: 1fr;
    gap: 2rem;
    margin-bottom: 1.5rem;
    align-items: start;
}

.plot-container {
    background: var(--card-bg);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    padding: 1.5rem;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
}

#traversePlot {
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    background: var(--plot-bg);
    width: 100%;
    aspect-ratio: 1;
    margin-bottom: 1rem;
}

@media (min-width: 992px) {
    .input-plot-container {
        grid-template-columns: 400px 1fr; /* Fixed width for inputs, rest for plot */
        gap: 3rem;
    }
    
    .plot-container {
        position: sticky;
        top: 2rem;
        height: calc(100vh - 6rem);
        max-height: 800px;
        min-height: 500px;
    }
    
    #traversePlot {
        width: 100%;
        /* Removed height: 100% to prevent overflow */
        max-width: none;
        aspect-ratio: auto;
        flex-grow: 1;
        min-height: 0; /* Allow shrinking in flex container */
    }
}

.plot-legend {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 1rem;
    font-size: 0.8rem;
    color: var(--text-muted);
    margin-top: 1rem;
}

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

.legend-color {
    width: 12px;
    height: 12px;
    border-radius: 2px;
}

/* DMS Calculator Specifics */
.operation-selector {
    display: flex;
    gap: 1.5rem;
    margin: 1.5rem 0;
    padding: 1rem;
    background: var(--input-bg);
    border-radius: var(--radius-md);
}

.radio-label {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    cursor: pointer;
    font-weight: 500;
}

/* Theme Toggle Button */
.theme-toggle-btn {
    position: fixed;
    bottom: 20px;
    right: 20px;
    width: 50px;
    height: 50px;
    border-radius: 50%;
    background: var(--card-bg);
    border: 1px solid var(--border-color);
    color: var(--text-main);
    font-size: 1.5rem;
    cursor: pointer;
    box-shadow: var(--shadow-lg);
    z-index: 1000;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.3s ease;
}

.theme-toggle-btn:hover {
    transform: scale(1.1);
    border-color: var(--primary-color);
}

/* Responsive */
@media (max-width: 768px) {
    body {
        padding: 1rem;
        padding-top: calc(1rem + env(safe-area-inset-top));
        padding-bottom: calc(5rem + env(safe-area-inset-bottom));
    }

    input, select, textarea, button {
        font-size: 16px;
    }

    .calculator-card, .start-card {
        padding: 1.5rem;
    }

    .app-bar-inner {
        flex-direction: column;
        align-items: stretch;
        gap: 0.75rem;
    }

    .app-bar-actions {
        justify-content: space-between;
    }

    .input-plot-container {
        grid-template-columns: 1fr;
    }

    .leg-item {
        grid-template-columns: 1fr;
    }

    .start-coords-grid {
        grid-template-columns: 1fr;
    }

    .result-details {
        flex-direction: column;
        width: 100%;
    }

    .detail-item {
        width: 100%;
        text-align: center;
    }
}

/* App bar */
.app-bar {
    position: sticky;
    top: 0;
    z-index: 900;
    width: 100%;
    max-width: 1600px;
    margin: 0 0 1rem;
    padding: 0;
}

.app-bar-inner {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    background: var(--card-bg);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-sm);
    padding: 0.75rem 1rem;
}

.app-bar-title {
    color: var(--text-main);
    font-weight: 600;
    text-decoration: none;
}

.app-bar-actions {
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.app-bar-user {
    color: var(--text-muted);
    font-size: 0.9rem;
}

.logout-form {
    margin: 0;
}

.logout-btn {
    background: transparent;
    border: 1px solid var(--border-color);
    color: var(--text-main);
    border-radius: var(--radius-sm);
    padding: 0.45rem 0.75rem;
    cursor: pointer;
    font-size: 0.875rem;
}

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

/* Flash messages */
.flash-messages {
    width: 100%;
    max-width: 1000px;
    margin: 0 0 1rem;
    padding: 0;
}

.flash {
    border-radius: var(--radius-md);
    padding: 0.75rem 1rem;
    margin-bottom: 0.5rem;
}

.flash-error {
    background: var(--error-bg);
    border: 1px solid var(--error-border);
    color: var(--danger-color);
}

.flash-info {
    background: var(--input-bg);
    border: 1px solid var(--border-color);
    color: var(--text-main);
}

/* Login */
.login-card {
    max-width: 420px;
    margin: 0 auto;
}

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

.remember-label {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    margin: 0.75rem 0 1.25rem;
    color: var(--text-muted);
    font-size: 0.95rem;
}

.field-spaced {
    margin-top: 1rem;
}

.result-highlight {
    font-size: 1.2em;
    text-align: center;
}

.legend-traverse {
    background-color: #667eea;
}

.legend-close {
    background-color: #e74c3c;
}

.legend-start {
    color: #27ae60;
    font-weight: bold;
}
