/* Query Runner -- master/detail layout (Story #19).
   Phone-friendly: the rail collapses above the editor on narrow screens.
   Wide query results open in a dismissable overlay (the Story Detail pattern)
   so the editor never has to fight the table for horizontal space. */

body { background: #F2F2F4; margin: 0; }

.qr-shell { display: flex; flex-direction: column; min-height: 100vh; min-height: 100dvh; }

.qr-header {
    background: #3C3C3C; color: #fff;
    padding: 10px 16px;
    display: flex; align-items: center; gap: 12px;
    flex: 0 0 auto;
}
.qr-header h1 { margin: 0; font-size: 14pt; font-weight: 600; flex: 1; }
.qr-header a {
    background: transparent; color: #fff;
    border: 1px solid #6A6A6A; border-radius: 3px;
    padding: 4px 12px; text-decoration: none; font: inherit;
}
.qr-header a:hover { background: #5A5A5A; }
.qr-readonly-badge {
    background: #1A7A3C; color: #fff;
    border-radius: 3px; padding: 3px 8px;
    font-size: 9pt; font-weight: 700; letter-spacing: 0.5px;
}

.qr-body {
    flex: 1;
    display: grid;
    grid-template-columns: 320px minmax(0, 1fr); /* minmax(0, 1fr) keeps the editor pane shrinkable */
    gap: 12px;
    padding: 12px;
    padding-bottom: calc(40px + env(safe-area-inset-bottom, 0px));
    min-height: 0;
}
@media (max-width: 800px) {
    .qr-body { grid-template-columns: minmax(0, 1fr); }
}

.qr-rail {
    display: flex; flex-direction: column; gap: 12px;
    min-height: 0; min-width: 0;
}
.qr-rail-section {
    background: #fff; border: 1px solid #E0E0E2; border-radius: 4px;
    padding: 10px 12px;
    display: flex; flex-direction: column;
    min-height: 0; min-width: 0;
}
.qr-rail-head {
    display: flex; align-items: center; gap: 8px;
    margin-bottom: 6px;
}
.qr-rail-head h2 {
    margin: 0; font-size: 11pt; font-weight: 600; color: #1A1A1A; flex: 1;
}
.qr-rail-foot { margin-top: 8px; }

.qr-saved-tree, .qr-history-list {
    flex: 1; overflow-y: auto; min-height: 80px; max-height: 40vh;
    border-top: 1px solid #F2F2F4;
    padding-top: 6px;
}
.qr-cat-group {
    margin-top: 6px;
    font-size: 10pt; color: #777; font-weight: 600;
    text-transform: uppercase; letter-spacing: 0.5px;
}
.qr-cat-group:first-child { margin-top: 0; }
.qr-saved-row {
    display: flex; align-items: center; gap: 6px;
    padding: 7px 6px; border-radius: 3px;
    font-size: 11pt; color: #1A1A1A;
    cursor: pointer;
}
.qr-saved-row:hover { background: #F2F2F4; }
.qr-saved-row.active { background: #DDEBF7; }
.qr-saved-row .title  { flex: 1; min-width: 0; word-break: break-word; }
.qr-saved-row .del    { color: #C00000; font-size: 14pt; line-height: 1; padding: 2px 6px; cursor: pointer; }
.qr-saved-row .del:hover { background: #FFEAEA; border-radius: 3px; }

.qr-history-row {
    display: block; padding: 6px;
    font-family: Consolas, "Courier New", monospace; font-size: 10pt;
    color: #444;
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
    cursor: pointer;
    border-radius: 3px;
}
.qr-history-row:hover { background: #F2F2F4; color: #000; }
.qr-history-row .when { color: #888; font-size: 9pt; margin-right: 6px; }

.qr-empty {
    color: #888; font-style: italic; font-size: 10pt;
    padding: 8px 6px;
}

.qr-main {
    display: flex; flex-direction: column; gap: 12px; min-height: 0; min-width: 0;
}

.qr-editor-pane {
    background: #fff; border: 1px solid #E0E0E2; border-radius: 4px;
    padding: 10px 12px;
    display: flex; flex-direction: column; gap: 8px;
    min-width: 0;
}
.qr-editor-head {
    display: flex; gap: 8px; align-items: center; flex-wrap: wrap;
}
.qr-title-input {
    flex: 1; min-width: 0;
    border: 1px solid #E0E0E2; border-radius: 3px;
    padding: 8px 10px; font: 11pt "Segoe UI", system-ui, sans-serif;
    color: #555; background: #FAFAFB;
}
.qr-editor-actions { display: flex; gap: 6px; }
.qr-sql {
    width: 100%; max-width: 100%;
    min-height: 220px; max-height: 60vh; resize: vertical;
    box-sizing: border-box;
    font-family: Consolas, "Courier New", monospace; font-size: 12pt;
    border: 1px solid #C9C9CC; border-radius: 3px;
    padding: 10px 12px;
    /* word-wrap so long lines fold instead of forcing horizontal scroll on phone */
    white-space: pre-wrap; word-break: break-word;
    overflow-x: hidden; overflow-y: auto;
}
.qr-editor-hint { color: #777; font-size: 10pt; }

.qr-btn {
    padding: 8px 14px;
    background: #F5F5F7; color: #555;
    border: 1px solid #C9C9CC; border-radius: 3px;
    font: 11pt "Segoe UI", system-ui, sans-serif;
    cursor: pointer;
}
.qr-btn.small { padding: 5px 10px; font-size: 10pt; }
.qr-btn.big   { padding: 11px 22px; font-size: 12pt; font-weight: 600; }
.qr-btn:hover { background: #ECECEE; color: #333; }
.qr-btn:disabled { color: #888; cursor: default; }
.qr-btn.primary {
    background: #1A7A3C; color: #fff; border-color: #1A7A3C;
}
.qr-btn.primary:hover { background: #236B40; }
.qr-btn.primary:disabled { background: #A0C4A8; border-color: #A0C4A8; }
.qr-btn.danger {
    background: #fff; color: #C00000; border-color: #C00000;
}
.qr-btn.danger:hover { background: #FFF0F0; }

.qr-statusbar {
    background: #2A2A2A; color: #BBB;
    padding: 4px 12px;
    padding-bottom: calc(4px + env(safe-area-inset-bottom, 0px));
    font-size: 11px;
    flex: 0 0 auto;
}

/* ---- Modal pattern -- mirrors stories.html / stories.js Detail modal ---- */
.qr-modal-backdrop {
    position: fixed; inset: 0;
    background: rgba(0,0,0,0.5);
    display: flex; align-items: flex-end;
    z-index: 200;
}
.qr-modal-sheet {
    background: #fff; border-radius: 12px 12px 0 0;
    width: 100%; max-height: 92vh; overflow-y: auto;
    padding: 18px 20px 28px; box-sizing: border-box;
    display: flex; flex-direction: column; gap: 12px;
}
@media (min-width: 760px) {
    .qr-modal-backdrop { align-items: center; justify-content: center; }
    .qr-modal-sheet { max-width: 560px; border-radius: 12px; max-height: 88vh; }
    .qr-modal-sheet.qr-results-sheet    { max-width: 1100px; }
    .qr-modal-sheet.qr-categories-sheet { max-width: 640px; }
}
.qr-modal-headerbar {
    display: flex; align-items: center; gap: 10px;
}
.qr-modal-title {
    margin: 0; font-size: 15pt; font-weight: 600; color: #1A1A1A; flex: 1;
}
.qr-modal-close {
    background: #F5F5F7; color: #555;
    border: 1px solid #C9C9CC; border-radius: 4px;
    font-size: 18pt; line-height: 1; padding: 0 12px; height: 36px;
    cursor: pointer;
}
.qr-modal-close:hover { background: #ECECEE; color: #C00000; border-color: #C00000; }
.qr-modal-subtitle {
    font-size: 11pt; color: #555;
    display: flex; justify-content: space-between; gap: 12px; flex-wrap: wrap;
}
.qr-modal-subtitle .err { color: #C00000; }
.qr-modal-subtitle .ok  { color: #155724; }

.qr-modal-field {
    display: flex; flex-direction: column; gap: 5px;
    font-size: 11pt; color: #1A1A1A;
}
.qr-modal-field label { font-weight: 600; color: #333; }
.qr-modal-field input, .qr-modal-field select {
    padding: 10px 12px; font: 12pt "Segoe UI", system-ui, sans-serif;
    border: 1px solid #C9C9CC; border-radius: 4px;
    box-sizing: border-box;
}
.qr-modal-actions { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 4px; }
.qr-modal-actions .qr-btn { flex: 1; min-width: 120px; }
.qr-modal-msg { font-size: 11pt; min-height: 16px; }
.qr-modal-msg.err { color: #C00000; }
.qr-modal-msg.ok  { color: #155724; }

/* ---- Results overlay ---- */
.qr-results-scroll {
    overflow: auto;
    border: 1px solid #E0E0E2; border-radius: 3px;
    flex: 1; min-height: 200px;
}
.qr-results-table {
    border-collapse: collapse; font-size: 10pt; width: max-content;
    min-width: 100%;
}
.qr-results-table th, .qr-results-table td {
    border-right: 1px solid #F2F2F4; border-bottom: 1px solid #F2F2F4;
    padding: 5px 10px; text-align: left; vertical-align: top;
    white-space: nowrap;
}
.qr-results-table th {
    background: #F5F5F7; position: sticky; top: 0; z-index: 1;
    font-weight: 600; color: #1A1A1A;
}
.qr-results-table tbody tr:nth-child(2n) { background: #FAFAFB; }
.qr-results-table .nullcell { color: #999; font-style: italic; }

/* ---- Manage Categories modal -- card per category, action row wraps ---- */
.qr-cat-list {
    display: flex; flex-direction: column; gap: 10px;
}
.qr-cat-card {
    background: #FAFAFB; border: 1px solid #E0E0E2; border-radius: 6px;
    padding: 12px;
    display: grid;
    grid-template-columns: minmax(0, 1fr) 100px;
    gap: 10px;
    align-items: end;
}
.qr-cat-card .qr-cat-actions {
    grid-column: 1 / -1;
    display: flex; gap: 10px; justify-content: flex-end; flex-wrap: wrap;
}
.qr-cat-newcard {
    background: #F0F6FF; border: 1px solid #C8DCF5; border-radius: 6px;
    padding: 14px 12px;
    display: grid;
    grid-template-columns: minmax(0, 1fr) 100px;
    gap: 10px;
    align-items: end;
}
.qr-cat-newcard h3 {
    grid-column: 1 / -1;
    margin: 0 0 4px 0; font-size: 12pt; font-weight: 600; color: #1A1A1A;
}
.qr-cat-newcard .qr-cat-newcard-actions {
    grid-column: 1 / -1;
    display: flex; gap: 10px; justify-content: flex-end;
}
.qr-cat-field {
    display: flex; flex-direction: column; gap: 4px;
    font-size: 10pt; color: #555;
    min-width: 0;
}
.qr-cat-field label { font-weight: 600; color: #333; }
.qr-cat-field input {
    padding: 9px 11px; font: 12pt "Segoe UI", system-ui, sans-serif;
    border: 1px solid #C9C9CC; border-radius: 4px;
    box-sizing: border-box; width: 100%;
}
@media (max-width: 520px) {
    .qr-cat-card, .qr-cat-newcard {
        grid-template-columns: minmax(0, 1fr);
    }
}

/* ---- Story 5757: the rail's sections and categories fold, with Fold All in the header ---- */
.qr-rail-head[data-qr-head] { cursor: pointer; user-select: none; }
.qr-rail-body { display: flex; flex-direction: column; min-height: 0; flex: 1; }
.qr-rail-section.collapsed .qr-rail-body { display: none; }
.qr-rail-section.collapsed .qr-rail-head { margin-bottom: 0; }
.qr-chev { display: inline-block; width: 12px; color: #64748B; font-size: 11px; text-align: center; flex: 0 0 auto; }
.qr-chev::before { content: "\25BE"; }
.qr-rail-section.collapsed .qr-rail-head .qr-chev::before,
.qr-cat-band.collapsed .qr-cat-group .qr-chev::before { content: "\25B8"; }
.qr-cat-group[data-cat-head] { cursor: pointer; user-select: none; display: flex; align-items: center; gap: 6px; padding: 3px 0; }
.qr-cat-band:first-child .qr-cat-group { margin-top: 0; }
.qr-cat-n { font-weight: 600; color: #999; text-transform: none; letter-spacing: 0; }
.qr-cat-band.collapsed .qr-cat-body { display: none; }
.qr-header .qr-foldall {
    background: transparent; color: #fff;
    border: 1px solid #6A6A6A; border-radius: 3px;
    padding: 4px 10px; font: inherit; cursor: pointer;
}
.qr-header .qr-foldall:hover { background: #5A5A5A; }
.qr-foldall[hidden] { display: none; }
