/* ==========================================================================
   Fieldbook UI styles
   Dark theme, served offline from a Raspberry Pi. No external assets.
   ========================================================================== */

:root {
    --bg: #383838;
    --bg-panel: #2E2E2E;
    --bg-raised: #3f3e3e;
    --border: #666666;
    --text: #fafafa;
    --text-soft: #FFFFC2;
    --text-muted: #A9A9A9;
    --accent: #FA8072;
    --accent-user: #00468BFF;
    --app-height: 100dvh;
}

html {
    background-color: var(--bg);
    -webkit-text-size-adjust: 100%;
}

body {
    margin: 0;
}

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

.visually-hidden {
    position: absolute !important;
    width: 1px;
    height: 1px;
    margin: -1px;
    padding: 0;
    overflow: hidden;
    clip: rect(0 0 0 0);
    white-space: nowrap;
    border: 0;
}

/* Page container (applied to <body>) */
.container {
    max-width: 1500px;
    margin: 0 auto;
    padding: 20px;
    font-family: Arial, sans-serif;
    background-color: var(--bg);
    min-height: 100vh;
    min-height: 100dvh;
    color: var(--text);
}

.page-content {
    display: block;
}

/* --------------------------------------------------------------------------
   Header / navigation
   -------------------------------------------------------------------------- */

.header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 15px;
    margin-bottom: 20px;
}

.logo-link {
    display: inline-flex;
    flex: 0 1 auto;
    min-width: 0;
}

.logo {
    width: 15vw;
    min-width: 140px;
    max-width: 260px;
    height: auto;
    display: block;
}

.user-info {
    font-size: 0.9em;
    color: var(--text);
    display: flex;
    align-items: center;
    gap: 15px;
}

.username {
    font-weight: bold;
    color: var(--accent-user);
}

.nav-icons {
    display: flex;
    gap: 15px;
}

.button {
    padding: 10px;
    color: var(--text-muted);
    text-decoration: none;
    border-radius: 4px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 44px;
    min-height: 44px;
}

.button .svg-icon {
    font-size: 1.2em;
}

.button:hover,
.button:focus-visible {
    color: var(--accent);
    background-color: var(--bg-raised);
}

.button.active {
    color: var(--accent);
    background-color: var(--bg-panel);
}

.logout-button {
    padding: 5px 10px;
    background-color: #2C3539;
    color: var(--accent);
    text-decoration: none;
    border-radius: 4px;
    font-size: 0.9em;
    border: 2px solid var(--border);
    transition: all 0.3s ease;
    white-space: nowrap;
}

.logout-button:hover {
    background-color: #1F2937;
    border-color: var(--text-muted);
}

/* --------------------------------------------------------------------------
   Login
   -------------------------------------------------------------------------- */

.logo-container {
    text-align: center;
    margin-bottom: 30px;
}

.logo-container img {
    width: 60%;
    min-width: 160px;
    max-width: 320px;
    height: auto;
}

.login-container {
    max-width: 400px;
    margin: 100px auto;
    padding: 20px;
    border: 1px solid #ccc;
    border-radius: 8px;
    background-color: var(--bg-raised);
    color: var(--accent);
}

.login-form {
    display: flex;
    flex-direction: column;
    gap: 20px;
}

.login-button {
    padding: 10px;
    background-color: #484848;
    color: var(--accent);
    border: none;
    border-radius: 4px;
    cursor: pointer;
    font-size: 16px;
}

.login-button:hover {
    background-color: #565656;
}

.form-group {
    display: flex;
    flex-direction: column;
    gap: 5px;
}

.form-input {
    padding: 8px;
    border: 1px solid var(--border);
    border-radius: 4px;
    font-size: 16px;
    background-color: var(--bg-raised);
    color: var(--accent);
    width: 100%;
}

.form-input:focus {
    outline: none;
    border-color: var(--text-muted);
    box-shadow: 0 0 5px rgba(169, 169, 169, 0.5);
}

.error-message {
    background-color: #f8d7da;
    color: #721c24;
    padding: 10px;
    border-radius: 4px;
    margin-bottom: 20px;
}

/* --------------------------------------------------------------------------
   Error page (dark theme)
   -------------------------------------------------------------------------- */

.error-page {
    max-width: 800px;
    margin: 0 auto;
    padding: 20px;
    text-align: center;
    color: var(--text);
}

.error-page h1 {
    color: var(--accent);
}

.error-page .error-message {
    background-color: var(--bg-raised);
    color: var(--accent);
    padding: 20px;
    border: 1px solid var(--accent);
    border-radius: 4px;
    margin: 20px 0;
}

.back-link {
    display: inline-block;
    margin-top: 20px;
    padding: 10px 20px;
    background-color: var(--bg-raised);
    color: var(--accent);
    text-decoration: none;
    border: 2px solid var(--border);
    border-radius: 4px;
}

.back-link:hover {
    background-color: #1F2937;
    border-color: var(--text-muted);
}

/* --------------------------------------------------------------------------
   Chat
   -------------------------------------------------------------------------- */

/* The chat page is a full-height flex column so the input stays pinned at the
   bottom and, on phones, above the on-screen keyboard. --app-height is updated
   from the visualViewport by chat.js (iOS Safari does not shrink 100dvh). */
body.chat-page {
    height: 100vh;
    height: var(--app-height, 100dvh);
    min-height: 0;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.chat-page .header,
.chat-page .footer {
    flex: 0 0 auto;
}

.chat-page .page-content {
    flex: 1 1 auto;
    min-height: 0;
    display: flex;
    flex-direction: column;
}

.chat-container {
    display: flex;
    flex-direction: column;
    height: 70vh;
    border: 1px solid #ccc;
    border-radius: 8px;
    background: var(--bg-panel);
    position: relative;
    overflow: hidden;
}

.chat-page .chat-container {
    flex: 1 1 auto;
    min-height: 0;
    height: auto;
}

.connection-banner {
    flex: 0 0 auto;
    background: #5a3a36;
    color: var(--text);
    border-bottom: 1px solid var(--accent);
    padding: 8px 15px;
    font-size: 0.9em;
    text-align: center;
}

.connection-banner[hidden] {
    display: none;
}

.banner-retry {
    margin-left: 8px;
    padding: 4px 10px;
    background: var(--bg-raised);
    color: var(--accent);
    border: 1px solid var(--accent);
    border-radius: 4px;
    cursor: pointer;
    font-size: inherit;
}

.chat-toolbar {
    flex: 0 0 auto;
    display: flex;
    justify-content: flex-end;
    padding: 4px 8px;
    border-bottom: 1px solid #ccc;
    background: var(--bg-panel);
}

.new-chat-button {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    min-height: 44px;
    padding: 8px 12px;
    background: transparent;
    color: var(--accent);
    border: 1px solid transparent;
    border-radius: 4px;
    cursor: pointer;
    font-size: 0.9em;
}

.new-chat-button:hover,
.new-chat-button:focus-visible {
    background: var(--bg-raised);
    border-color: var(--accent);
}

.new-chat-button:disabled {
    opacity: 0.5;
    cursor: default;
}

.chat-messages {
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding: 20px;
    border-bottom: 1px solid #ccc;
    background: var(--bg-panel);
}

.message {
    margin-bottom: 15px;
    max-width: 80%;
}

.user-message {
    margin-left: auto;
}

.assistant-message {
    margin-right: auto;
}

.message-content {
    display: flex;
    flex-direction: column;
    padding: 10px 15px;
    border-radius: 15px;
    background-color: var(--bg-raised);
    overflow-wrap: anywhere;
}

.user-message .message-content {
    background-color: var(--accent-user);
    color: white;
}

.markdown-content {
    line-height: 1.6;
    color: var(--text-soft);
}

.markdown-content code {
    background-color: var(--text-muted);
    padding: 2px 4px;
    border-radius: 4px;
    color: #e83e8c;
}

.markdown-content pre {
    background-color: var(--text-muted);
    padding: 1rem;
    border-radius: 8px;
    overflow-x: auto;
}

.markdown-content p {
    margin: 0.5em 0;
}

.markdown-content ul, .markdown-content ol {
    margin: 0.5em 0;
    padding-left: 1.5em;
}

.markdown-content table {
    display: block;
    max-width: 100%;
    overflow-x: auto;
    border-collapse: collapse;
}

.markdown-content img {
    max-width: 100%;
    height: auto;
}

.time-taken {
    font-size: 0.8em;
    color: #9a9a9a;
    margin-top: 8px;
    font-style: italic;
}

.chat-input {
    flex: 0 0 auto;
    display: flex;
    padding: 20px;
    border-top: 1px solid #383636;
    gap: 10px;
    position: relative;
}

.loading-wrapper {
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
    top: -30px;
    pointer-events: none;
}

.loading-indicator {
    opacity: 0;
    transition: opacity 0.2s ease-in-out;
    background: var(--bg-raised);
    border: 1px solid #e0e0e0;
    border-radius: 20px;
    padding: 8px 16px;
    box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}

.loading-indicator.is-active {
    opacity: 1;
}

.typing-indicator {
    display: flex;
    gap: 4px;
    align-items: center;
}

.dot {
    width: 8px;
    height: 8px;
    background: #111111;
    border-radius: 50%;
    animation: bounce 1.4s infinite ease-in-out;
}

.dot:nth-child(1) { animation-delay: -0.32s; }
.dot:nth-child(2) { animation-delay: -0.16s; }

@keyframes bounce {
    0%, 80%, 100% {
        transform: scale(0);
    }
    40% {
        transform: scale(1.0);
    }
}

.message-input {
    flex-grow: 1;
    min-width: 0;
    padding: 10px;
    border: 1px solid var(--border);
    border-radius: 4px;
    font-size: 16px; /* 16px stops iOS from zooming on focus */
    background: var(--bg-panel);
    color: var(--accent);
    transition: all 0.2s ease-in-out;
}

.message-input:focus {
    outline: none;
    border-color: var(--bg-raised);
    box-shadow: 0 0 5px rgba(169, 169, 169, 0.5);
}

.message-input:disabled {
    background-color: var(--text-muted);
    color: var(--border);
    cursor: not-allowed;
}

.send-button {
    padding: 10px 20px;
    background-color: var(--bg-raised);
    color: #f36050;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    transition: all 0.2s ease-in-out;
    min-height: 44px;
}

.send-button:disabled {
    background-color: #484848;
    cursor: not-allowed;
    opacity: 0.7;
}

.send-button:hover:not(:disabled) {
    background-color: #0056b3;
}

/* Sources and feedback inside an assistant message */

.message-sources {
    margin-top: 8px;
}

.sources-header {
    color: var(--text-muted);
    font-size: 0.85em;
}

.sources-list {
    margin: 4px 0 0;
    padding-left: 1.2em;
    font-size: 0.9em;
}

.source-name {
    color: var(--text-muted);
}

.source-relevance {
    color: var(--text-muted);
}

.message-feedback {
    display: flex;
    gap: 8px;
    margin-top: 8px;
    justify-content: flex-end;
    margin-left: auto;
}

.feedback-button {
    background: none;
    border: 1px solid #e0e0e0;
    padding: 4px 8px;
    cursor: pointer;
    border-radius: 4px;
    transition: all 0.2s ease-in-out;
    font-size: 14px;
}

.feedback-button:hover:not(:disabled) {
    background-color: #f0f0f0;
}

.feedback-button.selected {
    border-color: #007bff;
    background-color: #e7f3ff;
}

.feedback-button.selected-positive {
    border-color: #28a745;
    background-color: #e7ffe7;
}

.feedback-button.selected-negative {
    border-color: #dc3545;
    background-color: #ffe7e7;
}

.feedback-button:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

/* System / error notices inside the chat stream */

.system-message {
    text-align: center;
    margin: 10px auto;
    max-width: 100%;
}

.system-content {
    color: #d0d0d0;
    font-style: italic;
    font-size: 0.9em;
    background-color: var(--bg-raised);
    display: inline-block;
    padding: 5px 10px;
    border-radius: 10px;
}

.system-content.error-content {
    color: var(--accent);
    border: 1px solid var(--accent);
}

/* --------------------------------------------------------------------------
   File browser
   -------------------------------------------------------------------------- */

.files-container {
    background: var(--bg-raised);
    border-radius: 8px;
    border: 1px solid #ccc;
    margin-top: 20px;
    overflow: hidden;
}

.breadcrumb {
    padding: 15px 20px;
    background-color: var(--bg-panel);
    border-radius: 8px 8px 0 0;
    border-bottom: 1px solid #ccc;
    color: var(--text-soft);
    overflow-wrap: anywhere;
}

.files-header {
    display: grid;
    grid-template-columns: 1fr 100px 200px;
    padding: 15px 20px;
    background-color: var(--bg-panel);
    font-weight: bold;
    border-bottom: 1px solid #ccc;
    color: var(--text-soft);
}

.col-right {
    text-align: right;
}

.files-list {
    list-style: none;
    padding: 0;
    margin: 0;
}

.files-list li {
    display: grid;
    grid-template-columns: 1fr 100px 200px;
    padding: 12px 20px;
    border-bottom: 1px solid #555;
    align-items: center;
    background: var(--bg-panel);
}

.files-list li:hover {
    background-color: #717171;
}

.files-list a {
    color: var(--text-soft);
    text-decoration: none;
    display: flex;
    align-items: center;
    gap: 10px;
    min-width: 0;
    overflow-wrap: anywhere;
}

.files-list a:hover {
    color: #0056b3;
}

.item-meta {
    color: var(--text-soft);
    font-size: 0.9em;
    text-align: right;
}

.icon {
    font-size: 1.2em;
}

/* --------------------------------------------------------------------------
   Search
   -------------------------------------------------------------------------- */

.search-container {
    background: var(--bg-panel);
    border-radius: 8px;
    border: 1px solid #ccc;
    margin-top: 20px;
    padding: 20px;
}

.search-form {
    display: flex;
    gap: 10px;
    margin-bottom: 20px;
}

.search-input {
    flex-grow: 1;
    min-width: 0;
    padding: 10px;
    border: 1px solid #ccc;
    border-radius: 4px;
    font-size: 16px;
    background: var(--bg-panel);
    color: var(--accent);
}

.search-button {
    padding: 10px 20px;
    background-color: var(--bg-raised);
    color: var(--accent);
    border: none;
    border-radius: 4px;
    cursor: pointer;
    min-height: 44px;
}

.search-results {
    background: var(--bg-panel);
    list-style: none;
    padding: 0;
    margin: 0;
}

.result-item {
    background: var(--bg-raised);
    border-radius: 4px;
    margin-bottom: 10px;
    padding: 15px;
}

.result-item a {
    color: var(--text-soft);
    text-decoration: none;
    display: flex;
    align-items: center;
    gap: 10px;
    margin-bottom: 5px;
    overflow-wrap: anywhere;
}

.result-item a:hover {
    color: #0056b3;
}

.result-path {
    color: #f1f1b5;
    font-size: 0.9em;
    overflow-wrap: anywhere;
}

.result-excerpt {
    color: #f1f1b5;
    margin-top: 10px;
    font-size: 0.9em;
}

.no-results {
    color: rgb(204, 204, 205);
}

/* --------------------------------------------------------------------------
   Help (accordion)
   -------------------------------------------------------------------------- */

.help-content {
    margin: 0 auto;
    padding: 20px;
    border: 1px solid #ccc;
    border-radius: 8px;
    background: var(--bg-panel);
}

.help-section {
    margin-bottom: 15px;
    background-color: var(--bg-panel);
    border-radius: 8px;
    border: 1px solid var(--border);
    overflow: hidden;
}

.section-heading {
    margin: 0;
    font-size: 1.5em;
}

.section-header {
    width: 100%;
    padding: 15px 20px;
    cursor: pointer;
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 10px;
    background-color: var(--bg-panel);
    border: none;
    color: var(--accent);
    font: inherit;
    text-align: left;
    transition: background-color 0.3s ease;
}

.section-header:hover,
.section-header:focus-visible {
    background-color: var(--bg-raised);
}

.section-header:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: -2px;
}

.section-title {
    display: flex;
    align-items: center;
}

.section-icon {
    margin-right: 10px;
    color: var(--accent);
}

.toggle-icon {
    color: var(--accent);
    transition: transform 0.3s ease;
    flex: 0 0 auto;
}

.section-header[aria-expanded="true"] .toggle-icon {
    transform: rotate(180deg);
}

.section-content {
    padding: 0 20px;
    max-height: 0;
    overflow: hidden;
    transition: max-height 0.3s ease-out, padding 0.3s ease;
}

.section-content.expanded {
    max-height: 2000px; /* Large enough to contain content */
    padding: 20px;
    border-top: 1px solid var(--border);
}

.help-section h3 {
    color: var(--text-soft);
    margin: 15px 0 10px 0;
}

.help-section p, .help-section li {
    color: var(--text-soft);
}

.help-section ul {
    padding-left: 20px;
}

.help-section li {
    margin-bottom: 10px;
    line-height: 1.6;
}

.tip-box {
    background-color: var(--bg-raised);
    border-left: 4px solid var(--accent);
    padding: 15px;
    margin: 10px 0;
    border-radius: 0 4px 4px 0;
    color: var(--text-soft);
}

.example {
    background-color: var(--bg-raised);
    padding: 15px;
    border-radius: 4px;
    margin: 10px 0;
    font-family: monospace;
    color: var(--text-soft);
}

/* --------------------------------------------------------------------------
   Footer
   -------------------------------------------------------------------------- */

.footer {
    margin-top: 1px;
    padding: 1px;
}

.footer-wrapper {
    display: flex;
    justify-content: space-between;
    align-items: center;
    color: #8a8a8a;
    font-size: 0.9em;
}

.powered-by {
    text-align: left;
}

.footer a {
    color: #8a8a8a;
    text-decoration: none;
}

.footer a:hover {
    text-decoration: underline;
}

a:link,
a:visited,
a:hover,
a:active {
    text-decoration: none;
}

/* --------------------------------------------------------------------------
   Phones and small tablets
   -------------------------------------------------------------------------- */

@media (max-width: 768px) {
    .files-header,
    .files-list li {
        grid-template-columns: 1fr 90px 140px;
    }
}

@media (max-width: 600px) {
    .container {
        padding: 12px;
    }

    .header {
        flex-wrap: wrap;
        justify-content: center;
        gap: 10px;
        margin-bottom: 12px;
    }

    .logo {
        width: 40vw;
        min-width: 140px;
    }

    .user-info {
        width: 100%;
        justify-content: space-between;
    }

    .nav-icons {
        gap: 4px;
    }

    .chat-container {
        border-radius: 6px;
    }

    .chat-messages {
        padding: 12px;
    }

    .message {
        max-width: 92%;
    }

    .chat-input {
        padding: 10px;
        padding-bottom: calc(10px + env(safe-area-inset-bottom, 0px));
    }

    .send-button {
        padding: 10px 14px;
    }

    .login-container {
        margin: 40px auto;
    }

    /* Files: collapse to a single column, hide the header row */
    .files-header {
        display: none;
    }

    .files-list li {
        grid-template-columns: 1fr;
        gap: 2px;
        padding: 10px 12px;
    }

    .files-list li .item-meta {
        text-align: left;
        font-size: 0.8em;
        color: var(--text-muted);
    }

    .files-list li .item-meta:empty,
    .files-list li span:empty {
        display: none;
    }

    /* Search: stack the input and button */
    .search-container {
        padding: 12px;
    }

    .search-form {
        flex-direction: column;
    }

    .search-button {
        width: 100%;
    }

    .help-content {
        padding: 10px;
    }

    .section-heading {
        font-size: 1.15em;
    }

    .section-header {
        padding: 12px 14px;
    }

    .section-content.expanded {
        padding: 14px;
    }

    .footer-wrapper {
        justify-content: center;
        font-size: 0.8em;
    }
}

/* --------------------------------------------------------------------------
   Inline SVG icons (static/icons.svg sprite; replaces Font Awesome)
   -------------------------------------------------------------------------- */

.svg-icon {
    display: inline-block;
    width: 1em;
    height: 1em;
    fill: currentColor;
    vertical-align: -0.125em;
    flex: 0 0 auto;
}

li > .svg-icon,
h3 > .svg-icon,
.tip-box > .svg-icon,
.search-button > .svg-icon,
.result-item a > .svg-icon {
    margin-right: 0.4em;
}

/* --------------------------------------------------------------------------
   Chat status line (queued / streaming) and streaming bubble
   -------------------------------------------------------------------------- */

.chat-status {
    padding: 4px 16px;
    font-size: 0.85em;
    color: var(--text-soft);
}

.chat-status[hidden] {
    display: none;
}

.assistant-message.streaming .message-content {
    white-space: pre-wrap;
}

.assistant-message.streaming .message-content::after {
    content: "\258B";
    margin-left: 2px;
    opacity: 0.6;
    animation: blink 1s steps(2, start) infinite;
}

@keyframes blink {
    to { visibility: hidden; }
}

/* --------------------------------------------------------------------------
   Directory listing pager
   -------------------------------------------------------------------------- */

.page-count {
    color: var(--text-soft);
    font-size: 0.9em;
    margin-left: 8px;
}

.pager {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 0 0;
    color: var(--text-soft);
}

.pager a {
    color: var(--accent);
    text-decoration: none;
    padding: 10px 12px;
    min-height: 44px;
    display: inline-flex;
    align-items: center;
}

@media (prefers-reduced-motion: reduce) {
    .dot,
    .toggle-icon,
    .section-content,
    .section-header,
    .logout-button,
    .message-input,
    .send-button,
    .feedback-button,
    .loading-indicator {
        animation: none;
        transition: none;
    }
}
