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

:root {
    --bg: #ffffff;
    --bg-secondary: #f5f5f7;
    --text: #1d1d1f;
    --text-secondary: #86868b;
    --border: #e5e5e7;
    --accent: #007aff;
    --accent-light: #e8f0fe;
    --input-bg: #f0f0f2;
    --card-bg: #ffffff;
    --shadow: 0 1px 3px rgba(0,0,0,0.06);
    --danger: #ff3b30;
    --voice-active: #ff3b30;
    --heat-1: #d4edda;
    --heat-2: #82c991;
    --heat-3: #2da44e;
    --sidebar-w: 240px;
    --safe-bottom: env(safe-area-inset-bottom, 0px);
    --safe-top: env(safe-area-inset-top, 0px);
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #0a0a0a;
        --bg-secondary: #1c1c1e;
        --text: #f5f5f7;
        --text-secondary: #86868b;
        --border: #2c2c2e;
        --accent: #0a84ff;
        --accent-light: #1a2a3a;
        --input-bg: #1c1c1e;
        --card-bg: #1c1c1e;
        --shadow: 0 1px 3px rgba(0,0,0,0.3);
        --heat-1: #0e3a1e;
        --heat-2: #1a6b34;
        --heat-3: #2da44e;
    }
}

/* ── Login Screen ── */

#login-screen {
    position: fixed;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--bg);
    z-index: 1000;
}

#login-screen.hidden { display: none; }

#login-box {
    text-align: center;
    padding: 40px 32px;
    max-width: 320px;
    width: 100%;
}

#login-box h1 {
    font-size: 28px;
    font-weight: 700;
    margin-bottom: 8px;
    letter-spacing: -0.02em;
}

#login-box p {
    font-size: 14px;
    color: var(--text-secondary);
    margin-bottom: 24px;
}

#login-password {
    width: 100%;
    padding: 12px 16px;
    border: 1px solid var(--border);
    border-radius: 10px;
    background: var(--input-bg);
    color: var(--text);
    font-size: 16px;
    outline: none;
    text-align: center;
    margin-bottom: 12px;
}

#login-password:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(0, 122, 255, 0.15);
}

#login-btn {
    width: 100%;
    padding: 12px;
    border: none;
    border-radius: 10px;
    background: var(--accent);
    color: white;
    font-size: 16px;
    font-weight: 600;
    cursor: pointer;
    transition: opacity 0.2s;
}

#login-btn:active {
    opacity: 0.8;
}

#login-error {
    color: var(--danger) !important;
    margin-top: 12px;
    font-size: 13px !important;
}

/* ── Layout ── */

html, body {
    height: 100%;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
    -webkit-font-smoothing: antialiased;
    overflow: hidden;
}

/* ── Layout: left-right split ── */

#app {
    display: flex;
    height: 100%;
}

/* ── Sidebar (calendar) ── */

#sidebar {
    width: var(--sidebar-w);
    flex-shrink: 0;
    border-right: 1px solid var(--border);
    background: var(--bg-secondary);
    padding: calc(16px + var(--safe-top)) 16px 16px;
    display: flex;
    flex-direction: column;
    overflow-y: auto;
}

#sidebar h1 {
    font-size: 18px;
    font-weight: 700;
    letter-spacing: -0.02em;
    margin-bottom: 16px;
}

#mobile-cal-toggle {
    display: none;
}

/* ── Calendar ── */

#calendar {
    flex-shrink: 0;
}

#calendar-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 0 8px;
}

#cal-title {
    font-size: 14px;
    font-weight: 600;
    color: var(--text);
}

#cal-prev, #cal-next {
    background: none;
    border: none;
    color: var(--accent);
    font-size: 22px;
    width: 28px;
    height: 28px;
    border-radius: 6px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.15s;
}

#cal-prev:hover, #cal-next:hover {
    background: var(--input-bg);
}

#calendar-weekdays {
    display: grid;
    grid-template-columns: repeat(7, 1fr);
    text-align: center;
    font-size: 10px;
    color: var(--text-secondary);
    padding-bottom: 4px;
}

#calendar-grid {
    display: grid;
    grid-template-columns: repeat(7, 1fr);
    gap: 2px;
}

.cal-day {
    aspect-ratio: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 11px;
    border-radius: 5px;
    cursor: default;
    color: var(--text-secondary);
    transition: all 0.15s;
}

.cal-day.has-memo {
    cursor: pointer;
    color: var(--text);
    font-weight: 500;
}

.cal-day.heat-1 { background: var(--heat-1); }
.cal-day.heat-2 { background: var(--heat-2); }
.cal-day.heat-3 { background: var(--heat-3); }
.cal-day.heat-2, .cal-day.heat-3 { color: #fff; }

.cal-day.today {
    box-shadow: inset 0 0 0 1.5px var(--accent);
}

.cal-day.selected {
    background: var(--accent) !important;
    color: white !important;
    font-weight: 600;
}

.cal-day.has-memo:active {
    transform: scale(0.88);
}

.cal-day.other-month {
    opacity: 0.2;
}

#cal-selected-hint {
    margin-top: 12px;
    font-size: 13px;
    color: var(--accent);
    font-weight: 500;
    text-align: center;
}

#cal-clear {
    margin-top: 8px;
    background: none;
    border: 1px solid var(--border);
    border-radius: 8px;
    color: var(--text-secondary);
    font-size: 12px;
    padding: 6px 0;
    cursor: pointer;
    transition: all 0.2s;
}

#cal-clear:hover {
    border-color: var(--accent);
    color: var(--accent);
}

/* ── Main content (right side) ── */

#main {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
    height: 100%;
}

#header {
    flex-shrink: 0;
    padding: calc(12px + var(--safe-top)) 16px 10px;
    background: var(--bg);
    border-bottom: 1px solid var(--border);
}

#search-bar {
    position: relative;
}

#search-input {
    width: 100%;
    padding: 8px 32px 8px 12px;
    border: none;
    border-radius: 8px;
    background: var(--input-bg);
    color: var(--text);
    font-size: 15px;
    outline: none;
    transition: background 0.2s;
}

#search-input:focus {
    background: var(--bg-secondary);
    box-shadow: 0 0 0 2px var(--accent);
}

#search-clear {
    position: absolute;
    right: 8px;
    top: 50%;
    transform: translateY(-50%);
    background: var(--text-secondary);
    color: var(--bg);
    border: none;
    border-radius: 50%;
    width: 18px;
    height: 18px;
    font-size: 12px;
    line-height: 18px;
    text-align: center;
    cursor: pointer;
}

/* ── Timeline ── */

#timeline {
    flex: 1;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding: 8px 16px;
}

.date-group {
    margin-bottom: 4px;
}

.date-label {
    font-size: 13px;
    font-weight: 600;
    color: var(--text-secondary);
    padding: 12px 0 6px;
    position: sticky;
    top: 0;
    background: var(--bg);
    z-index: 1;
}

.memo-item {
    background: var(--card-bg);
    border-radius: 12px;
    padding: 12px 14px;
    margin-bottom: 6px;
    box-shadow: var(--shadow);
    border: 1px solid var(--border);
    transition: opacity 0.3s;
    animation: fadeIn 0.25s ease-out;
}

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

.memo-content {
    font-size: 15px;
    line-height: 1.6;
    white-space: pre-wrap;
    word-break: break-word;
}

.memo-meta {
    display: flex;
    align-items: center;
    gap: 6px;
    margin-top: 8px;
    font-size: 12px;
    color: var(--text-secondary);
}

.memo-voice-tag {
    font-size: 11px;
}

.memo-delete {
    margin-left: auto;
    background: none;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    padding: 2px 6px;
    border-radius: 4px;
    font-size: 12px;
    opacity: 0;
    transition: opacity 0.2s, color 0.2s;
}

.memo-item:hover .memo-delete {
    opacity: 1;
}

.memo-delete:hover {
    color: var(--danger);
}

@media (hover: none) {
    .memo-delete {
        opacity: 0.6;
    }
}

.memo-item.deleting {
    opacity: 0;
    transition: opacity 0.3s;
}

#load-more {
    text-align: center;
    padding: 16px 0;
}

#load-more-btn {
    background: var(--input-bg);
    border: none;
    color: var(--accent);
    padding: 8px 24px;
    border-radius: 20px;
    font-size: 14px;
    cursor: pointer;
}

#empty-state {
    text-align: center;
    padding: 60px 20px;
    color: var(--text-secondary);
}

#empty-state p {
    font-size: 16px;
    margin-bottom: 4px;
}

#empty-state .sub {
    font-size: 14px;
}

/* ── Input Bar ── */

#input-bar {
    flex-shrink: 0;
    display: flex;
    align-items: flex-end;
    gap: 8px;
    padding: 10px 12px calc(10px + var(--safe-bottom));
    background: var(--bg);
    border-top: 1px solid var(--border);
}

#voice-btn, #send-btn {
    flex-shrink: 0;
    width: 40px;
    height: 40px;
    border: none;
    border-radius: 50%;
    background: var(--input-bg);
    color: var(--text-secondary);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s, color 0.2s, transform 0.15s;
}

#voice-btn:active, #send-btn:active {
    transform: scale(0.92);
}

#voice-btn.recording {
    background: var(--voice-active);
    color: white;
    animation: pulse 1.5s ease-in-out infinite;
}

@keyframes pulse {
    0%, 100% { box-shadow: 0 0 0 0 rgba(255, 59, 48, 0.4); }
    50% { box-shadow: 0 0 0 8px rgba(255, 59, 48, 0); }
}

#send-btn:not(:disabled) {
    background: var(--accent);
    color: white;
}

#send-btn:disabled {
    cursor: default;
}

#input-wrapper {
    flex: 1;
    position: relative;
}

#memo-input {
    width: 100%;
    padding: 10px 14px;
    border: none;
    border-radius: 20px;
    background: var(--input-bg);
    color: var(--text);
    font-size: 16px;
    font-family: inherit;
    line-height: 1.4;
    resize: none;
    outline: none;
    max-height: 120px;
    overflow-y: auto;
    transition: background 0.2s;
}

#memo-input:focus {
    background: var(--bg-secondary);
    box-shadow: 0 0 0 2px var(--accent);
}

#memo-input::placeholder {
    color: var(--text-secondary);
}

/* ── Mobile: sidebar as overlay ── */

@media (max-width: 680px) {
    #sidebar {
        position: fixed;
        top: 0;
        left: 0;
        bottom: 0;
        width: 260px;
        z-index: 100;
        transform: translateX(-100%);
        transition: transform 0.25s ease;
        padding-top: calc(16px + var(--safe-top));
    }

    #sidebar.open {
        transform: translateX(0);
        box-shadow: 4px 0 20px rgba(0,0,0,0.15);
    }

    #mobile-cal-toggle {
        display: flex;
        position: fixed;
        top: calc(10px + var(--safe-top));
        left: 12px;
        z-index: 50;
        width: 36px;
        height: 36px;
        align-items: center;
        justify-content: center;
        background: var(--input-bg);
        border: 1px solid var(--border);
        border-radius: 8px;
        color: var(--text-secondary);
        cursor: pointer;
    }

    #mobile-cal-toggle.active {
        background: var(--accent);
        color: white;
        border-color: var(--accent);
    }

    #header {
        padding-left: 56px;
    }

    #main {
        width: 100%;
    }

    .sidebar-backdrop {
        position: fixed;
        inset: 0;
        background: rgba(0,0,0,0.3);
        z-index: 99;
    }
}

/* ── Utility ── */

.hidden {
    display: none !important;
}

#timeline::-webkit-scrollbar {
    width: 4px;
}

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

#voice-btn.unsupported {
    opacity: 0.3;
    cursor: default;
}
