/*
 * Editor CSS - Dark Theme
 *
 * This stylesheet provides:
 * - Dark theme color scheme via CSS custom properties
 * - Editor layout and typography
 * - Markdown rendering styles (headings, bold, italic, code, etc.)
 * - Line focus reveal/hide mechanism for markdown syntax
 */

/* ============================================
   JetBrains Mono Font Face Declarations
   ============================================ */
@font-face {
    font-family: 'JetBrains Mono';
    src: url('../fonts/JetBrainsMono-Regular.woff2') format('woff2');
    font-weight: 400;
    font-style: normal;
    font-display: swap;
}

@font-face {
    font-family: 'JetBrains Mono';
    src: url('../fonts/JetBrainsMono-Bold.woff2') format('woff2');
    font-weight: 700;
    font-style: normal;
    font-display: swap;
}

@font-face {
    font-family: 'JetBrains Mono';
    src: url('../fonts/JetBrainsMono-Italic.woff2') format('woff2');
    font-weight: 400;
    font-style: italic;
    font-display: swap;
}

@font-face {
    font-family: 'JetBrains Mono';
    src: url('../fonts/JetBrainsMono-BoldItalic.woff2') format('woff2');
    font-weight: 700;
    font-style: italic;
    font-display: swap;
}

/* ============================================
   CSS Custom Properties (Dark Theme)
   ============================================ */
:root {
    /* Background colors */
    --bg-primary: #1a1a1a;
    --bg-secondary: #242424;
    --bg-tertiary: #2d2d2d;

    /* Text colors */
    --text-primary: #e0e0e0;
    --text-secondary: #a0a0a0;
    --text-muted: #666666;

    /* Accent colors */
    --accent-blue: #6ab0f3;
    --accent-green: #98c379;
    --accent-purple: #c792ea;
    --accent-orange: #e5c07b;
    --accent-red: #e06c75;

    /* Syntax colors (shown when line is focused) */
    --syntax-color: #5c6370;

    /* Code colors */
    --code-bg: #2d2d2d;
    --code-text: #abb2bf;

    /* Link colors */
    --link-color: #6ab0f3;
    --link-hover: #8ac4f8;

    /* Selection */
    --selection-bg: rgba(106, 176, 243, 0.3);

    /* View transitions */
    --view-transition-duration: 100ms;

    /* Spacing */
    --line-height: 1.6;
    --paragraph-spacing: 0.5em;

    /* Typography - JetBrains Mono as primary font everywhere */
    --font-family: 'JetBrains Mono', 'SF Mono', Menlo, Monaco, 'Courier New', monospace;
    --font-family-mono: 'JetBrains Mono', 'SF Mono', Menlo, Monaco, 'Courier New', monospace;
    --font-size-base: 16px;
}

/* ============================================
   Reset and Base Styles
   ============================================ */
*, *::before, *::after {
    box-sizing: border-box;
}

html, body {
    margin: 0;
    padding: 0;
    height: 100%;
    width: 100%;
    overflow: hidden;
    background-color: var(--bg-primary);
    color: var(--text-primary);
    font-family: var(--font-family);
    font-size: var(--font-size-base);
    line-height: var(--line-height);
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

/* ============================================
   Editor Container
   ============================================ */
#editor-container {
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
}

/* ============================================
   Editor Scroller (scrollable area)
   ============================================ */
.editor-scroller {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    -webkit-overflow-scrolling: touch; /* Smooth scrolling on iOS */
}

/* ============================================
   Editor Content (contenteditable area)
   ============================================ */
.editor-content {
    min-height: 100%;
    padding: 24px;
    padding-bottom: 50vh; /* Extra space at bottom for comfortable editing */
    outline: none;
    white-space: pre-wrap;
    word-wrap: break-word;
    overflow-wrap: break-word;
    caret-color: var(--accent-blue);
}

/* Ensure the editor takes focus styling */
.editor-content:focus {
    outline: none;
}

/* Selection styling */
.editor-content ::selection {
    background-color: var(--selection-bg);
}

/* ============================================
   Line Styles
   ============================================ */
.line {
    position: relative;
    min-height: 1.6em; /* Match line-height */
}

/* Empty line placeholder to maintain height */
.line:empty::before {
    content: '\200B'; /* Zero-width space */
}

/* Line that contains only whitespace */
.line.empty {
    min-height: 1.6em;
}

/* ============================================
   Markdown Syntax (hide/reveal mechanism)

   By default, syntax markers are hidden.
   When a line is focused (cursor on it or part of selection),
   the syntax markers become visible.
   ============================================ */
.syntax {
    color: var(--syntax-color);
    font-weight: normal;
    font-style: normal;
}

/* Hidden syntax (default state) */
.syntax-hidden {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

/* When line is focused, reveal syntax */
.line.focused .syntax-hidden {
    position: static;
    width: auto;
    height: auto;
    padding: 0;
    margin: 0;
    overflow: visible;
    clip: auto;
    white-space: pre-wrap;
}

/* ============================================
   Heading Styles
   ============================================ */
.md-heading {
    font-weight: 600;
    color: var(--text-primary);
    margin: 0;
    padding: 0;
}

.md-heading-1 {
    font-size: 2em;
    line-height: 1.3;
    margin-top: 0.67em;
    margin-bottom: 0.67em;
}

.md-heading-2 {
    font-size: 1.5em;
    line-height: 1.35;
    margin-top: 0.83em;
    margin-bottom: 0.83em;
}

.md-heading-3 {
    font-size: 1.25em;
    line-height: 1.4;
    margin-top: 1em;
    margin-bottom: 1em;
}

.md-heading-4 {
    font-size: 1.1em;
    line-height: 1.45;
    margin-top: 1.33em;
    margin-bottom: 1.33em;
}

.md-heading-5 {
    font-size: 1em;
    line-height: 1.5;
    margin-top: 1.67em;
    margin-bottom: 1.67em;
}

.md-heading-6 {
    font-size: 0.9em;
    line-height: 1.55;
    color: var(--text-secondary);
    margin-top: 2em;
    margin-bottom: 2em;
}

/* First heading shouldn't have top margin */
.line:first-child .md-heading {
    margin-top: 0;
}

/* ============================================
   Bold and Italic
   ============================================ */
.md-bold {
    font-weight: 700;
    color: var(--text-primary);
}

.md-italic {
    font-style: italic;
}

.md-bold-italic {
    font-weight: 700;
    font-style: italic;
}

/* ============================================
   Inline Code
   ============================================ */
.md-code-inline {
    font-family: var(--font-family-mono);
    font-size: 0.9em;
    background-color: var(--code-bg);
    color: var(--code-text);
    padding: 0.15em 0.4em;
    border-radius: 4px;
}

/* ============================================
   Code Blocks (fenced)
   ============================================ */
.line.code-block {
    font-family: var(--font-family-mono);
    font-size: 0.9em;
    background-color: var(--code-bg);
    color: var(--code-text);
    padding-left: 1em;
    padding-right: 1em;
}

.line.code-block-start {
    border-top-left-radius: 6px;
    border-top-right-radius: 6px;
    padding-top: 0.75em;
}

.line.code-block-end {
    border-bottom-left-radius: 6px;
    border-bottom-right-radius: 6px;
    padding-bottom: 0.75em;
}

/* ============================================
   Links
   ============================================ */
.md-link {
    color: var(--link-color);
    text-decoration: none;
}

/* Hover effect only on devices that support hover (not touch-only) */
@media (hover: hover) {
    .md-link:hover {
        color: var(--link-hover);
        text-decoration: underline;
    }
}

/* Active state for touch feedback during tap */
.md-link:active {
    color: var(--link-hover);
}

/* Link URL portion (usually hidden) */
.md-link-url {
    color: var(--text-muted);
}

/* ============================================
   Blockquotes
   ============================================ */
.line.blockquote {
    border-left: 3px solid var(--accent-purple);
    padding-left: 1em;
    color: var(--text-secondary);
}

/* ============================================
   Lists
   ============================================ */
.line.list-item {
    /* Use ch units for precise monospace alignment */
    position: relative;
}

/* Unordered lists: marker is 1ch (e.g., "-"), plus 1ch space = 2ch total */
.line.list-item-unordered {
    padding-left: 2ch;
}

/* Ordered lists: marker is 2ch+ (e.g., "1."), plus 1ch space = 3ch+ total */
.line.list-item-ordered {
    padding-left: 3ch;
}

/* Handle multi-digit ordered lists (e.g., "10.", "100.") */
.line.list-item-ordered[data-marker-width="3"] { padding-left: 4ch; }
.line.list-item-ordered[data-marker-width="4"] { padding-left: 5ch; }
.line.list-item-ordered[data-marker-width="5"] { padding-left: 6ch; }

/* Indent levels for unordered lists - each level adds 4ch (4 spaces) */
.line.list-item-unordered[data-indent="1"] { padding-left: 6ch; }  /* 4 + 2 */
.line.list-item-unordered[data-indent="2"] { padding-left: 10ch; } /* 8 + 2 */
.line.list-item-unordered[data-indent="3"] { padding-left: 14ch; } /* 12 + 2 */
.line.list-item-unordered[data-indent="4"] { padding-left: 18ch; } /* 16 + 2 */
.line.list-item-unordered[data-indent="5"] { padding-left: 22ch; } /* 20 + 2 */
.line.list-item-unordered[data-indent="6"] { padding-left: 26ch; } /* 24 + 2 */

/* Indent levels for ordered lists - each level adds 4ch (4 spaces) */
.line.list-item-ordered[data-indent="1"] { padding-left: 7ch; }  /* 4 + 3 */
.line.list-item-ordered[data-indent="2"] { padding-left: 11ch; } /* 8 + 3 */
.line.list-item-ordered[data-indent="3"] { padding-left: 15ch; } /* 12 + 3 */
.line.list-item-ordered[data-indent="4"] { padding-left: 19ch; } /* 16 + 3 */
.line.list-item-ordered[data-indent="5"] { padding-left: 23ch; } /* 20 + 3 */
.line.list-item-ordered[data-indent="6"] { padding-left: 27ch; } /* 24 + 3 */

/* List marker styling - use syntax color like other markdown syntax */
.md-list-marker {
    color: var(--syntax-color);
    /* Position the marker in the padding area */
    position: absolute;
    left: 0;
}

/* Adjust marker position for each indent level (unordered) */
.line.list-item-unordered[data-indent="1"] .md-list-marker { left: 4ch; }
.line.list-item-unordered[data-indent="2"] .md-list-marker { left: 8ch; }
.line.list-item-unordered[data-indent="3"] .md-list-marker { left: 12ch; }
.line.list-item-unordered[data-indent="4"] .md-list-marker { left: 16ch; }
.line.list-item-unordered[data-indent="5"] .md-list-marker { left: 20ch; }
.line.list-item-unordered[data-indent="6"] .md-list-marker { left: 24ch; }

/* Adjust marker position for each indent level (ordered) */
.line.list-item-ordered[data-indent="1"] .md-list-marker { left: 4ch; }
.line.list-item-ordered[data-indent="2"] .md-list-marker { left: 8ch; }
.line.list-item-ordered[data-indent="3"] .md-list-marker { left: 12ch; }
.line.list-item-ordered[data-indent="4"] .md-list-marker { left: 16ch; }
.line.list-item-ordered[data-indent="5"] .md-list-marker { left: 20ch; }
.line.list-item-ordered[data-indent="6"] .md-list-marker { left: 24ch; }

/* When focused, show the full syntax including whitespace */
.line.list-item.focused {
    /* Reset padding when focused - raw whitespace provides indentation */
    padding-left: 0;
}

/* When focused, marker becomes part of the normal text flow */
.line.list-item.focused .md-list-marker {
    position: static;
}

/* ============================================
   Horizontal Rule
   ============================================ */
.line.horizontal-rule {
    display: flex;
    align-items: center;
    height: 2em;
}

.line.horizontal-rule::after {
    content: '';
    flex: 1;
    height: 1px;
    background-color: var(--text-muted);
}

/* ============================================
   Strikethrough (GFM)
   ============================================ */
.md-strikethrough {
    text-decoration: line-through;
    color: var(--text-secondary);
}

/* ============================================
   Task Lists (GFM)
   ============================================ */
.md-task-checkbox {
    font-family: var(--font-family-mono);
    color: var(--accent-green);
}

.md-task-checked {
    color: var(--text-muted);
    text-decoration: line-through;
}

/* ============================================
   iOS Safari Specific Fixes
   ============================================ */

/* Prevent iOS from zooming on input */
@supports (-webkit-touch-callout: none) {
    .editor-content {
        font-size: 16px; /* Minimum to prevent zoom on iOS */
    }
}

/* Fix for iOS momentum scrolling */
.editor-scroller {
    -webkit-overflow-scrolling: touch;
}

/* Ensure proper text rendering on iOS */
.editor-content {
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
}

/* ============================================
   Placeholder (shown when editor is empty)
   ============================================ */
.editor-content.empty::before {
    content: attr(data-placeholder);
    color: var(--text-muted);
    pointer-events: none;
    position: absolute;
}

/* ============================================
   Focus indicator for accessibility
   ============================================ */
.editor-scroller:focus-within {
    /* Subtle glow to indicate focus */
    box-shadow: inset 0 0 0 1px rgba(106, 176, 243, 0.1);
}

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

/* ============================================
   App Container and Layout
   ============================================ */
#app-container {
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: row;
    overflow: hidden;
}

/* Default: single view mode (mobile) */
#note-list-container,
#editor-container,
#settings-container {
    width: 100%;
    height: 100%;
    flex-shrink: 0;
}

/* Split view mode (large screens) */
#app-container.split-view {
    flex-direction: row;
}

#app-container.split-view #note-list-container {
    width: 320px;
    min-width: 320px;
    border-right: 1px solid var(--bg-tertiary);
}

#app-container.split-view #editor-container {
    flex: 1;
    width: auto;
}

#app-container.split-view #settings-container {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: var(--bg-primary);
    z-index: 100;
}

/* ============================================
   Common Header Styles
   ============================================ */
.note-list-header,
.editor-header,
.settings-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 56px;
    padding: 0 12px;
    background-color: var(--bg-secondary);
    border-bottom: 1px solid var(--bg-tertiary);
    flex-shrink: 0;
}

.header-title {
    font-size: 1.1em;
    font-weight: 600;
    color: var(--text-primary);
    margin: 0;
    text-align: center;
    flex: 1;
}

.header-title-placeholder {
    flex: 1;
}

.header-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 44px;
    height: 44px;
    padding: 0;
    border: none;
    background: transparent;
    cursor: pointer;
    border-radius: 8px;
    transition: background-color 0.15s ease;
}

/* Hover effect only on devices that support hover (not touch-only) */
@media (hover: hover) {
    .header-btn:hover {
        background-color: var(--bg-tertiary);
    }
}

.header-btn:active {
    background-color: var(--bg-primary);
}

.header-btn-placeholder {
    width: 44px;
    height: 44px;
}

/* ============================================
   Icon Styles
   ============================================ */
.icon {
    width: 24px;
    height: 24px;
    filter: invert(0.85);
}

/* ============================================
   Note List View
   ============================================ */
.note-list {
    display: flex;
    flex-direction: column;
    height: 100%;
    background-color: var(--bg-primary);
}

.note-list-content {
    flex: 1;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
}

/* Search Container */
.search-container {
    display: flex;
    align-items: center;
    padding: 12px 16px;
    background-color: var(--bg-primary);
    border-bottom: 1px solid var(--bg-tertiary);
}

.search-icon {
    width: 20px;
    height: 20px;
    filter: invert(0.5);
    margin-right: 12px;
    flex-shrink: 0;
}

.search-input {
    flex: 1;
    background: transparent;
    border: none;
    color: var(--text-primary);
    font-family: var(--font-family);
    font-size: 16px;
    outline: none;
}

.search-input::placeholder {
    color: var(--text-muted);
}

/* Notes List */
.notes-list {
    list-style: none;
    margin: 0;
    padding: 0;
}

/* Note Item */
.note-item {
    position: relative;
    overflow: hidden;
    border-bottom: 1px solid var(--bg-tertiary);
}

.note-item-content {
    display: flex;
    align-items: center;
    padding: 16px;
    background-color: var(--bg-primary);
    cursor: pointer;
    transition: transform 0.2s ease-out;
    position: relative;
    z-index: 1;
}

/* Hover effect only on devices that support hover (not touch-only) */
@media (hover: hover) {
    .note-item-content:hover {
        background-color: var(--bg-secondary);
    }
}

/* Active state for touch feedback during tap */
.note-item-content:active {
    background-color: var(--bg-secondary);
}

.note-item.selected .note-item-content {
    background-color: var(--bg-tertiary);
}

.note-item-inner {
    display: flex;
    flex-direction: column;
    gap: 4px;
    flex: 1;
    min-width: 0;
}

.note-preview {
    font-size: 15px;
    color: var(--text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.note-time {
    font-size: 13px;
    color: var(--text-muted);
}

/* Delete Button (revealed on swipe) */
.note-delete-btn {
    position: absolute;
    right: 0;
    top: 0;
    bottom: 0;
    width: 80px;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: var(--accent-red);
    border: none;
    cursor: pointer;
    z-index: 0;
}

.note-delete-btn .icon {
    filter: invert(1);
}

/* Empty State */
.empty-state {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 200px;
    padding: 24px;
}

.empty-message {
    color: var(--text-muted);
    font-size: 15px;
    text-align: center;
}

/* ============================================
   Editor View (with header)
   ============================================ */
.editor-wrapper {
    display: flex;
    flex-direction: column;
    height: 100%;
}

.editor-content-wrapper {
    flex: 1;
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

/* Menu Container and Dropdown */
.menu-container {
    position: relative;
}

.dropdown-menu {
    position: absolute;
    top: 100%;
    right: 0;
    min-width: 180px;
    background-color: var(--bg-secondary);
    border: 1px solid var(--bg-tertiary);
    border-radius: 8px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
    z-index: 1000;
    overflow: hidden;
}

.dropdown-item {
    display: flex;
    align-items: center;
    gap: 12px;
    width: 100%;
    padding: 12px 16px;
    border: none;
    background: transparent;
    color: var(--text-primary);
    font-family: var(--font-family);
    font-size: 15px;
    cursor: pointer;
    text-align: left;
}

/* Hover effect only on devices that support hover (not touch-only) */
@media (hover: hover) {
    .dropdown-item:hover {
        background-color: var(--bg-tertiary);
    }
}

/* Active state for touch feedback during tap */
.dropdown-item:active {
    background-color: var(--bg-tertiary);
}

.dropdown-item.delete-item {
    color: var(--accent-red);
}

.dropdown-item.delete-item .icon {
    filter: invert(0.45) sepia(1) saturate(5) hue-rotate(320deg);
}

/* ============================================
   Settings View
   ============================================ */
.settings-view {
    display: flex;
    flex-direction: column;
    height: 100%;
    background-color: var(--bg-primary);
}

.settings-content {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
}

.settings-section {
    background-color: var(--bg-secondary);
    border-radius: 12px;
    overflow: hidden;
}

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

.settings-item:last-child {
    border-bottom: none;
}

.settings-label {
    color: var(--text-primary);
    font-size: 15px;
}

.settings-value {
    color: var(--text-secondary);
    font-size: 15px;
}

/* ============================================
   Responsive Breakpoints
   ============================================ */
@media (min-width: 1024px) {
    /* Split view is handled by the split-view class added via JS */
}

@media (max-width: 1023px) {
    /* Single view mode - containers are full width and toggled via JS */
    #note-list-container,
    #editor-container,
    #settings-container {
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
    }

    /*
     * View Transitions (Mobile)
     *
     * Note list is the base layer (always at translateX(0)).
     * Editor slides in from the right.
     * Settings slides up from the bottom.
     */

    /* Editor: slides in from right */
    #editor-container {
        transform: translateX(100%);
        transition: transform var(--view-transition-duration) ease-out,
                    visibility var(--view-transition-duration);
        visibility: hidden;
        /* Solid background to prevent note list from showing through */
        background-color: var(--bg-primary);
    }

    #editor-container.view-visible {
        transform: translateX(0);
        visibility: visible;
    }

    /* Settings: slides up from bottom */
    #settings-container {
        transform: translateY(100%);
        transition: transform var(--view-transition-duration) ease-out,
                    visibility var(--view-transition-duration);
        visibility: hidden;
        z-index: 100;
    }

    #settings-container.view-visible {
        transform: translateY(0);
        visibility: visible;
    }
}

/* ============================================
   Settings Overlay Transitions (Split View)

   In split view, settings is a full-screen overlay
   that slides up from the bottom.
   ============================================ */
#app-container.split-view #settings-container {
    transform: translateY(100%);
    transition: transform var(--view-transition-duration) ease-out,
                visibility var(--view-transition-duration);
    visibility: hidden;
}

#app-container.split-view #settings-container.view-visible {
    transform: translateY(0);
    visibility: visible;
}
