/* 
  CSS Variables & Theme
*/
:root {
    --primary: #B13B28;
    --primary-hover: #9A3322;
    --bg-color: #F8F4ED;
    --surface: #FFFFFF;
    --surface-hover: #F0EBE1;
    --text-main: #363532;
    --text-muted: #8C8A84;
    --border: rgba(0, 0, 0, 0.08);

    --nav-bg: #2B5F75;

    --jpy-color: #ec4899;
    --jpy-bg: rgba(236, 72, 153, 0.1);
    --twd-color: #10b981;
    --twd-bg: rgba(16, 185, 129, 0.1);

    --danger: #ef4444;
    --safe-area-bottom: env(safe-area-inset-bottom, 0px);
    --nav-height: calc(70px + var(--safe-area-bottom));
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    -webkit-tap-highlight-color: transparent;
}

html,
body {
    height: var(--vh, 100vh);
    margin: 0;
    padding: 0;
    overflow: hidden;
}

html {
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    text-rendering: optimizeLegibility;
}

body {
    font-family: system-ui, -apple-system, 'Inter', BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    background-color: var(--nav-bg); /* Use nav color as base to hide keyboard gaps */
    color: var(--text-main);
    line-height: 1.6;
}

/* 
  Views & Layouts 
*/
/* Loading Screen */
.loading-screen {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: var(--bg-color);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 9999;
    opacity: 0;
    visibility: hidden;
    transition: all 0.3s ease;
}

.loading-screen.active {
    opacity: 1;
    visibility: visible;
}

.loader-content {
    text-align: center;
}

.spinner {
    width: 40px;
    height: 40px;
    border: 4px solid rgba(177, 59, 40, 0.1);
    border-top: 4px solid var(--primary);
    border-radius: 50%;
    animation: spin 1s linear infinite;
    margin: 0 auto 16px;
}

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

.view-container {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: var(--vh, 100vh);
    background-color: var(--bg-color); /* Content area stays beige */
}

.view-container.active {
    display: flex;
    flex-direction: column;
}

/* 
  Auth View 
*/
#auth-view {
    justify-content: center;
    align-items: center;
    padding: 20px;
    background: var(--bg-color);
}

.auth-card {
    background: rgba(255, 255, 255, 0.85);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    border: 1px solid rgba(0, 0, 0, 0.05);
    border-radius: 24px;
    padding: 40px 30px;
    width: 100%;
    max-width: 400px;
    box-shadow: 0 15px 35px rgba(0, 0, 0, 0.05);
    transform: translateY(0);
    animation: fadeUp 0.6s cubic-bezier(0.16, 1, 0.3, 1);
}

@keyframes fadeUp {
    from {
        opacity: 0;
        transform: translateY(20px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.auth-header {
    text-align: center;
    margin-bottom: 30px;
}

.auth-header h1 {
    font-size: 2.5rem;
    font-weight: 800;
    color: var(--primary);
    margin-bottom: 8px;
}

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

.input-group {
    position: relative;
    margin-bottom: 20px;
}

.input-group i {
    position: absolute;
    left: 16px;
    top: 50%;
    transform: translateY(-50%);
    color: var(--text-muted);
}

.input-group input {
    width: 100%;
    background: rgba(0, 0, 0, 0.03);
    border: 1px solid var(--border);
    border-radius: 16px;
    padding: 16px 16px 16px 48px;
    color: var(--text-main);
    font-size: 1rem;
    outline: none;
    transition: all 0.3s ease;
}

.input-group input:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 4px rgba(99, 102, 241, 0.1);
}

.error-msg {
    color: var(--danger);
    font-size: 0.875rem;
    margin-top: 10px;
    text-align: center;
    min-height: 20px;
}

/* 
  Buttons 
*/
.btn-primary {
    width: 100%;
    background: var(--primary);
    color: #FFFFFF;
    border: none;
    border-radius: 16px;
    padding: 16px;
    font-size: 1.1rem;
    font-weight: 600;
    cursor: pointer;
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 10px;
    transition: all 0.2s ease;
}

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

.btn-danger {
    width: 100%;
    background: rgba(239, 68, 68, 0.1);
    color: var(--danger);
    border: 1px solid rgba(239, 68, 68, 0.2);
    border-radius: 16px;
    padding: 16px;
    font-size: 1.1rem;
    font-weight: 600;
    cursor: pointer;
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 10px;
    transition: all 0.2s ease;
}

/* 
  Main App Tabs 
*/
.tab-content {
    display: none;
    flex: 1;
    overflow-y: auto;
    padding-bottom: calc(var(--nav-height) + 40px);
}

.tab-content.active {
    display: block;
    animation: fadeIn 0.4s ease;
}

@keyframes fadeIn {
    from {
        opacity: 0;
    }

    to {
        opacity: 1;
    }
}

/* Headers */
.glass-header {
    position: sticky;
    top: 0;
    z-index: 10;
    background: rgba(43, 95, 117, 0.95);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    padding: calc(20px + env(safe-area-inset-top)) 24px 20px 24px;
    border-bottom: none;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
}

.glass-header h2 {
    font-size: 1.8rem;
    font-weight: 700;
    color: #FFFFFF;
    margin-bottom: 4px;
}

.glass-header p {
    color: rgba(255, 255, 255, 0.8);
    font-size: 0.9rem;
}

/* Dashboard Scroll Area */
/* Segmented Control */
.segmented-control {
    display: flex;
    background: rgba(0, 0, 0, 0.08);
    padding: 3px;
    border-radius: 12px;
    margin-top: 16px;
    width: 100%;
}
.segmented-item {
    flex: 1;
    border: none;
    background: transparent;
    padding: 8px;
    font-size: 0.9rem;
    font-weight: 600;
    color: rgba(255, 255, 255, 0.6);
    cursor: pointer;
    border-radius: 9px;
    transition: all 0.3s cubic-bezier(0.16, 1, 0.3, 1);
}
.segmented-item.active {
    background: #FFFFFF;
    color: var(--nav-bg);
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.dashboard-scroll,
.itinerary-scroll,
.expenses-scroll,
.settings-container {
    padding: 20px 24px;
}

/* Typography & Utils */
.section-title {
    font-size: 1.2rem;
    font-weight: 600;
    margin-bottom: 16px;
    color: var(--text-main);
}

.mt-4 {
    margin-top: 32px;
}

/* 
  Cards 
*/
.trip-card {
    background: var(--surface);
    border-radius: 16px;
    padding: 20px;
    margin-bottom: 16px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.04);
    border: 1px solid rgba(0, 0, 0, 0.03);
    position: relative;
    overflow: hidden;
}

.trip-card::before {
    content: '';
    position: absolute;
    left: 0;
    top: 0;
    width: 4px;
    height: 100%;
    background: var(--primary);
    border-radius: 4px 0 0 4px;
}

.trip-card.ongoing::before {
    background: var(--twd-color);
}

.trip-time {
    color: var(--text-muted);
    font-size: 0.9rem;
    margin-bottom: 8px;
    font-weight: 500;
    display: flex;
    align-items: center;
    gap: 6px;
}

.trip-place {
    font-size: 1.4rem;
    font-weight: 700;
    margin-bottom: 8px;
}

.trip-notes {
    color: var(--text-muted);
    font-size: 0.95rem;
    margin-bottom: 16px;
    line-height: 1.5;
}

.map-btn {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    background: rgba(177, 59, 40, 0.1);
    color: var(--primary);
    padding: 10px 16px;
    border-radius: 12px;
    font-size: 0.9rem;
    font-weight: 600;
    text-decoration: none;
}

.empty-state {
    padding: 30px;
    text-align: center;
    color: var(--text-muted);
    background: rgba(0, 0, 0, 0.02);
    border-radius: 16px;
    border: 1px dashed var(--border);
}

/* 
  Itinerary Timeline
*/
.timeline-date-group {
    margin-bottom: 32px;
}

.timeline-date-title {
    font-size: 1.1rem;
    font-weight: 700;
    color: var(--primary);
    margin-bottom: 16px;
    position: sticky;
    top: 80px;
    background: var(--bg-color);
    padding: 8px 0;
    z-index: 5;
}

.timeline-item {
    display: flex;
    gap: 16px;
    margin-bottom: 24px;
    position: relative;
}

.timeline-item:not(:last-child)::after {
    content: '';
    position: absolute;
    left: 23px;
    top: 40px;
    bottom: -20px;
    width: 2px;
    background: var(--border);
}

.timeline-time {
    min-width: 50px;
    font-weight: 600;
    color: var(--text-muted);
    font-size: 0.9rem;
    padding-top: 4px;
}

.timeline-content {
    background: var(--surface);
    padding: 16px;
    border-radius: 16px;
    flex: 1;
    border: 1px solid rgba(0, 0, 0, 0.04);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.02);
}

/* 
  Expenses 
*/
.expense-summary-card {
    display: flex;
    background: var(--surface);
    border-radius: 16px;
    padding: 24px;
    margin-bottom: 24px;
    border: 1px solid rgba(0, 0, 0, 0.04);
    box-shadow: 0 8px 20px rgba(0, 0, 0, 0.05);
}

.summary-item {
    flex: 1;
    text-align: center;
}

.summary-item span {
    font-size: 0.85rem;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 1px;
}

.summary-item h3 {
    font-size: 1.6rem;
    font-weight: 800;
    margin-top: 8px;
}

.summary-item.jpy h3 {
    color: var(--jpy-color);
}

.summary-item.twd h3 {
    color: var(--twd-color);
}

.summary-divider {
    width: 1px;
    background: var(--border);
    margin: 0 20px;
}

.card-form {
    background: var(--surface);
    padding: 20px;
    border-radius: 16px;
    margin-bottom: 24px;
    border: 1px solid rgba(0, 0, 0, 0.04);
    box-shadow: 0 8px 20px rgba(0, 0, 0, 0.03);
}

.card-form h3 {
    margin-bottom: 16px;
    font-size: 1.1rem;
}

.card-form input[type="text"],
.card-form input[type="number"],
.card-form input[type="url"],
.card-form input[type="datetime-local"],
.card-form select {
    width: 100%;
    height: 50px;
    background: var(--bg-color);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 0 14px;
    color: var(--text-main);
    font-size: 1rem;
    margin-bottom: 12px;
    outline: none;
    -webkit-appearance: none;
    appearance: none;
}

.card-form select {
    background-image: url("data:image/svg+xml;charset=US-ASCII,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%22292.4%22%20height%3D%22292.4%22%3E%3Cpath%20fill%3D%22%2394a3b8%22%20d%3D%22M287%2069.4a17.6%2017.6%200%200%200-13-5.4H18.4c-5%200-9.3%201.8-12.9%205.4A17.6%2017.6%200%200%200%200%2082.2c0%205%201.8%209.3%205.4%2012.9l128%20127.9c3.6%203.6%207.8%205.4%2012.8%205.4s9.2-1.8%2012.8-5.4L287%2095c3.5-3.5%205.4-7.8%205.4-12.8%200-5-1.9-9.2-5.5-12.8z%22%2F%3E%3C%2Fsvg%3E");
    background-repeat: no-repeat;
    background-position: right 14px top 50%;
    background-size: 12px auto;
}

.card-form input:focus,
.card-form select:focus {
    border-color: var(--primary);
}

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

.card-form .row>* {
    flex: 1;
}

.toggle-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: rgba(0, 0, 0, 0.02);
    border: 1px solid var(--border);
    padding: 16px;
    border-radius: 16px;
    margin-bottom: 20px;
    cursor: pointer;
}

.toggle-row span {
    font-size: 1rem;
    color: var(--text-main);
    font-weight: 500;
}

.toggle-wrapper {
    position: relative;
    width: 54px;
    height: 32px;
}

.toggle-wrapper input {
    opacity: 0;
    width: 0;
    height: 0;
    position: absolute;
}

.toggle-bg {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: rgba(0, 0, 0, 0.1);
    border-radius: 32px;
    transition: 0.3s;
}

.toggle-knob {
    position: absolute;
    height: 26px;
    width: 26px;
    left: 3px;
    bottom: 3px;
    background-color: white;
    border-radius: 50%;
    transition: 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

.toggle-wrapper input:checked+.toggle-bg {
    background-color: var(--primary);
}

.toggle-wrapper input:checked~.toggle-knob {
    transform: translateX(22px);
}

.expense-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: var(--surface);
    padding: 16px;
    border-radius: 16px;
    margin-bottom: 12px;
    border: 1px solid rgba(0, 0, 0, 0.04);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.02);
}

.expense-info h4 {
    font-size: 1rem;
    margin-bottom: 4px;
}

.expense-info p {
    font-size: 0.8rem;
    color: var(--text-muted);
}

.expense-amount {
    font-size: 1.2rem;
    font-weight: 700;
}

.expense-amount.jpy {
    color: var(--jpy-color);
}

.expense-amount.twd {
    color: var(--twd-color);
}

/* Settings */
.profile-card {
    display: flex;
    align-items: center;
    gap: 16px;
    background: var(--surface);
    padding: 24px;
    border-radius: 16px;
    margin-bottom: 32px;
    border: 1px solid rgba(0, 0, 0, 0.04);
    box-shadow: 0 8px 20px rgba(0, 0, 0, 0.03);
}

.avatar {
    width: 60px;
    height: 60px;
    border-radius: 50%;
    background: rgba(177, 59, 40, 0.1);
    color: var(--primary);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.5rem;
}

/* 
  Bottom Navigation 
*/
.bottom-nav {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    width: 100%;
    /* Use dynamic height with safe area */
    height: calc(65px + env(safe-area-inset-bottom, 0px));
    background: rgba(43, 95, 117, 0.98);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    display: flex;
    justify-content: space-around;
    align-items: flex-start; /* Align items to top of nav */
    padding-top: 10px;
    padding-bottom: env(safe-area-inset-bottom, 0px);
    border-top: 1px solid rgba(255, 255, 255, 0.1);
    z-index: 100;
    box-shadow: 0 -4px 20px rgba(0, 0, 0, 0.15);
}

.nav-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    color: rgba(255, 255, 255, 0.5);
    text-decoration: none;
    flex: 1;
    height: 100%;
    transition: all 0.3s ease;
}

.nav-item i {
    font-size: 1.4rem;
    margin-bottom: 4px;
    transition: transform 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

.nav-item span {
    font-size: 0.7rem;
    font-weight: 500;
}

.nav-item.active {
    color: #FFFFFF;
}

.nav-item.active i {
    transform: translateY(-2px) scale(1.1);
}

/* PWA Toast */
.pwa-toast {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    width: 100%;
    background: var(--nav-bg);
    color: white;
    padding: 16px 24px calc(16px + var(--safe-area-bottom)) 24px;
    border-radius: 20px 20px 0 0;
    /* Rounded top corners only */
    display: none;
    /* Truly hide from rendering */
    align-items: center;
    justify-content: space-between;
    box-shadow: 0 -10px 25px rgba(0, 0, 0, 0.1);
    z-index: 1000;
    animation: slideUp 0.5s cubic-bezier(0.16, 1, 0.3, 1) forwards;
}

.pwa-toast.show {
    display: flex;
}

@keyframes slideUp {
    from {
        transform: translateY(150%);
        opacity: 0;
    }

    to {
        transform: translateY(0);
        opacity: 1;
    }
}

.pwa-toast button {
    background: rgba(255, 255, 255, 0.2);
    border: none;
    color: white;
    padding: 8px 16px;
    border-radius: 8px;
    font-weight: 600;
}

#pwa-close-btn {
    background: transparent;
    padding: 8px;
    font-size: 1.2rem;
}

/* Modal Overlay */
.modal-overlay {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    background: rgba(0, 0, 0, 0.6);
    z-index: 2000;
    justify-content: center;
    align-items: center;
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
}

.modal-overlay.active {
    display: flex;
    animation: fadeIn 0.3s ease;
}

.modal-content {
    background: var(--surface);
    border-radius: 16px;
    padding: 24px;
    width: 90%;
    max-width: 400px;
    border: 1px solid rgba(0, 0, 0, 0.05);
    box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
    max-height: 90vh;
    overflow-y: auto;
}

.modal-content input[type="datetime-local"] {
    width: 100%;
    background: var(--bg-color);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 14px;
    color: var(--text-main);
    font-size: 1rem;
    margin-bottom: 12px;
    outline: none;
}

/* Manage Trip Item */
.manage-trip-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: rgba(0, 0, 0, 0.02);
    border: 1px solid var(--border);
    padding: 16px;
    border-radius: 16px;
    margin-bottom: 12px;
}

.manage-trip-item .trip-info h4 {
    font-size: 1.1rem;
    margin-bottom: 4px;
}

.manage-trip-item .trip-info p {
    font-size: 0.85rem;
    color: var(--text-muted);
}

.manage-trip-actions {
    display: flex;
    gap: 8px;
}

.manage-trip-actions button {
    background: var(--surface-hover);
    color: var(--text-main);
    border: none;
    width: 36px;
    height: 36px;
    border-radius: 10px;
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: pointer;
}

.manage-trip-actions .btn-delete {
    color: var(--danger);
    background: rgba(239, 68, 68, 0.1);
}