/* CSS Variables */
:root {
    --primary: #1a5f7a;
    --primary-dark: #134b61;
    --primary-light: #2d8aa8;
    --secondary: #57c5b6;
    --secondary-dark: #3da99a;
    --accent: #159895;
    --danger: #ef4444;
    --danger-dark: #dc2626;

    --bg-primary: #ffffff;
    --bg-secondary: #f1f5f9;
    --bg-card: #ffffff;
    --text-primary: #1e293b;
    --text-secondary: #64748b;
    --text-muted: #94a3b8;
    --border: #e2e8f0;

    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.05);
    --shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
    --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1);

    --radius-sm: 8px;
    --radius: 12px;
    --radius-lg: 16px;

    --safe-area-top: env(safe-area-inset-top);
    --safe-area-bottom: env(safe-area-inset-bottom);
}

/* Dark Mode */
[data-theme="dark"] {
    --bg-primary: #0f172a;
    --bg-secondary: #1e293b;
    --bg-card: #1e293b;
    --text-primary: #f1f5f9;
    --text-secondary: #94a3b8;
    --text-muted: #64748b;
    --border: #334155;
}

/* Reset & Base */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

html {
    font-size: 16px;
    -webkit-tap-highlight-color: transparent;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
    background-color: var(--bg-secondary);
    color: var(--text-primary);
    line-height: 1.5;
    min-height: 100vh;
    overflow-x: hidden;
}

#app {
    display: flex;
    flex-direction: column;
    min-height: 100vh;
    max-width: 480px;
    margin: 0 auto;
    background-color: var(--bg-primary);
}

/* Header */
.app-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: calc(var(--safe-area-top) + 12px) 16px 12px;
    background: var(--primary);
    color: white;
    position: sticky;
    top: 0;
    z-index: 100;
}

.app-header h1 {
    font-size: 1.25rem;
    font-weight: 600;
}

.icon-btn {
    background: none;
    border: none;
    color: white;
    width: 40px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    border-radius: var(--radius-sm);
    transition: background-color 0.2s;
}

.icon-btn:hover {
    background-color: rgba(255, 255, 255, 0.1);
}

.icon-btn svg {
    width: 24px;
    height: 24px;
}

/* Main Content */
.app-content {
    flex: 1;
    overflow-y: auto;
    padding-bottom: calc(70px + var(--safe-area-bottom));
}

/* Views */
.view {
    display: none;
    padding: 16px;
    animation: fadeIn 0.2s ease-out;
}

.view.active {
    display: block;
}

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

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

.form-group label {
    display: block;
    font-weight: 600;
    margin-bottom: 8px;
    color: var(--text-primary);
}

/* Intensity Slider */
.intensity-display, .mood-display {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 8px;
}

.intensity-display span:first-child,
.mood-display span:first-child {
    font-size: 2rem;
    font-weight: 700;
    color: var(--primary);
    width: 50px;
}

.intensity-display span:last-child,
.mood-display span:last-child {
    font-size: 0.875rem;
    color: var(--text-secondary);
}

.intensity-slider, .mood-slider {
    -webkit-appearance: none;
    width: 100%;
    height: 8px;
    border-radius: 4px;
    background: var(--bg-secondary);
    outline: none;
}

.intensity-slider::-webkit-slider-thumb,
.mood-slider::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 28px;
    height: 28px;
    border-radius: 50%;
    background: var(--primary);
    cursor: pointer;
    box-shadow: var(--shadow);
    transition: transform 0.1s;
}

.intensity-slider::-webkit-slider-thumb:active,
.mood-slider::-webkit-slider-thumb:active {
    transform: scale(1.1);
}

.intensity-scale, .mood-scale {
    display: flex;
    justify-content: space-between;
    margin-top: 4px;
    font-size: 0.75rem;
    color: var(--text-muted);
}

/* Triggers Grid */
.triggers-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 8px;
}

.trigger-item {
    display: flex;
    align-items: center;
    padding: 10px 12px;
    background: var(--bg-secondary);
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: all 0.2s;
    border: 2px solid transparent;
}

.trigger-item:hover {
    background: var(--bg-card);
    border-color: var(--border);
}

.trigger-item.selected {
    background: var(--primary);
    color: white;
    border-color: var(--primary);
}

.trigger-item input {
    display: none;
}

.trigger-item span {
    font-size: 0.875rem;
}

/* Notes Textarea */
textarea {
    width: 100%;
    min-height: 80px;
    padding: 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    font-family: inherit;
    font-size: 1rem;
    resize: vertical;
    background: var(--bg-card);
    color: var(--text-primary);
}

textarea:focus {
    outline: none;
    border-color: var(--primary);
}

/* DateTime Input */
input[type="datetime-local"] {
    width: 100%;
    padding: 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    font-family: inherit;
    font-size: 1rem;
    background: var(--bg-card);
    color: var(--text-primary);
}

input[type="datetime-local"]:focus {
    outline: none;
    border-color: var(--primary);
}

/* Buttons */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 14px 24px;
    font-size: 1rem;
    font-weight: 600;
    border: none;
    border-radius: var(--radius);
    cursor: pointer;
    transition: all 0.2s;
    width: 100%;
}

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

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

.btn-primary:active {
    transform: scale(0.98);
}

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

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

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

.btn-danger:hover {
    background: var(--danger-dark);
}

/* Bottom Navigation */
.bottom-nav {
    position: fixed;
    bottom: 0;
    left: 50%;
    transform: translateX(-50%);
    width: 100%;
    max-width: 480px;
    display: flex;
    background: var(--bg-card);
    border-top: 1px solid var(--border);
    padding-bottom: var(--safe-area-bottom);
    z-index: 100;
}

.nav-btn {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 12px 0;
    background: none;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    transition: color 0.2s;
}

.nav-btn svg {
    width: 24px;
    height: 24px;
    margin-bottom: 4px;
}

.nav-btn span {
    font-size: 0.75rem;
    font-weight: 500;
}

.nav-btn.active {
    color: var(--primary);
}

/* History View */
.history-filters {
    margin-bottom: 16px;
}

.history-filters select {
    width: 100%;
    padding: 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    font-size: 1rem;
    background: var(--bg-card);
    color: var(--text-primary);
}

.history-list {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.history-item {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 16px;
    position: relative;
}

.history-item-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    margin-bottom: 12px;
}

.history-date {
    font-size: 0.875rem;
    color: var(--text-secondary);
}

.history-intensity {
    font-size: 1.5rem;
    font-weight: 700;
    color: var(--primary);
}

.history-item-content {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.history-triggers {
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
}

.history-trigger-tag {
    font-size: 0.75rem;
    padding: 4px 8px;
    background: var(--bg-secondary);
    border-radius: 4px;
    color: var(--text-secondary);
}

.history-mood {
    font-size: 0.875rem;
    color: var(--text-secondary);
}

.history-notes {
    font-size: 0.875rem;
    color: var(--text-secondary);
    font-style: italic;
}

.history-actions {
    display: flex;
    gap: 8px;
    margin-top: 12px;
    padding-top: 12px;
    border-top: 1px solid var(--border);
}

.history-actions button {
    flex: 1;
    padding: 8px;
    font-size: 0.875rem;
}

.empty-state {
    text-align: center;
    padding: 48px 24px;
    color: var(--text-muted);
}

.empty-state svg {
    width: 64px;
    height: 64px;
    margin-bottom: 16px;
    opacity: 0.5;
}

/* Analytics View */
.analytics-period {
    margin-bottom: 16px;
}

.analytics-period select {
    width: 100%;
    padding: 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    font-size: 1rem;
    background: var(--bg-card);
    color: var(--text-primary);
}

.stats-cards {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 12px;
    margin-bottom: 24px;
}

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

.stat-value {
    display: block;
    font-size: 1.5rem;
    font-weight: 700;
    color: var(--primary);
}

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

.chart-container {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 16px;
    margin-bottom: 16px;
}

.chart-container h3 {
    font-size: 0.875rem;
    font-weight: 600;
    margin-bottom: 12px;
    color: var(--text-primary);
}

.chart-container canvas {
    max-height: 200px;
}

/* Settings View */
.settings-section {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 16px;
    margin-bottom: 16px;
}

.settings-section h3 {
    font-size: 1rem;
    font-weight: 600;
    margin-bottom: 12px;
}

.custom-triggers-list {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-bottom: 12px;
}

.custom-trigger-tag {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 6px 12px;
    background: var(--bg-secondary);
    border-radius: var(--radius-sm);
    font-size: 0.875rem;
}

.custom-trigger-tag button {
    background: none;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    padding: 0;
    font-size: 1rem;
    line-height: 1;
}

.custom-trigger-tag button:hover {
    color: var(--danger);
}

.add-trigger-form {
    display: flex;
    gap: 8px;
}

.add-trigger-form input {
    flex: 1;
    padding: 10px 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    font-size: 0.875rem;
    background: var(--bg-primary);
    color: var(--text-primary);
}

.add-trigger-form .btn {
    width: auto;
    padding: 10px 16px;
}

.settings-section .btn {
    margin-bottom: 8px;
}

.settings-section .btn:last-child {
    margin-bottom: 0;
}

.theme-toggle {
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.toggle {
    position: relative;
    width: 50px;
    height: 28px;
    -webkit-appearance: none;
    appearance: none;
    background: var(--bg-secondary);
    border-radius: 14px;
    cursor: pointer;
    transition: background 0.2s;
}

.toggle:checked {
    background: var(--primary);
}

.toggle::before {
    content: '';
    position: absolute;
    top: 2px;
    left: 2px;
    width: 24px;
    height: 24px;
    background: white;
    border-radius: 50%;
    transition: transform 0.2s;
    box-shadow: var(--shadow-sm);
}

.toggle:checked::before {
    transform: translateX(22px);
}

.app-info {
    text-align: center;
}

.app-info p {
    font-size: 0.875rem;
    color: var(--text-muted);
    margin-bottom: 4px;
}

/* Modal */
.modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    z-index: 200;
    align-items: center;
    justify-content: center;
    padding: 16px;
}

.modal.active {
    display: flex;
}

.modal-content {
    background: var(--bg-card);
    border-radius: var(--radius-lg);
    width: 100%;
    max-width: 400px;
    max-height: 90vh;
    overflow-y: auto;
}

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

.modal-header h2 {
    font-size: 1.125rem;
}

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

.modal-content form {
    padding: 16px;
}

.modal-actions {
    display: flex;
    gap: 12px;
    margin-top: 16px;
}

.modal-actions .btn {
    flex: 1;
}

/* Toast */
.toast {
    position: fixed;
    bottom: calc(80px + var(--safe-area-bottom));
    left: 50%;
    transform: translateX(-50%) translateY(100px);
    background: var(--text-primary);
    color: var(--bg-primary);
    padding: 12px 24px;
    border-radius: var(--radius);
    font-size: 0.875rem;
    opacity: 0;
    transition: all 0.3s;
    z-index: 300;
    max-width: calc(100% - 32px);
    text-align: center;
}

.toast.show {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
}

/* Intensity Color Coding */
.intensity-1, .intensity-2 { --intensity-color: #22c55e; }
.intensity-3, .intensity-4 { --intensity-color: #84cc16; }
.intensity-5, .intensity-6 { --intensity-color: #eab308; }
.intensity-7, .intensity-8 { --intensity-color: #f97316; }
.intensity-9, .intensity-10 { --intensity-color: #ef4444; }

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

/* Scrollbar Styling */
::-webkit-scrollbar {
    width: 4px;
}

::-webkit-scrollbar-track {
    background: transparent;
}

::-webkit-scrollbar-thumb {
    background: var(--border);
    border-radius: 2px;
}

/* Focus States for Accessibility */
button:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible {
    outline: 2px solid var(--primary);
    outline-offset: 2px;
}

/* Responsive adjustments for very small screens */
@media (max-width: 350px) {
    .triggers-grid {
        grid-template-columns: 1fr;
    }

    .stats-cards {
        grid-template-columns: 1fr;
    }
}
