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

body {
    font-family:
        -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial,
        sans-serif;
    line-height: 1.4;
    color: #333;
    background-color: #f5f5f5;
    min-height: 100vh;
}

.container {
    max-width: 800px;
    margin: 0 auto;
    padding: 2rem;
}

/* Header */
header {
    background: #fff;
    border-bottom: 1px solid #e0e0e0;
    padding: 1rem 0;
    margin-bottom: 2rem;
}

header .container {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 2rem;
}

header h1 {
    font-size: 1.5rem;
    font-weight: 600;
}

header h1 a {
    color: inherit;
    text-decoration: none;
}

/* Editable document title */
header h1[contenteditable] {
    border-radius: 4px;
    padding: 0.25rem 0.5rem;
    margin: -0.25rem -0.5rem; /* Offset padding so text stays aligned */
    outline: none;
}

header h1[contenteditable]:focus {
    outline: 2px solid #007bff;
    outline-offset: 0;
}

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

header nav a {
    color: #666;
    text-decoration: none;
}

header nav a:hover {
    color: #333;
}

header nav span {
    color: #666;
}

/* User avatar dropdown */
.user-menu {
    position: relative;
    margin-left: auto;
}

.user-avatar {
    width: 2rem;
    height: 2rem;
    border-radius: 50%;
    background: #007bff;
    color: #fff;
    border: none;
    font-size: 0.875rem;
    font-weight: 600;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background-color 0.15s ease;
}

.user-avatar:hover {
    background: #0056b3;
}

.user-dropdown {
    position: absolute;
    top: calc(100% + 0.5rem);
    right: 0;
    background: #fff;
    border: 1px solid #e0e0e0;
    border-radius: 8px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    min-width: 200px;
    opacity: 0;
    visibility: hidden;
    transform: translateY(-0.5rem);
    transition: opacity 0.15s ease, transform 0.15s ease, visibility 0.15s ease;
    z-index: 1000;
}

.user-menu.open .user-dropdown {
    opacity: 1;
    visibility: visible;
    transform: translateY(0);
}

.user-dropdown-header {
    padding: 0.75rem 1rem;
    border-bottom: 1px solid #e0e0e0;
    font-size: 0.875rem;
    color: #666;
    word-break: break-all;
}

.user-dropdown-item {
    display: block;
    padding: 0.75rem 1rem;
    color: #333;
    text-decoration: none;
    transition: background-color 0.15s ease;
}

.user-dropdown-item:hover {
    background: #f5f5f5;
}

/* Main content */
main {
    background: #fff;
    border-radius: 8px;
    padding: 2rem;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

/* Buttons */
.btn {
    display: inline-block;
    padding: 0.75rem 1.5rem;
    border-radius: 6px;
    text-decoration: none;
    font-weight: 500;
    cursor: pointer;
    border: none;
    font-size: 1rem;
    transition: background-color 0.15s ease;
}

.btn-primary {
    background: #007bff;
    color: #fff;
}

.btn-primary:hover {
    background: #0056b3;
}

.btn-secondary {
    background: #fff;
    color: #333;
    border: 1px solid #ddd;
}

.btn-secondary:hover {
    background: #f5f5f5;
}

.btn-danger {
    background: #dc3545;
    color: #fff;
}

.btn-danger:hover {
    background: #c82333;
}

.btn-small {
    padding: 0.5rem 1rem;
    font-size: 0.875rem;
}

.btn-google {
    background: #fff;
    color: #333;
    border: 1px solid #ddd;
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
}

.btn-google:hover {
    background: #f5f5f5;
}

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

.form-group label {
    display: block;
    margin-bottom: 0.5rem;
    font-weight: 500;
}

.form-group input[type="text"],
.form-group input[type="email"],
.form-group input[type="password"] {
    width: 100%;
    padding: 0.75rem;
    border: 1px solid #ddd;
    border-radius: 6px;
    font-size: 1rem;
}

.form-group input[type="text"]:focus,
.form-group input[type="email"]:focus,
.form-group input[type="password"]:focus {
    outline: none;
    border-color: #007bff;
}

.form-group input[type="checkbox"] {
    width: auto;
}

.form-group:has(input[type="checkbox"]) {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.form-group:has(input[type="checkbox"]) label {
    display: inline;
    margin-bottom: 0;
}

/* Messages */
.messages {
    margin-bottom: 1rem;
    list-style: none;
}

.messages li {
    padding: 0.75rem;
    border-radius: 6px;
    margin-bottom: 0.5rem;
}

.messages .success {
    background: #d4edda;
    color: #155724;
}

.messages .error {
    background: #f8d7da;
    color: #721c24;
}

.messages .warning {
    background: #fff3cd;
    color: #856404;
}

.errorlist {
    color: #721c24;
    list-style: none;
    font-size: 0.875rem;
    margin-top: 0.25rem;
}

/* Document list */
.document-list {
    list-style: none;
}

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

.document-list-header h2 {
    font-size: 1.25rem;
    font-weight: 600;
}

.document-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1rem;
    border: 1px solid #e0e0e0;
    border-radius: 6px;
    margin-bottom: 0.75rem;
    background: #fff;
    transition: border-color 0.15s ease;
}

.document-item:hover {
    border-color: #007bff;
}

.document-item a {
    color: #333;
    text-decoration: none;
    font-weight: 500;
    flex: 1;
}

.document-item a:hover {
    color: #007bff;
}

.document-item .document-meta {
    color: #666;
    font-size: 0.875rem;
}

.document-item-actions {
    display: flex;
    gap: 0.5rem;
}

.empty-state {
    text-align: center;
    padding: 3rem 1rem;
    color: #666;
}

.empty-state p {
    margin-bottom: 1rem;
}

/* Editor */
.editor-container {
    margin-bottom: 1rem;
}

.editor-content {
    min-height: 400px;
    border: 1px solid #e0e0e0;
    border-radius: 6px;
    padding: 1rem;
    background: #fff;
}

.editor-content:focus {
    outline: none;
    border-color: #007bff;
}

/* Tiptap editor styles */
.tiptap {
    min-height: 400px;
    outline: none;
}

.tiptap p {
    margin-bottom: 0.25rem;
}

.tiptap ul,
.tiptap ol {
    padding-left: 1.5rem;
    margin-bottom: 0.25rem;
}

.tiptap ul[data-type="taskList"] {
    list-style: none;
    padding-left: 0;
}

.tiptap ul[data-type="taskList"] li {
    display: flex;
    align-items: flex-start;
    gap: 0.5rem;
    margin-bottom: 0.125rem;
}

.tiptap ul[data-type="taskList"] li > label {
    flex-shrink: 0;
    display: flex;
    align-items: center;
    height: 1.5em; /* Match line height for proper alignment */
}

.tiptap ul[data-type="taskList"] li > label input[type="checkbox"] {
    cursor: pointer;
    pointer-events: auto;
}

.tiptap ul[data-type="taskList"] li > div {
    flex: 1;
}

.tiptap ul[data-type="taskList"] li[data-checked="true"] > div {
    text-decoration: line-through;
    color: #666;
}

/* Heading spacing */
.tiptap h1 {
    margin-top: 1rem;
    margin-bottom: 0.25rem;
}

.tiptap h2 {
    margin-top: 0.75rem;
    margin-bottom: 0.25rem;
}

.tiptap h3,
.tiptap h4,
.tiptap h5,
.tiptap h6 {
    margin-top: 0.5rem;
    margin-bottom: 0.125rem;
}

/* First heading should have no top margin */
.tiptap > h1:first-child,
.tiptap > h2:first-child,
.tiptap > h3:first-child,
.tiptap > h4:first-child,
.tiptap > h5:first-child,
.tiptap > h6:first-child {
    margin-top: 0;
}

/* Section collapse/expand */
.tiptap h1,
.tiptap h2,
.tiptap h3,
.tiptap h4,
.tiptap h5,
.tiptap h6 {
    position: relative;
    display: flex;
    align-items: center;
    gap: 0.25rem;
}

.section-toggle {
    width: 1.25rem;
    height: 1.25rem;
    flex-shrink: 0;
    border: none;
    background: transparent;
    cursor: pointer;
    padding: 0;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border-radius: 3px;
    color: #666;
    transition:
        color 0.15s ease,
        background-color 0.15s ease;
}

.section-toggle:hover {
    background: #e0e0e0;
    color: #333;
}

.section-toggle::before {
    content: "";
    width: 0;
    height: 0;
    border-left: 5px solid currentColor;
    border-top: 4px solid transparent;
    border-bottom: 4px solid transparent;
    transition: transform 0.15s ease;
    transform: rotate(90deg);
}

.section-toggle.collapsed::before {
    transform: rotate(0deg);
}

.section-collapsed {
    opacity: 0.7;
}

/* Hidden section content (CSS-based hiding preserves Yjs sync) */
.section-content-hidden,
.tiptap .section-content-hidden,
.tiptap > .section-content-hidden {
    display: none !important;
}

/* Alternative: Use sibling selector from collapsed heading */
/* This works because headings have data-collapsed attribute set by NodeView */
.tiptap
    h1[data-collapsed="true"]
    ~ *:not(h1):not(h2):not(h3):not(h4):not(h5):not(h6),
.tiptap
    h2[data-collapsed="true"]
    ~ *:not(h1):not(h2):not(h3):not(h4):not(h5):not(h6),
.tiptap
    h3[data-collapsed="true"]
    ~ *:not(h1):not(h2):not(h3):not(h4):not(h5):not(h6),
.tiptap
    h4[data-collapsed="true"]
    ~ *:not(h1):not(h2):not(h3):not(h4):not(h5):not(h6),
.tiptap
    h5[data-collapsed="true"]
    ~ *:not(h1):not(h2):not(h3):not(h4):not(h5):not(h6),
.tiptap
    h6[data-collapsed="true"]
    ~ *:not(h1):not(h2):not(h3):not(h4):not(h5):not(h6) {
    display: none !important;
}

/* Collapse All / Expand All toolbar */
.section-toolbar {
    display: flex;
    gap: 0.5rem;
    margin-bottom: 0.5rem;
    padding-bottom: 0.5rem;
    border-bottom: 1px solid #e0e0e0;
}

.section-toolbar button {
    padding: 0.25rem 0.5rem;
    font-size: 0.75rem;
    border: 1px solid #ddd;
    border-radius: 4px;
    background: #fff;
    color: #666;
    cursor: pointer;
    transition:
        background-color 0.15s ease,
        color 0.15s ease;
}

.section-toolbar button:hover {
    background: #f5f5f5;
    color: #333;
}

/* Connection status */
.connection-status {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.875rem;
    cursor: default;
}

.connection-status .status-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
}

.connection-status .status-dot.connected {
    background: #28a745;
}

.connection-status .status-dot.connecting {
    background: #ffc107;
}

.connection-status .status-dot.disconnected {
    background: #dc3545;
}

.connection-status .status-dot.offline {
    background: #6c757d;
}

.connection-status .status-text {
    color: #666;
}

/* Offline banner */
.offline-banner {
    background: #6c757d;
    color: #fff;
    padding: 0.5rem 1rem;
    border-radius: 6px;
    margin-bottom: 1rem;
    font-size: 0.875rem;
    text-align: center;
}

/* Utility classes */
.flex {
    display: flex;
}

.flex-gap {
    gap: 1rem;
}

.mt-1 {
    margin-top: 1rem;
}

.mt-2 {
    margin-top: 2rem;
}

.mb-1 {
    margin-bottom: 1rem;
}

.mb-2 {
    margin-bottom: 2rem;
}

.text-muted {
    color: #666;
}

.text-small {
    font-size: 0.875rem;
}

/* Loading spinner */
.loading {
    display: inline-block;
    width: 1rem;
    height: 1rem;
    border: 2px solid #e0e0e0;
    border-top-color: #007bff;
    border-radius: 50%;
    animation: spin 0.6s linear infinite;
}

@keyframes spin {
    to {
        transform: rotate(360deg);
    }
}

/* Help button */
.btn-help {
    width: 2rem;
    height: 2rem;
    border-radius: 50%;
    border: 1px solid #ddd;
    background: #fff;
    color: #666;
    font-size: 0.875rem;
    font-weight: 600;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    transition:
        background-color 0.15s ease,
        color 0.15s ease,
        border-color 0.15s ease;
}

.btn-help:hover {
    background: #f5f5f5;
    color: #333;
    border-color: #ccc;
}

/* Keyboard shortcuts overlay */
.shortcuts-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    padding: 1rem;
}

.shortcuts-modal {
    background: #fff;
    border-radius: 12px;
    box-shadow: 0 20px 40px rgba(0, 0, 0, 0.2);
    max-width: 600px;
    width: 100%;
    max-height: 80vh;
    display: flex;
    flex-direction: column;
    outline: none;
}

.shortcuts-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1.25rem 1.5rem;
    border-bottom: 1px solid #e0e0e0;
}

.shortcuts-header h2 {
    font-size: 1.25rem;
    font-weight: 600;
    margin: 0;
}

.shortcuts-close {
    width: 2rem;
    height: 2rem;
    border: none;
    background: transparent;
    font-size: 1.5rem;
    color: #666;
    cursor: pointer;
    border-radius: 6px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition:
        background-color 0.15s ease,
        color 0.15s ease;
}

.shortcuts-close:hover {
    background: #f0f0f0;
    color: #333;
}

.shortcuts-content {
    padding: 1rem 1.5rem;
    overflow-y: auto;
    flex: 1;
}

.shortcuts-category {
    margin-bottom: 1.5rem;
}

.shortcuts-category:last-child {
    margin-bottom: 0;
}

.shortcuts-category-title {
    font-size: 0.75rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: #666;
    margin-bottom: 0.75rem;
}

.shortcuts-list {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.shortcut-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.375rem 0;
}

.shortcut-keys {
    display: flex;
    align-items: center;
    gap: 0.25rem;
}

.shortcut-key {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 1.5rem;
    height: 1.5rem;
    padding: 0 0.375rem;
    background: #f5f5f5;
    border: 1px solid #ddd;
    border-radius: 4px;
    font-family:
        -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    font-size: 0.75rem;
    font-weight: 500;
    color: #333;
}

.shortcut-description {
    color: #666;
    font-size: 0.875rem;
}

.shortcuts-footer {
    padding: 1rem 1.5rem;
    border-top: 1px solid #e0e0e0;
    text-align: center;
}

.shortcuts-footer .text-muted {
    font-size: 0.75rem;
}

.shortcuts-footer .shortcut-key {
    font-size: 0.625rem;
    min-width: 1.25rem;
    height: 1.25rem;
}

/* Voice input */
.voice-input-container {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    margin-left: auto;
}

.voice-button {
    width: 2rem;
    height: 2rem;
    border-radius: 50%;
    border: 1px solid #ddd;
    background: #fff;
    color: #666;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    transition: all 0.15s ease;
}

.voice-button:hover {
    border-color: #007bff;
    color: #007bff;
}

.voice-button.connecting {
    border-color: #ffc107;
    color: #ffc107;
    animation: pulse 1s infinite;
}

.voice-button.recording {
    border-color: #dc3545;
    background: #dc3545;
    color: #fff;
    animation: pulse 1s infinite;
}

@keyframes pulse {
    0%,
    100% {
        transform: scale(1);
        opacity: 1;
    }
    50% {
        transform: scale(1.05);
        opacity: 0.8;
    }
}

.voice-button svg {
    width: 18px;
    height: 18px;
}

.voice-status {
    font-size: 0.75rem;
    color: #666;
    white-space: nowrap;
}

/* Date shortcuts */
.date-shortcut {
    display: inline-flex;
    align-items: center;
    background: #f5f5f5;
    border: 1px solid #e0e0e0;
    border-radius: 3px;
    padding: 0.125rem 0.375rem;
    font-size: 0.875rem;
    font-weight: 500;
    color: #333;
    cursor: default;
    user-select: none;
    white-space: nowrap;
}

.date-shortcut:hover {
    background: #e8e8e8;
    border-color: #d0d0d0;
}

/* Date suggestion popup (autocomplete dropdown) */
.date-suggestion-popup {
    position: fixed;
    background: white;
    border: 1px solid #ddd;
    border-radius: 8px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    max-height: 300px;
    overflow-y: auto;
    z-index: 1000;
    min-width: 240px;
}

.date-suggestion-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 8px 12px;
    cursor: pointer;
    gap: 16px;
}

.date-suggestion-item:first-child {
    border-radius: 7px 7px 0 0;
}

.date-suggestion-item:last-child {
    border-radius: 0 0 7px 7px;
}

.date-suggestion-item:hover,
.date-suggestion-item.selected {
    background: #f0f7ff;
}

.date-suggestion-label {
    font-weight: 500;
    color: #333;
}

.date-suggestion-meta {
    display: flex;
    gap: 8px;
    align-items: center;
}

.date-suggestion-date {
    color: #666;
    font-size: 0.875rem;
}

.date-suggestion-relative {
    color: #888;
    font-size: 0.75rem;
    font-style: italic;
}

/* Responsive - Mobile styles */
@media (max-width: 640px) {
    /* iOS safe area support */
    body {
        padding-left: env(safe-area-inset-left);
        padding-right: env(safe-area-inset-right);
        padding-bottom: env(safe-area-inset-bottom);
    }

    .container {
        padding: 0 1rem;
    }

    header .container {
        padding: 0 1rem;
    }

    header {
        margin-bottom: 0;
        /* iOS safe area at top */
        padding-top: env(safe-area-inset-top);
    }

    /* Smaller header title on mobile */
    header h1 {
        font-size: 1.125rem;
        max-width: 50vw;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    /* Editable title on mobile - more compact */
    header h1[contenteditable] {
        padding: 0.125rem 0.25rem;
        margin: -0.125rem -0.25rem;
    }

    /* Smaller nav gap on mobile */
    header nav {
        gap: 0.75rem;
    }

    main {
        border-radius: 0;
        box-shadow: none;
        padding: 0.5rem 1rem;
        margin: 0 -1rem;
    }

    .editor-content {
        border: none;
        padding: 0;
        border-radius: 0;
    }

    /* Larger touch targets for checkboxes (44px minimum) */
    .tiptap ul[data-type="taskList"] li > label {
        min-width: 44px;
        min-height: 44px;
        display: flex;
        align-items: center;
        justify-content: center;
        margin: -0.5rem;
        padding: 0.5rem;
    }

    .tiptap ul[data-type="taskList"] li > label input[type="checkbox"] {
        width: 20px;
        height: 20px;
        margin: 0;
    }

    /* Adjust task item layout for larger checkbox */
    .tiptap ul[data-type="taskList"] li {
        gap: 0.25rem;
    }

    /* Hide connection status text on mobile - just show dot */
    .connection-status .status-text {
        display: none;
    }

    .document-item {
        flex-direction: column;
        align-items: flex-start;
        gap: 0.5rem;
    }

    .document-item-actions {
        width: 100%;
        justify-content: flex-end;
    }

    /* Larger buttons on mobile for touch */
    .btn {
        min-height: 44px;
        padding: 0.625rem 1rem;
    }

    .btn-small {
        min-height: 36px;
        padding: 0.5rem 0.75rem;
    }

    /* Voice button larger on mobile */
    .voice-button {
        width: 44px;
        height: 44px;
    }

    .voice-button svg {
        width: 22px;
        height: 22px;
    }

    /* Help button larger on mobile */
    .btn-help {
        width: 36px;
        height: 36px;
    }

    /* User avatar larger on mobile */
    .user-avatar {
        width: 36px;
        height: 36px;
    }

    /* Section toggle larger on mobile */
    .section-toggle {
        width: 44px;
        height: 44px;
        margin: -0.5rem;
        padding: 0.5rem;
    }

    /* Shortcuts modal full screen on mobile */
    .shortcuts-modal {
        max-width: 100%;
        max-height: 100%;
        border-radius: 0;
        height: 100%;
    }

    .shortcuts-overlay {
        padding: 0;
    }

    /* Date suggestion popup full width on mobile */
    .date-suggestion-popup {
        left: 1rem !important;
        right: 1rem !important;
        width: auto !important;
        min-width: auto;
    }

    /* Section toolbar - smaller buttons on mobile */
    .section-toolbar button {
        min-height: 32px;
        padding: 0.25rem 0.75rem;
    }

    /* Tiptap editor minimum height reduced on mobile */
    .tiptap {
        min-height: 200px;
    }
}

/* Touch device specific - prevent text selection on interactive elements */
@media (hover: none) and (pointer: coarse) {
    .section-toggle,
    .voice-button,
    .btn-help,
    .user-avatar {
        -webkit-tap-highlight-color: transparent;
        touch-action: manipulation;
    }

    /* Disable double-tap zoom on buttons */
    button, a.btn {
        touch-action: manipulation;
    }
}
