:root {
    --primary: #c41e3a;
    --bg: #121212;
    --card-bg: #1e1e1e;
    --text: #f5f5f5;
    --border: #333;
    --shadow: 0 4px 16px rgba(196,30,58,0.2);
    --radius: 16px;
    --card-min-height: 200px;
    --view-cols: 1;
    --safe-area-bottom: env(safe-area-inset-bottom);
}

[data-theme="light"] {
    --bg: #f8f9fa;
    --card-bg: #ffffff;
    --text: #222;
    --border: #ddd;
    --shadow: 0 4px 16px rgba(196,30,58,0.15);
}

/* View Modes */
body.view-grid { --view-cols: 2; }
body.view-full { --view-cols: 1; }

/* MOBILE OPTIMIZATION */
@media (max-width: 768px) {
    body.view-grid { --view-cols: 2; } /* Force 2x2 */
    #grid { gap: 10px; padding: 10px; }
    #inputPanel { padding: 10px; gap: 8px; padding-bottom: calc(10px + var(--safe-area-bottom)); }
    #userInput { padding: 14px; font-size: 16px; height: 48px; }
    #sendBtn { padding: 12px 16px; min-width: 50px; height: 48px; font-size: 22px; }
    #topBar { height: 50px; }
    .fab { width: 36px; height: 36px; font-size: 12px; }
    .view-fab-container { bottom: 95px; right: 10px; }
    .card-face { font-size: 14px; }
    #cardMenu { min-width: 200px; left: 50% !important; transform: translateX(-50%) !important; top: auto !important; bottom: 80px; }
}

* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }
body {
    font-family: system-ui, -apple-system, sans-serif;
    background: var(--bg);
    color: var(--text);
    min-height: 100vh;
    padding-top: 50px;
    padding-bottom: 80px;
    transition: background 0.3s, color 0.3s;
    overflow-x: hidden;
}

/* Top Bar */
#topBar {
    position: fixed; top: 0; left: 0; right: 0; height: 50px;
    padding: 0 10px; display: flex; align-items: center; justify-content: space-between;
    z-index: 50; background: linear-gradient(to bottom, var(--bg), transparent); pointer-events: none;
}
#brandPlaceholder { pointer-events: none; font-weight: 900; color: var(--primary); text-transform: uppercase; letter-spacing: 1px; font-size: 12px; }
.top-fabs { pointer-events: auto; display: flex; gap: 6px; }
.fab {
    width: 36px; height: 36px; border-radius: 50%; background: var(--card-bg);
    border: 1px solid var(--border); color: var(--text); display: flex; align-items: center; justify-content: center;
    cursor: pointer; box-shadow: var(--shadow); transition: all 0.2s; font-size: 12px; backdrop-filter: blur(5px);
}
.fab:hover { transform: scale(1.1); background: var(--bg); }
.fab.primary { background: var(--primary); color: white; border: none; }

/* Selection Bar */
#selectionBar {
    position: fixed; top: 0; left: 0; right: 0; background: var(--card-bg); border-bottom: 1px solid var(--border);
    padding: 10px; z-index: 1000; display: none; align-items: center; justify-content: space-between;
    box-shadow: var(--shadow); transform: translateY(-100%); transition: transform 0.3s;
}
#selectionBar.active { display: flex; transform: translateY(0); }
#mergeSelected, #deleteSelected, #splitSelected {
    background: var(--primary); color: white; border: none; padding: 8px 12px; border-radius: 8px; cursor: pointer; font-size: 13px; margin-left: 6px;
    display: flex; align-items: center; gap: 4px;
}
#deleteSelected { background: #d32f2f; }
#cancelSelect { padding: 6px 10px; }

/* Grid Area */
#grid {
    display: grid; grid-template-columns: repeat(var(--view-cols), 1fr); gap: 16px; padding: 16px;
    max-width: 1000px; margin: 0 auto; transition: all 0.3s ease; min-height: 50vh; outline: none;
}
body.view-full #grid { display: block; padding: 0; gap: 0; }
body.view-full #grid .flip-card { height: 100vh; border-radius: 0; border: none; min-height: unset; }

/* Keyboard Focus Accessibility */
#grid:focus-visible { outline: 2px solid var(--primary); outline-offset: 4px; }

/* Card */
.flip-card {
    min-height: var(--card-min-height); perspective: 1000px; border-radius: var(--radius);
    overflow: hidden; box-shadow: var(--shadow); position: relative;
    background: var(--card-bg); border: 1px solid var(--border);
    transition: transform 0.2s, box-shadow 0.2s; touch-action: manipulation;
}
.flip-card:hover { transform: translateY(-2px); box-shadow: 0 8px 24px rgba(0,0,0,0.2); }
.flip-card:focus-visible { outline: 3px solid var(--primary); outline-offset: 2px; } /* Keyboard Nav */
.flip-card.new { animation: appear 0.4s ease forwards; opacity: 0; transform: scale(0.95); }
@keyframes appear { to { opacity: 1; transform: scale(1); } }
.flip-card.selected { outline: 3px solid var(--primary); outline-offset: -3px; z-index: 10; }
.flip-card.locked { border: 2px dashed var(--primary); }

.flip-card-inner { width: 100%; height: 100%; transition: transform 0.6s; transform-style: preserve-3d; position: relative; }
.flip-card.flipped .flip-card-inner { transform: rotateY(180deg); }

.card-face {
    position: absolute; inset: 0; backface-visibility: hidden;
    display: flex; flex-direction: column; padding: 14px; overflow-y: auto;
    font-size: 14px; line-height: 1.5; background: inherit; color: inherit;
}
.card-back { transform: rotateY(180deg); }

.card-header {
    position: sticky; top: 0; display: flex; justify-content: space-between; align-items: center;
    padding-bottom: 8px; background: inherit; z-index: 5; border-bottom: 1px solid var(--border);
    margin-bottom: 8px; gap: 8px;
}
.card-actions { display: flex; gap: 4px; }
.card-actions button { 
    background: none; border: none; color: var(--text); cursor: pointer; 
    padding: 4px; opacity: 0.4; border-radius: 4px; transition: all 0.2s; 
    min-width: 30px; min-height: 30px;
}
.card-actions button:hover { opacity: 1; background: rgba(255,255,255,0.1); }

/* Fullscreen Overlay - READER MODE OPTIMIZED */
.fullscreen-overlay {
    position: fixed; inset: 0; background: var(--bg); z-index: 2000; display: none; flex-direction: column;
}
.fullscreen-overlay.active { display: flex; }
.fs-header { 
    padding: 12px 16px; background: var(--card-bg); border-bottom: 1px solid var(--border); 
    display: flex; justify-content: space-between; align-items: center; flex-shrink: 0; 
}

.fs-content { 
    flex: 1; 
    overflow-y: auto; 
    padding: 0; /* Remove padding for full immersion */
    display: flex;
    flex-direction: column;
}

.fs-pane { 
    background: var(--card-bg); 
    border: none; /* Remove borders for cleaner look */
    border-top: 1px solid var(--border); /* Only top separator if stacked */
    border-radius: 0; /* Sharp corners for immersive feel */
    padding: 24px 32px; /* Generous padding for text */
    overflow-y: auto; 
    min-height: 0; 
    width: 100%; 
    font-size: 1.15rem; /* Larger text for readability */
    line-height: 1.7; /* Increased line height */
    margin-bottom: 0; 
}

/* Fullscreen Typography Enhancements */
.fs-pane h1, .fs-pane h2, .fs-pane h3 { margin-top: 1.5em; margin-bottom: 0.5em; color: var(--primary); }
.fs-pane p { margin-bottom: 1em; }
.fs-pane ul, .fs-pane ol { padding-left: 1.5em; margin-bottom: 1em; }

/* Streaming & Thinking Visuals */
.streaming { text-align: center; color: #888; padding: 20px; display: flex; align-items: center; justify-content: center; gap: 10px; flex-wrap: wrap; }
.cursor { display: inline-block; width: 3px; height: 1em; background: currentColor; animation: blink 1s infinite; vertical-align: middle; }
@keyframes blink { 50% { opacity: 0; } }

.thinking-indicator {
    display: inline-block;
    color: var(--primary);
    font-weight: bold;
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: 1px;
    animation: pulse-text 1.5s infinite;
}
@keyframes pulse-text { 0% { opacity: 0.3; } 50% { opacity: 1; } 100% { opacity: 0.3; } }

/* Stop Stream Button */
.stop-stream-btn {
    background: #d32f2f;
    color: white;
    border: none;
    padding: 4px 8px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 12px;
    vertical-align: middle;
    margin-left: 10px;
    transition: all 0.2s;
}
.stop-stream-btn:hover { background: #b71c1c; transform: scale(1.05); }

/* Input Bar - MOBILE OPTIMIZED */
#inputPanel {
    position: fixed; bottom: 0; left: 0; right: 0; 
    background: var(--card-bg); border-top: 1px solid var(--border); 
    padding: 12px; display: flex; gap: 10px; z-index: 100; 
    box-shadow: 0 -4px 20px rgba(0,0,0,0.2); align-items: center;
    padding-bottom: calc(12px + var(--safe-area-bottom));
}

#userInput {
    flex: 1; padding: 12px 14px; border-radius: 24px; border: 1px solid var(--border); 
    background: var(--bg); color: var(--text); font-size: 16px; outline: none; 
    transition: border 0.2s; resize: none; height: 46px; max-height: 100px; overflow-y: auto;
}
#userInput:focus { border-color: var(--primary); }

/* NEW SEND ICON ⏎ */
#sendBtn { 
    background: var(--primary); color: white; border: none; padding: 0 16px; 
    border-radius: 24px; cursor: pointer; font-weight: bold; 
    display: flex; align-items: center; justify-content: center; 
    min-width: 46px; height: 46px; font-size: 22px; 
    transition: all 0.2s; flex-shrink: 0;
}
#sendBtn:hover { background: #a1172f; transform: translateY(-1px); }
#sendBtn.listening { background: #d32f2f; animation: pulse-btn 1s infinite; }
@keyframes pulse-btn { 0% { transform: scale(1); } 50% { transform: scale(1.05); } 100% { transform: scale(1); } }

/* View Button (Bottom Right) */
.view-fab-container { position: fixed; bottom: 80px; right: 12px; z-index: 90; }

/* Context Menu */
#cardMenu {
    position: fixed; background: var(--card-bg); border: 1px solid var(--border); border-radius: 12px;
    padding: 6px; display: none; flex-direction: column; gap: 2px; z-index: 2000;
    box-shadow: 0 10px 30px rgba(0,0,0,0.5); min-width: 180px; max-height: 60vh; overflow-y: auto;
}
#cardMenu.active { display: flex; }
.menu-item { 
    display: flex; align-items: center; gap: 10px; padding: 10px 12px; border-radius: 6px; 
    cursor: pointer; color: var(--text); background: transparent; border: none; width: 100%; 
    text-align: left; font-size: 14px; transition: background 0.1s;
}
.menu-item:active { background: rgba(255,255,255,0.1); }
.menu-item i { width: 20px; text-align: center; }
.menu-separator { height: 1px; background: var(--border); margin: 4px 0; }

/* Modals */
.modal-overlay {
    position: fixed; inset: 0; background: rgba(0,0,0,0.6); backdrop-filter: blur(4px); z-index: 1500;
    display: none; align-items: center; justify-content: center; padding: 20px;
}
.modal-overlay.active { display: flex; }
.modal { background: var(--card-bg); border-radius: 16px; width: 100%; max-width: 600px; max-height: 90vh; border: 1px solid var(--border); display: flex; flex-direction: column; box-shadow: 0 20px 50px rgba(0,0,0,0.5); overflow: hidden; }
.modal-header { padding: 16px 20px; border-bottom: 1px solid var(--border); display: flex; justify-content: space-between; align-items: center; }
.modal-body { padding: 20px; overflow-y: auto; }
.modal-footer { padding: 16px 20px; border-top: 1px solid var(--border); display: flex; justify-content: flex-end; gap: 12px; }

textarea, input[type="text"], input[type="file"] {
    width: 100%; padding: 12px; border-radius: 8px; border: 1px solid var(--border); background: var(--bg); color: var(--text);
    font-family: inherit; font-size: 15px; resize: vertical; min-height: 120px; margin-bottom: 12px;
}

button.btn { padding: 10px 16px; border-radius: 8px; border: none; cursor: pointer; font-size: 14px; font-weight: 600; transition: all 0.2s; }
.btn-primary { background: var(--primary); color: white; }
.btn-danger { background: #d32f2f; color: white; }
.btn-ghost { background: transparent; color: var(--text); border: 1px solid var(--border); }

/* Style Forms */
.style-section { margin-bottom: 16px; }
.style-section h4 { font-size: 12px; text-transform: uppercase; opacity: 0.7; margin-bottom: 8px; letter-spacing: 0.5px; }
.color-picker { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
.color-picker input { width: 40px; height: 30px; border: none; padding: 0; border-radius: 4px; cursor: pointer; }
.toggle-group { display: flex; gap: 6px; flex-wrap: wrap; }
.toggle-btn { padding: 6px 10px; border: 1px solid var(--border); border-radius: 6px; font-size: 12px; cursor: pointer; background: transparent; color: var(--text); }
.toggle-btn.active { background: var(--primary); color: white; border-color: var(--primary); }
.slider-group { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
.slider-group input { flex: 1; }
.lock-checkbox { display: flex; align-items: center; gap: 8px; margin-top: 12px; padding: 10px; background: rgba(255,255,255,0.05); border-radius: 6px; }

/* Toast */
#toast { position: fixed; bottom: 100px; left: 50%; transform: translateX(-50%) translateY(100px); background: var(--card-bg); border: 1px solid var(--border); color: var(--text); padding: 10px 16px; border-radius: 20px; font-size: 13px; z-index: 2500; transition: transform 0.3s; opacity: 0; box-shadow: var(--shadow); }
#toast.show { transform: translateX(-50%) translateY(0); opacity: 1; }

#emptyState { text-align: center; color: rgba(255,255,255,0.3); margin-top: 50px; font-size: 18px; pointer-events: none; display: flex; flex-direction: column; gap: 10px; align-items: center; }