:root {
    --chat-font-size: 14px;
}

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

body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
    background: #f6f2e8;
    color: #2e2a24;
    min-height: 100vh;
    padding: 0;
    margin: 0;
    overflow: hidden;
}

.app-wrapper {
    width: 100vw;
    height: 100vh;
    height: 100dvh; /* Use dynamic viewport height on iOS */
    margin: 0;
    padding: 0;
    display: flex;
    flex-direction: column;
}

.app-header {
    flex: 0 0 auto;
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: #fdfaf3;
    border-bottom: 1px solid #e6dcc8;
    padding: 12px 16px;
    box-shadow: 0 2px 8px rgba(40, 30, 15, 0.05);
}

.header-left {
    display: flex;
    align-items: center;
    gap: 16px;
}

.header-right {
    display: flex;
    align-items: center;
    gap: 12px;
}

.support-link {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 16px;
    background: linear-gradient(135deg, #FFDD00 0%, #FFA500 100%);
    color: #2e2a24;
    text-decoration: none;
    border-radius: 8px;
    font-weight: 600;
    font-size: 14px;
    transition: transform 0.2s, box-shadow 0.2s;
    box-shadow: 0 2px 8px rgba(255, 165, 0, 0.3);
}

.support-link:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(255, 165, 0, 0.4);
}

.support-link svg {
    fill: currentColor;
    stroke: none;
}

.brand {
    display: flex;
    align-items: center;
    gap: 10px;
    font-weight: 600;
    font-size: 18px;
    color: #2e2a24;
}

.brand-mark {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 18px;
    height: 18px;
    border-radius: 50%;
    background: #f26532;
    color: #fdfaf3;
    font-size: 10px;
}

.brand-name {
    letter-spacing: 0.4px;
}

.btn-upload {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 10px 16px;
    border-radius: 12px;
    border: none;
    background: #f26532;
    color: white;
    font-weight: 500;
    cursor: pointer;
    transition: transform 0.15s ease, box-shadow 0.15s ease;
}

.btn-upload:hover {
    transform: translateY(-1px);
    box-shadow: 0 6px 18px rgba(242, 101, 50, 0.3);
}

.btn-upload svg {
    stroke: currentColor;
}

.header-right {
    display: flex;
    align-items: center;
    gap: 16px;
}

.header-link {
    color: #8a7860;
    text-decoration: none;
    font-weight: 500;
    font-size: 14px;
    transition: color 0.15s ease;
}

.header-link:hover {
    color: #f26532;
}

.container {
    flex: 1;
    display: flex;
    gap: 0;
    background: #fdfaf3;
    border-top: 1px solid #e6dcc8;
    padding: 0;
    height: 100%;
    position: relative;
    overflow: hidden;
}

/* PDF Section */
.pdf-section {
    flex: 0 0 65%;
    background: #fffdf7;
    border-right: 1px solid #e6dcc8;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    min-width: 320px;
}

.pane-resizer {
    flex: 0 0 12px;
    width: 12px;
    height: 100%;
    cursor: col-resize;
    background: linear-gradient(180deg, rgba(230, 220, 200, 0), rgba(230, 220, 200, 0.8), rgba(230, 220, 200, 0));
    display: flex;
    align-items: center;
    justify-content: center;
}

.pane-resizer::before {
    content: '';
    width: 2px;
    height: 48px;
    border-radius: 999px;
    background: #d8c8aa;
}

.upload-area {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
    padding: 40px;
}

.upload-content {
    text-align: center;
    color: #8a7860;
}

.upload-content svg {
    color: #f26532;
    margin-bottom: 20px;
}

.upload-content h2 {
    color: #2e2a24;
    margin-bottom: 10px;
}

.upload-content p {
    margin-bottom: 20px;
    color: #8a7860;
}

.upload-subtitle {
    font-size: 15px;
    margin-bottom: 30px !important;
}

.feature-description {
    max-width: 500px;
    margin: 0 auto 30px;
    text-align: left;
    background: #fdfaf3;
    border: 1px solid #e6dcc8;
    border-radius: 12px;
    padding: 20px;
}

.feature-item {
    display: flex;
    align-items: flex-start;
    gap: 12px;
    margin-bottom: 16px;
}

.feature-item:last-child {
    margin-bottom: 0;
}

.feature-item svg {
    flex-shrink: 0;
    margin-top: 2px;
    color: #f26532;
}

.feature-item span {
    color: #2e2a24;
    font-size: 14px;
    line-height: 1.5;
}

.feature-item strong {
    color: #f26532;
    font-weight: 600;
}

.btn-primary {
    background: #f26532;
    color: white;
    border: none;
    padding: 12px 32px;
    border-radius: 8px;
    font-size: 16px;
    cursor: pointer;
    transition: transform 0.15s, box-shadow 0.15s;
}

.btn-primary:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 18px rgba(242, 101, 50, 0.3);
}

/* URL Input Section */
.url-divider {
    margin: 20px 0;
    text-align: center;
    position: relative;
}

.url-divider::before,
.url-divider::after {
    content: '';
    position: absolute;
    top: 50%;
    width: 40%;
    height: 1px;
    background: #e6dcc8;
}

.url-divider::before {
    left: 0;
}

.url-divider::after {
    right: 0;
}

.url-divider span {
    padding: 0 15px;
    color: #8a7860;
    font-size: 13px;
    font-weight: 500;
    background: #f6f2e8;
}

.url-section {
    width: 100%;
    max-width: 500px;
    margin: 0 auto;
}

.url-header {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-bottom: 8px;
    font-size: 15px;
    font-weight: 600;
    color: #2e2a24;
}

.beta-badge {
    display: inline-block;
    padding: 2px 8px;
    background: #f26532;
    color: white;
    font-size: 11px;
    font-weight: 700;
    border-radius: 4px;
    letter-spacing: 0.5px;
}

.wip-badge {
    display: inline-block;
    padding: 2px 8px;
    background: #EBB434;
    color: #2e2a24;
    font-size: 11px;
    font-weight: 700;
    border-radius: 4px;
    letter-spacing: 0.5px;
}

/* API Key Banner */
.api-key-banner {
    background: linear-gradient(135deg, #f26532 0%, #f28a32 100%);
    color: white;
    padding: 12px 16px;
    box-shadow: 0 2px 8px rgba(242, 101, 50, 0.2);
    animation: slideDown 0.3s ease-out;
}

.banner-content {
    display: flex;
    align-items: center;
    gap: 12px;
    max-width: 1400px;
    margin: 0 auto;
}

.banner-text {
    flex: 1;
    font-size: 14px;
    line-height: 1.4;
}

.banner-text strong {
    font-weight: 700;
}

.banner-close {
    background: rgba(255, 255, 255, 0.2);
    border: none;
    color: white;
    font-size: 24px;
    width: 32px;
    height: 32px;
    border-radius: 4px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s;
    flex-shrink: 0;
}

.banner-close:hover {
    background: rgba(255, 255, 255, 0.3);
}

.api-key-banner.hidden {
    display: none;
}

@keyframes slideDown {
    from {
        transform: translateY(-100%);
        opacity: 0;
    }
    to {
        transform: translateY(0);
        opacity: 1;
    }
}

.url-note {
    font-size: 13px;
    color: #8a7860;
    margin-bottom: 12px;
    font-style: italic;
}

.url-input-container {
    display: flex;
    gap: 10px;
    width: 100%;
}

.url-input-container input[type="url"] {
    flex: 1;
    padding: 12px 16px;
    border: 2px solid #e6dcc8;
    border-radius: 8px;
    font-size: 14px;
    background: #fdfaf3;
    color: #2e2a24;
    transition: border-color 0.2s;
}

.url-input-container input[type="url"]:focus {
    outline: none;
    border-color: #f26532;
}

.url-input-container input[type="url"]::placeholder {
    color: #c4af87;
}

.btn-secondary {
    padding: 12px 24px;
    border: 2px solid #f26532;
    background: transparent;
    color: #f26532;
    border-radius: 8px;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s;
    white-space: nowrap;
}

.btn-secondary:hover {
    background: #f26532;
    color: white;
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(242, 101, 50, 0.2);
}

.btn-secondary:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    transform: none;
}

/* Loading Progress Bar */
.loading-progress {
    width: 100%;
    max-width: 400px;
    margin: 30px auto 0;
    padding: 20px;
    background: #fdfaf3;
    border: 1px solid #e6dcc8;
    border-radius: 12px;
    box-shadow: 0 4px 12px rgba(40, 30, 15, 0.08);
}

.progress-info {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 12px;
    font-size: 14px;
    color: #2e2a24;
}

.progress-info #loadingStatus {
    font-weight: 500;
    color: #8a7860;
}

.progress-info #loadingPercent {
    font-weight: 600;
    color: #f26532;
}

.progress-bar-container {
    width: 100%;
    height: 8px;
    background: #e6dcc8;
    border-radius: 4px;
    overflow: hidden;
    position: relative;
}

.progress-bar {
    height: 100%;
    background: linear-gradient(90deg, #f26532 0%, #ff8a5c 100%);
    border-radius: 4px;
    transition: width 0.3s ease;
    width: 0%;
    box-shadow: 0 2px 4px rgba(242, 101, 50, 0.2);
}

.pdf-viewer {
    display: flex;
    flex-direction: column;
    height: 100%;
}

.document-container {
    flex: 1;
    position: relative;
    display: flex;
    flex-direction: column;
    min-height: 0; /* Fix for flexbox overflow */
}

/* PDF Toolbar */
.pdf-toolbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 18px;
    background: #f8f4eb;
    border-bottom: 1px solid #e6dcc8;
    gap: 18px;
    position: relative;
    z-index: 100;
    flex-shrink: 0; /* Prevent toolbar from shrinking */
    min-height: 60px; /* Ensure minimum height */
}

.toolbar-left,
.toolbar-center,
.toolbar-right {
    display: flex;
    align-items: center;
    gap: 10px;
}

.page-label {
    font-size: 13px;
    color: #8a7860;
    font-weight: 500;
}

.btn-icon {
    background: #fcf6eb;
    border: 1px solid #ded3be;
    padding: 6px;
    border-radius: 10px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s, border-color 0.2s;
    color: #6f614c;
}

.btn-icon:hover:not(:disabled) {
    background: #f1e7d3;
    border-color: #cbbda3;
}

.btn-icon:disabled {
    opacity: 0.35;
    cursor: not-allowed;
}

.btn-control {
    background: #fbf2df;
    border: 1px solid #dfcdb1;
    padding: 6px 14px;
    border-radius: 10px;
    cursor: pointer;
    transition: background 0.2s, border-color 0.2s;
    font-size: 13px;
    color: #6f614c;
}

.btn-control:hover {
    background: #f1e7d3;
    border-color: #cbbda3;
}

#zoomLevel {
    font-size: 14px;
    color: #5c4f3c;
    min-width: 48px;
    text-align: center;
}

#pageInput {
    width: 56px;
    padding: 4px 8px;
    border: 1px solid #d9c6a6;
    border-radius: 10px;
    text-align: center;
    font-size: 14px;
    background: #fcf6eb;
    color: #5c4f3c;
}

#pageTotal {
    font-size: 14px;
    color: #8a7860;
}

/* PDF Container */
.pdf-container {
    flex: 1;
    overflow: auto;
    background: #f4efe3;
    padding: 16px;
    height: 100%;
}

.pdf-page {
    background: white;
    margin: 0 auto 16px;
    box-shadow: 0 4px 12px rgba(45, 35, 20, 0.08);
    position: relative;
    display: block;
    border-radius: 4px;
}

.pdf-page canvas {
    display: block;
    width: 100%;
    height: 100%;
}

.pdf-page .textLayer {
    position: absolute;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
    overflow: hidden;
    opacity: 1;
    line-height: 1.0;
    mix-blend-mode: multiply;
}

.pdf-page .textLayer > span {
    color: transparent;
    position: absolute;
    white-space: pre;
    cursor: text;
    transform-origin: 0% 0%;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

.pdf-page .textLayer ::selection {
    background: rgba(242, 101, 50, 0.35);
    color: transparent;
}

/* Highlight layer for persistent highlights */
.highlightLayer {
    position: absolute;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
    pointer-events: none;
    z-index: 5;
}

.pdf-highlight {
    position: absolute;
    background: rgba(255, 235, 59, 0.4);
    mix-blend-mode: multiply;
    pointer-events: all;
    cursor: pointer;
    transition: background 0.2s;
}

.pdf-highlight:hover {
    background: rgba(255, 235, 59, 0.6);
}

.pdf-highlight.highlight-yellow {
    background: rgba(255, 235, 59, 0.4);
}

.pdf-highlight.highlight-green {
    background: rgba(76, 175, 80, 0.3);
}

.pdf-highlight.highlight-blue {
    background: rgba(33, 150, 243, 0.3);
}

.pdf-highlight.highlight-pink {
    background: rgba(233, 30, 99, 0.3);
}

/* PDF Link Annotations */
.pdf-link-annotation {
    z-index: 10;
    text-decoration: none;
    border-radius: 2px;
}

.pdf-link-annotation:hover {
    box-shadow: 0 2px 8px rgba(242, 101, 50, 0.3);
}

.epub-container {
    display: none;
    flex: 1;
    width: 100%;
    background: #f4efe3;
    overflow: hidden;
    position: relative;
    padding: 24px;
    box-sizing: border-box;
}

.text-container {
    display: none;
    flex: 1;
    overflow: auto;
    background: #f4efe3;
    padding: 16px;
}

.text-page {
    background: white;
    margin: 0 auto;
    padding: 40px;
    max-width: 900px;
    box-shadow: 0 4px 12px rgba(45, 35, 20, 0.08);
    border-radius: 4px;
    line-height: 1.8;
    white-space: pre-wrap;
    word-break: break-word;
    font-family: 'Source Code Pro', 'Menlo', 'Courier New', monospace;
    color: #2e2a24;
}

/* Webpage iframe container */
.iframe-container {
    width: 100%;
    height: 100%;
    background: white;
    overflow: hidden;
    position: relative;
}

.iframe-container iframe {
    width: 100%;
    height: calc(100% + 150px); /* Extra height to account for negative margin */
    margin-top: -150px; /* Hide top header/navigation area */
}

/* Remove padding from text-container when showing iframe */
.text-container:has(.iframe-container) {
    padding: 0 !important;
    overflow: hidden;
    background: white; /* Remove beige background */
}

.webpage-iframe {
    width: 100%;
    height: 100%;
    border: none;
}

/* Webpage Rendering Styles */
.webpage-container {
    background: white;
    margin: 0 auto;
    max-width: 900px;
    box-shadow: 0 4px 12px rgba(45, 35, 20, 0.08);
    border-radius: 4px;
}

.webpage-header {
    padding: 30px 40px;
    border-bottom: 2px solid #f6f2e8;
    background: #fdfaf3;
    border-radius: 4px 4px 0 0;
}

.webpage-header h1 {
    margin: 0 0 12px 0;
    color: #2e2a24;
    font-size: 28px;
    line-height: 1.3;
}

.webpage-url {
    display: inline-block;
    color: #f26532;
    text-decoration: none;
    font-size: 14px;
    transition: opacity 0.2s;
}

.webpage-url:hover {
    opacity: 0.8;
    text-decoration: underline;
}

.webpage-content {
    padding: 40px;
    color: #2e2a24;
    line-height: 1.7;
    font-size: 16px;
}

.webpage-content h1,
.webpage-content h2,
.webpage-content h3,
.webpage-content h4 {
    margin: 24px 0 16px 0;
    color: #2e2a24;
    font-weight: 600;
    line-height: 1.3;
}

.webpage-content h1 { font-size: 32px; }
.webpage-content h2 { font-size: 26px; }
.webpage-content h3 { font-size: 22px; }
.webpage-content h4 { font-size: 18px; }

.webpage-content p {
    margin: 0 0 16px 0;
}

.webpage-content ul,
.webpage-content ol {
    margin: 0 0 16px 0;
    padding-left: 30px;
}

.webpage-content li {
    margin-bottom: 8px;
}

.webpage-content a {
    color: #f26532;
    text-decoration: none;
}

.webpage-content a:hover {
    opacity: 0.8;
    text-decoration: underline;
}

.webpage-content img {
    max-width: 100%;
    height: auto;
    border-radius: 4px;
    margin: 16px 0;
}

.webpage-content table {
    border-collapse: collapse;
    width: 100%;
    margin: 16px 0;
}

.webpage-content table th,
.webpage-content table td {
    border: 1px solid #e6dcc8;
    padding: 8px 12px;
    text-align: left;
}

.webpage-content table th {
    background: #f6f2e8;
    font-weight: 600;
}

.webpage-content blockquote {
    margin: 16px 0;
    padding: 12px 20px;
    border-left: 4px solid #f26532;
    background: #fdfaf3;
}

.webpage-content code {
    background: #f6f2e8;
    padding: 2px 6px;
    border-radius: 3px;
    font-family: 'Courier New', monospace;
    font-size: 14px;
}

.webpage-content pre {
    background: #2e2a24;
    color: #f6f2e8;
    padding: 16px;
    border-radius: 4px;
    overflow-x: auto;
    margin: 16px 0;
}

.webpage-content pre code {
    background: none;
    padding: 0;
    color: inherit;
}

.text-container::-webkit-scrollbar {
    width: 10px;
}

.text-container::-webkit-scrollbar-thumb {
    background: rgba(97, 78, 54, 0.3);
    border-radius: 999px;
}

.epub-container iframe {
    border: none !important;
    width: 100%;
    height: 100%;
    background: #fffdf7;
    box-shadow: 0 10px 32px rgba(45, 35, 20, 0.08);
    border-radius: 8px;
}

/* EPUB Pages (rendered as HTML in pdf-container) */
.epub-page {
    background: white;
    margin: 0 auto 28px;
    padding: 40px;
    max-width: 800px;
    box-shadow: 0 10px 32px rgba(45, 35, 20, 0.08);
    border-radius: 8px;
    position: relative;
    display: block;
}

.epub-content {
    font-family: 'Georgia', 'Times New Roman', serif;
    font-size: 16px;
    line-height: 1.8;
    color: #2e2a24;
}

.epub-content h1,
.epub-content h2,
.epub-content h3,
.epub-content h4,
.epub-content h5,
.epub-content h6 {
    margin-top: 1.5em;
    margin-bottom: 0.5em;
    font-weight: 600;
    line-height: 1.3;
    color: #1a1715;
}

.epub-content h1 { font-size: 2em; }
.epub-content h2 { font-size: 1.5em; }
.epub-content h3 { font-size: 1.25em; }

.epub-content p {
    margin: 1em 0;
    text-align: justify;
}

.epub-content img {
    max-width: 100%;
    height: auto;
    display: block;
    margin: 1.5em auto;
}

.epub-content a {
    color: #f26532;
    text-decoration: none;
}

.epub-content a:hover {
    text-decoration: underline;
}

.epub-content ul,
.epub-content ol {
    margin: 1em 0;
    padding-left: 2em;
}

.epub-content li {
    margin: 0.5em 0;
}

.epub-content blockquote {
    border-left: 4px solid #f26532;
    padding-left: 1.5em;
    margin: 1.5em 0;
    font-style: italic;
    color: #6f614c;
}

.epub-content code {
    background: #f4efe3;
    padding: 0.2em 0.4em;
    border-radius: 3px;
    font-family: 'Monaco', 'Courier New', monospace;
    font-size: 0.9em;
}

.epub-content pre {
    background: #f4efe3;
    padding: 1em;
    border-radius: 8px;
    overflow-x: auto;
    margin: 1.5em 0;
}

.epub-content pre code {
    background: none;
    padding: 0;
}

/* Make EPUB text selectable */
.epub-page ::selection {
    background: rgba(242, 101, 50, 0.35);
}

/* Context Menu */
.context-menu {
    position: fixed;
    background: #fffdf7;
    border-radius: 12px;
    border: 1px solid #e6dcc8;
    box-shadow: 0 12px 32px rgba(40, 30, 15, 0.12);
    padding: 6px 0;
    z-index: 1000;
    min-width: 180px;
}

.context-menu-item {
    padding: 10px 18px;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 10px;
    font-size: 14px;
    color: #2e2a24;
    transition: background 0.2s, color 0.2s;
}

.context-menu-item:hover {
    background: #f1e7d3;
    color: #f26532;
}

.context-menu-item svg {
    width: 16px;
    height: 16px;
    stroke: currentColor;
}

/* Chat Sidebar */
.chat-sidebar {
    flex: 0 0 35%;
    background: #fffdf7;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    min-width: 260px;
    min-height: 0; /* Fix for flexbox in iOS Safari */
}

.chat-header {
    background: #f8f4eb;
    color: #2e2a24;
    padding: 12px 16px;
    border-bottom: 1px solid #e6dcc8;
}

.chat-header-content {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%;
}

.chat-header-controls {
    display: flex;
    align-items: center;
    gap: 8px;
}

/* Model Selector Styles */
.model-selector {
    display: flex;
    align-items: center;
    gap: 8px;
    background: rgba(242, 101, 50, 0.08);
    padding: 6px 10px;
    border-radius: 10px;
    border: 1px solid rgba(242, 101, 50, 0.15);
}

.model-label {
    font-size: 13px;
    font-weight: 600;
    color: #f26532;
    user-select: none;
}

.model-select {
    background: rgba(255, 255, 255, 0.9);
    border: 1px solid rgba(242, 101, 50, 0.2);
    border-radius: 6px;
    padding: 4px 8px;
    font-size: 13px;
    font-weight: 500;
    color: #2d3748;
    cursor: pointer;
    outline: none;
    transition: all 0.2s ease;
}

.model-select:hover {
    background: rgba(255, 255, 255, 1);
    border-color: rgba(242, 101, 50, 0.4);
}

.model-select:focus {
    border-color: #f26532;
    box-shadow: 0 0 0 3px rgba(242, 101, 50, 0.1);
}

.font-size-control {
    display: flex;
    align-items: center;
    gap: 6px;
    background: rgba(242, 101, 50, 0.08);
    padding: 4px 8px;
    border-radius: 10px;
    border: 1px solid rgba(242, 101, 50, 0.15);
}

.font-size-label {
    font-size: 14px;
    font-weight: 600;
    color: #f26532;
    min-width: 24px;
    text-align: center;
    user-select: none;
    transition: transform 0.2s ease, font-size 0.2s ease;
}

.chat-header h3 {
    font-size: 20px;
    margin-bottom: 4px;
    font-weight: 600;
}

.chat-subtitle {
    font-size: 13px;
    color: #8a7860;
}

.btn-icon-header {
    background: transparent;
    border: none;
    padding: 6px;
    border-radius: 8px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s, transform 0.2s;
}

.btn-icon-header:hover {
    background: rgba(242, 101, 50, 0.12);
    transform: translateY(-1px);
}

.btn-icon-header:active {
    transform: translateY(0);
}

.btn-icon-header svg {
    stroke: #f26532;
}

.chat-header-controls > .btn-icon-header {
    background: #fff5e9;
    padding: 8px 10px;
    border-radius: 10px;
}

.chat-header-controls > .btn-icon-header:hover {
    background: #ffe8d4;
}

.btn-secondary {
    background: #f3e7d6;
    color: #8a7860;
    border: none;
    padding: 12px 24px;
    border-radius: 8px;
    font-size: 15px;
    cursor: pointer;
    transition: background 0.2s ease, transform 0.2s ease;
}

.btn-secondary:hover {
    background: #ecdac0;
    transform: translateY(-1px);
}

#apiKeyBtn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    min-width: 132px;
    justify-content: center;
}

#apiKeyBtn svg {
    stroke: #f26532;
}

.api-key-status {
    font-size: 12px;
    font-weight: 500;
    color: #8a7860;
}

.api-key-status.set {
    color: #2e2a24;
}

.modal {
    position: fixed;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 2000;
}

.modal.hidden {
    display: none;
}

.modal-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(25, 18, 9, 0.4);
    backdrop-filter: blur(2px);
}

.modal-content {
    position: relative;
    background: #fffdf7;
    border-radius: 16px;
    padding: 24px;
    width: min(90vw, 420px);
    box-shadow: 0 18px 36px rgba(40, 30, 15, 0.18);
    border: 1px solid #e6dcc8;
    display: flex;
    flex-direction: column;
    gap: 16px;
    z-index: 1;
}

.modal-close {
    position: absolute;
    top: 12px;
    right: 12px;
    background: none;
    border: none;
    font-size: 22px;
    cursor: pointer;
    color: #8a7860;
}

.modal-close:hover {
    color: #f26532;
}

.modal-description {
    font-size: 14px;
    color: #8a7860;
    line-height: 1.5;
}

#apiKeyForm {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

#apiKeyForm label {
    font-weight: 600;
    font-size: 14px;
    color: #2e2a24;
}

#apiKeyInput {
    padding: 12px;
    border-radius: 10px;
    border: 1px solid #d8c8aa;
    font-size: 15px;
    background: #fffaf1;
}

#apiKeyInput:focus {
    outline: none;
    border-color: #f26532;
    box-shadow: 0 0 0 3px rgba(242, 101, 50, 0.18);
}

.modal-actions {
    display: flex;
    justify-content: flex-end;
    gap: 12px;
    margin-top: 8px;
}

.modal-hint {
    font-size: 12px;
    color: #8a7860;
    line-height: 1.4;
}

.chat-messages {
    flex: 1;
    padding: 0;
    overflow-y: auto;
    background: #fdfaf3;
    display: flex;
    flex-direction: column;
}

.welcome-message {
    text-align: left;
    padding: 12px 18px;
    color: #a19078;
    border-bottom: 1px solid rgba(214, 200, 170, 0.4);
}

.message {
    display: flex;
    align-items: flex-start;
    gap: 8px;
    padding: 12px 18px;
    border-bottom: 1px solid rgba(214, 200, 170, 0.4);
    animation: fadeIn 0.3s ease-in;
    color: #2e2a24;
    flex-wrap: wrap;
}

.message:last-child {
    border-bottom: none;
}

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

.message-user {
    flex-direction: row;
}

.message::before {
    font-size: 18px;
    line-height: 1;
    margin-top: 4px;
}

.message-user::before {
    content: '👤';
}

.message-assistant::before {
    content: '✨';
}

.message-content {
    max-width: 100%;
    padding: 8px 12px;
    border-radius: 10px;
    word-wrap: break-word;
    border: 1px solid #e6dcc8;
    background: white;
    font-size: var(--chat-font-size);
    line-height: 1.6;
}

.message-user .message-content {
    background: #fff3e5;
    border-color: #f2d2b1;
}

.message-assistant .message-content {
    background: #ffffff;
    border-color: #e6dcc8;
}

.message-stopped .message-content {
    border-color: #f2b08c;
    box-shadow: inset 0 0 0 1px rgba(242, 176, 140, 0.2);
}

.message-actions {
    margin-left: auto;
    display: flex;
    gap: 8px;
    align-items: center;
    flex-shrink: 0;
}

.message-action-btn {
    background: transparent;
    border: none;
    border-radius: 8px;
    padding: 6px;
    font-size: 12px;
    color: #8a7860;
    cursor: pointer;
    transition: all 0.2s ease;
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0.6;
}

.message-action-btn:hover {
    background: rgba(242, 101, 50, 0.1);
    color: #f26532;
    opacity: 1;
    transform: scale(1.1);
}

.message-action-btn svg {
    stroke: currentColor;
}

.message-action-btn:focus-visible {
    outline: 2px solid #e3b789;
    outline-offset: 2px;
}

.message-stop-notice {
    margin-top: 12px;
    padding: 6px 10px;
    border-left: 3px solid #f26532;
    background: #fff3e5;
    font-size: 12px;
    color: #a15a2c;
    border-radius: 8px;
}

/* Markdown styles in messages */
.message-content h1,
.message-content h2,
.message-content h3,
.message-content h4,
.message-content h5,
.message-content h6 {
    margin-top: 12px;
    margin-bottom: 8px;
    font-weight: 600;
}

.message-content h1 { font-size: 1.5em; }
.message-content h2 { font-size: 1.3em; }
.message-content h3 { font-size: 1.1em; }

.message-content p {
    margin: 8px 0;
    line-height: 1.6;
}

.message-content ul,
.message-content ol {
    margin: 8px 0;
    padding-left: 20px;
}

.message-content li {
    margin: 4px 0;
    line-height: 1.5;
}

.message-content code {
    background: rgba(0, 0, 0, 0.05);
    padding: 2px 6px;
    border-radius: 3px;
    font-family: 'Monaco', 'Courier New', monospace;
    font-size: 0.9em;
}

.message-content pre {
    background: #f0ebe0;
    padding: 0;
    border-radius: 8px;
    overflow: hidden;
    margin: 10px 0;
    position: relative;
}

.code-block-wrapper {
    position: relative;
}

.code-block-header {
    background: #e9dfca;
    padding: 9px 14px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid #d8c8aa;
}

.code-language {
    font-size: 12px;
    color: #6f614c;
    font-family: 'Monaco', 'Courier New', monospace;
}

.copy-code-btn {
    background: #fffaf0;
    border: 1px solid #d8c8aa;
    color: #6f614c;
    padding: 4px 12px;
    border-radius: 10px;
    cursor: pointer;
    font-size: 11px;
    display: flex;
    align-items: center;
    gap: 4px;
    transition: all 0.2s;
}

.copy-code-btn:hover {
    background: #f1e7d3;
    color: #2e2a24;
}

.copy-code-btn.copied {
    background: #c3e7c0;
    border-color: #9bd595;
    color: #275826;
}

.message-content pre code {
    background: none;
    padding: 14px 16px;
    display: block;
    overflow-x: auto;
    color: #4a4032;
}

/* Highlight.js overrides */
.message-content pre code.hljs {
    background: transparent;
    padding: 14px;
}

.message-content blockquote {
    border-left: 3px solid #f26532;
    padding-left: 12px;
    margin: 8px 0;
    color: #6f614c;
}

.message-content strong {
    font-weight: 600;
}

.message-content em {
    font-style: italic;
}

.message-content a {
    color: #667eea;
    text-decoration: none;
}

.message-content a:hover {
    text-decoration: underline;
}

.message-loading {
    text-align: left;
}

.message-loading .message-content {
    background: white;
    color: #666;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.typing-indicator {
    display: inline-flex;
    gap: 4px;
    padding: 12px 16px;
}

.typing-indicator span {
    width: 8px;
    height: 8px;
    background: #d7c6a9;
    border-radius: 50%;
    animation: typing 1.4s infinite;
}

.typing-indicator span:nth-child(2) {
    animation-delay: 0.2s;
}

.typing-indicator span:nth-child(3) {
    animation-delay: 0.4s;
}

@keyframes typing {
    0%, 60%, 100% {
        transform: translateY(0);
        opacity: 0.5;
    }
    30% {
        transform: translateY(-10px);
        opacity: 1;
    }
}

/* Streaming cursor */
.streaming-cursor {
    display: inline-block;
    color: #f26532;
    animation: blink 1s infinite;
    margin-left: 2px;
}

@keyframes blink {
    0%, 49% {
        opacity: 1;
    }
    50%, 100% {
        opacity: 0;
    }
}

.chat-input-container {
    display: flex;
    gap: 8px;
    padding: 12px;
    padding-bottom: calc(12px + env(safe-area-inset-bottom)); /* iOS safe area */
    background: #f8f4eb;
    border-top: 1px solid #e6dcc8;
    flex-shrink: 0; /* Never shrink the input area */
}

#stopStreamBtn {
    display: none;
}

.btn-stop {
    background: #c24936;
    color: white;
    border: none;
    width: 46px;
    height: 46px;
    border-radius: 12px;
    cursor: pointer;
    display: none;
    align-items: center;
    justify-content: center;
    transition: transform 0.2s, box-shadow 0.2s, opacity 0.2s;
}

.btn-stop svg {
    pointer-events: none;
}

.btn-stop:hover:not(:disabled) {
    transform: translateY(-2px);
    box-shadow: 0 8px 18px rgba(194, 73, 54, 0.35);
}

.btn-stop:disabled {
    opacity: 0.6;
    cursor: not-allowed;
}

.btn-stop.is-stopping {
    opacity: 0.7;
}

#chatInput {
    flex: 1;
    border: 1px solid #d9c6a6;
    border-radius: 12px;
    padding: 12px 14px;
    font-family: inherit;
    font-size: 14px;
    resize: none;
    max-height: 120px;
    transition: border-color 0.2s;
    background: #fffdf7;
    color: #2e2a24;
}

#chatInput:focus {
    outline: none;
    border-color: #f26532;
}

#chatInput:disabled {
    background: #f2ecde;
    cursor: not-allowed;
}

.btn-send {
    background: #f26532;
    color: white;
    border: none;
    width: 46px;
    height: 46px;
    border-radius: 12px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: transform 0.2s, box-shadow 0.2s;
}

.btn-send:hover:not(:disabled) {
    transform: translateY(-2px);
    box-shadow: 0 8px 18px rgba(242, 101, 50, 0.35);
}

.btn-send:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

/* Scrollbar Styling */
.chat-messages::-webkit-scrollbar {
    width: 8px;
}

.chat-messages::-webkit-scrollbar-track {
    background: #f2ebdd;
}

.chat-messages::-webkit-scrollbar-thumb {
    background: #d7c6a9;
    border-radius: 4px;
}

.chat-messages::-webkit-scrollbar-thumb:hover {
    background: #c4af87;
}

/* Responsive - Mobile First */

/* Mobile phones (< 768px) - Chat-first experience */
@media (max-width: 767px) {
    /* Fix iOS Safari viewport issues */
    body {
        position: fixed;
        width: 100%;
        height: 100vh;
        height: 100dvh; /* Dynamic viewport height for iOS */
        overflow: hidden;
    }

    .app-wrapper {
        height: 100vh;
        height: 100dvh; /* Dynamic viewport height for iOS */
    }

    /* Stack vertically with chat taking priority */
    .container {
        flex-direction: column-reverse;
        height: auto;
        min-height: 0;
    }

    /* Hide pane resizer on mobile */
    .pane-resizer {
        display: none;
    }

    /* Hide headers when user is chatting (triggered by JS) */
    .mobile-hide-headers .app-header,
    .mobile-hide-headers .chat-header {
        max-height: 0;
        overflow: hidden;
        padding: 0;
        border: none;
        transition: all 0.3s ease;
    }

    /* Show headers on hover/tap (so user can still access controls) */
    .mobile-hide-headers:hover .app-header,
    .mobile-hide-headers.show-headers .app-header,
    .mobile-hide-headers:hover .chat-header,
    .mobile-hide-headers.show-headers .chat-header {
        max-height: 200px;
        padding: 12px 16px;
    }

    .mobile-hide-headers .chat-header {
        padding: 10px 12px;
    }

    /* PDF section hidden by default on mobile, shows when document loaded */
    .pdf-section {
        flex: 0 0 0;
        min-height: 0;
        max-height: 0;
        width: 100%;
        min-width: 0;
        border-right: none;
        border-bottom: 1px solid #e6dcc8;
        overflow: hidden;
        transition: all 0.3s ease;
    }

    /* When PDF is loaded, show it collapsed */
    .pdf-section.has-document {
        flex: 0 0 60px;
        min-height: 60px;
        max-height: 60px;
        cursor: pointer;
        position: relative;
    }

    /* Add tap indicator */
    .pdf-section.has-document::after {
        content: '⌃ Tap to expand';
        position: absolute;
        bottom: 8px;
        left: 50%;
        transform: translateX(-50%);
        background: rgba(242, 101, 50, 0.9);
        color: white;
        padding: 4px 12px;
        border-radius: 12px;
        font-size: 11px;
        font-weight: 600;
        pointer-events: none;
        z-index: 10;
        opacity: 0.9;
    }

    /* When expanded, show more */
    .pdf-section.mobile-expanded {
        flex: 0 0 40vh;
        min-height: 40vh;
        max-height: 40vh;
    }

    /* Hide tap indicator when expanded */
    .pdf-section.mobile-expanded::after {
        content: '⌄ Tap to collapse';
        top: 8px;
        bottom: auto;
    }

    /* Chat sidebar becomes main view */
    .chat-sidebar {
        flex: 1 1 auto;
        width: 100%;
        min-width: 0;
        min-height: 0;
        height: auto;
        display: flex;
        flex-direction: column;
    }

    /* Ensure chat messages can scroll properly */
    .chat-messages {
        flex: 1 1 auto;
        min-height: 0;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch; /* Smooth scrolling on iOS */
    }

    /* Ensure input stays at bottom */
    .chat-input-container {
        flex: 0 0 auto;
        padding: 12px;
        padding-bottom: calc(12px + env(safe-area-inset-bottom, 20px)); /* Extra padding for iOS */
    }

    /* Make all touch targets bigger (44px minimum for iOS) */
    .btn-icon {
        min-width: 44px;
        min-height: 44px;
        padding: 10px;
    }

    .btn-control {
        min-height: 44px;
        padding: 10px 16px;
        font-size: 14px;
    }

    .message-action-btn {
        min-width: 44px;
        min-height: 44px;
        padding: 12px;
    }

    .btn-send,
    .btn-stop {
        min-width: 48px;
        min-height: 48px;
    }

    /* Simplify toolbar - hide some controls */
    #fitWidth,
    #fitPage {
        display: none;
    }

    .toolbar-left {
        gap: 6px;
    }

    .toolbar-center {
        gap: 6px;
    }

    /* Make page input bigger */
    #pageInput {
        width: 64px;
        padding: 8px;
        font-size: 16px; /* Prevents iOS zoom on focus */
    }

    /* Chat input bigger to prevent iOS zoom */
    #chatInput {
        font-size: 16px;
        padding: 14px;
    }

    /* Header adjustments */
    .app-header {
        padding: 10px 12px;
    }

    .brand-name {
        font-size: 16px;
    }

    /* Hide "Upload" text on button, just show icon */
    .btn-upload {
        padding: 10px 12px;
    }

    .btn-upload svg {
        margin-right: 0;
    }

    /* Reduce PDF container padding */
    .pdf-container {
        padding: 8px;
    }

    /* Smaller margins for messages */
    .message {
        padding: 10px 12px;
    }

    .message-content {
        font-size: 15px;
    }

    /* Make chat header more compact */
    .chat-header {
        padding: 10px 12px;
        flex-shrink: 0; /* Never shrink the header */
    }

    .chat-header h3 {
        font-size: 18px;
    }

    /* Fix for iOS Safari - ensure proper height calculation */
    .chat-sidebar {
        max-height: 100%;
        overflow: hidden;
    }

    /* Adjust context menu for touch */
    .context-menu {
        min-width: 200px;
    }

    .context-menu-item {
        padding: 14px 20px;
        font-size: 15px;
    }
}

/* Tablets (768px - 1024px) - Balanced experience */
@media (min-width: 768px) and (max-width: 1024px) {
    .container {
        flex-direction: column;
        height: auto;
    }

    .pane-resizer {
        display: none;
    }

    /* Better proportions for tablet */
    .pdf-section {
        flex: 0 0 50%;
        min-height: 400px;
        width: 100%;
        min-width: 0;
        border-right: none;
        border-bottom: 1px solid #e6dcc8;
    }

    .chat-sidebar {
        flex: 1;
        width: 100%;
        min-width: 0;
        min-height: 300px;
    }

    /* Bigger touch targets for tablet */
    .btn-icon,
    .btn-control {
        min-width: 40px;
        min-height: 40px;
    }

    .message-action-btn {
        min-width: 40px;
        min-height: 40px;
        padding: 10px;
    }

    /* Prevent iOS zoom */
    #chatInput,
    #pageInput {
        font-size: 16px;
    }
}

/* Tablet landscape mode - side by side */
@media (min-width: 768px) and (max-width: 1024px) and (orientation: landscape) {
    .container {
        flex-direction: row;
    }

    .pdf-section {
        flex: 0 0 58%;
        border-right: 1px solid #e6dcc8;
        border-bottom: none;
    }

    .chat-sidebar {
        flex: 0 0 42%;
    }

    /* Show thicker resizer for touch */
    .pane-resizer {
        display: flex;
        width: 20px;
        cursor: col-resize;
    }

    .pane-resizer::before {
        width: 3px;
        height: 60px;
    }
}
