:root {
    font: 13px Arial, sans-serif;
    color: #ebedf0;
    color-scheme: dark;
    background: #111213;
    --metal: url("/assets/gui/panelmetal003.jpg");
    --pane: #24292e;
    --edge: #58626c;
    --blue: #86baf0;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
body { margin: 0; }
body:has(#game:not([hidden])) { display: flex; flex-direction: column; justify-content: center; min-height: 100dvh; }
main { width: 100%; max-width: 1600px; margin-inline: auto; }
main[data-view="compact"] { max-width: 1320px; }
main:has(#game:not([hidden])) { display: flex; flex-direction: column; height: min(100dvh, 1000px); }
main[data-view="compact"]:has(#game:not([hidden])) { height: min(100dvh, 820px); }
p, h1, h2, h3 { margin: 0; }
h1 { font-size: 15px; }
h2 { font-size: 13px; }
h3 { font-size: 19px; }
button, input, select { font: inherit; }
button {
    cursor: pointer;
    color: #f0f3f5;
    border: 1px solid var(--edge);
    background: linear-gradient(#38414a, #252b31);
    padding: 5px 9px;
}
button:hover, button[aria-expanded="true"], button[data-held] { background: #46586a; }
button:focus-visible, input:focus-visible, canvas:focus-visible, summary:focus-visible {
    outline: 2px solid #9accff; outline-offset: -2px;
}
button img, .brand img { width: 18px; height: 18px; image-rendering: pixelated; vertical-align: middle; object-fit: contain; }
.masthead {
    min-height: 35px; flex-shrink: 0; display: flex; align-items: center; gap: 7px; padding: 3px 6px;
    background: #30343a; border-bottom: 1px solid #060607;
}
.brand { display: flex; align-items: center; gap: 6px; margin-right: auto; flex-shrink: 0; }
#status { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 12px; color: #c1cbd5; }
#leave, #open-settings { flex-shrink: 0; }
#open-settings { display: flex; align-items: center; gap: 5px; }
#login, #character-choice {
    display: grid; gap: 12px; width: min(390px, calc(100% - 24px)); margin: 12dvh auto;
    border: 7px solid transparent; border-image: var(--metal) 24 repeat; padding: 16px; background: var(--pane);
}
#login label, #character-choice label { display: grid; gap: 4px; }
#character-choice select { min-height: 36px; border: 1px solid var(--edge); background: #222629; color: inherit; }
input:not([type=checkbox]) { padding: 6px 9px; border: 1px solid #111; color: #15181b; background: #fafafa; }
.phone-panels { display: none; }
#game {
    display: grid; grid-template-columns: 166px minmax(0, 1fr) 256px;
    grid-template-areas: "left map pack" "left chat pack";
    grid-template-rows: minmax(0, 1fr) auto; flex: 1; min-height: 0;
}
.stage { display: contents; }
.left-column { grid-area: left; min-height: 0; overflow-y: auto; }
.left-column, .pack { background: var(--metal) #363636; border: 2px solid #252525; }
.panel { min-width: 0; }
.character, .window-panel, .dialogue {
    background: var(--pane);
    border: 6px solid transparent; border-image: var(--metal) 24 repeat;
}
.character { padding: 4px; }
#minimap { display: block; width: 100%; aspect-ratio: 4 / 3; background: #000; border: 1px solid #151515; }
#character-name { color: #e2e8ee; margin: 6px 0; overflow-wrap: anywhere; }
.character dl div { display: flex; gap: 4px; }
.character dt::after { content: ":"; }
.character dl { margin: 5px 0 0; }
.character dt, .character dd { margin: 0; line-height: 1.4; }
.character dd { font-weight: bold; color: #fff; }
#guide {
    position: absolute; z-index: 2; right: 6px; top: 31px; width: min(295px, calc(100% - 12px));
    max-height: calc(100% - 66px); overflow: auto; padding: 8px; background: #24292ef5;
}
#guide:not([open]) { width: auto; }
#guide:has(#guide-title:empty) { display: none; }
#guide summary { color: var(--blue); cursor: pointer; font-weight: bold; }
#guide h2 { margin-top: 8px; }
#guide p { margin-top: 5px; line-height: 1.4; color: #d7dce2; }
#guide-route { margin-top: 6px; }

.world { grid-area: map; position: relative; display: flex; flex-direction: column; min-height: 0; border: 3px groove #6a6a6a; background: #000; }
.world-header {
    display: flex; justify-content: space-between; align-items: center; min-height: 25px; padding: 2px 8px; gap: 5px;
    background: #26292e; border-bottom: 1px solid #51565e;
}
#position { font-variant-numeric: tabular-nums; color: #aab8c6; white-space: nowrap; }
#stop-attack { margin-left: auto; padding: 2px 6px; }
.map-frame { position: relative; flex: 1; min-height: 0; overflow: hidden; background: #000; }
#grid { display: block; width: 100%; height: 100%; cursor: crosshair; touch-action: manipulation; }
canvas { image-rendering: pixelated; }
#map-status:empty { display: none; }
#map-status { position: absolute; top: 8px; left: 8px; padding: 6px; background: #20252a; pointer-events: none; }
#map-status[data-loading] { top: 50%; left: 50%; transform: translate(-50%, -50%); border: 1px solid #57616a; white-space: nowrap; }
.controls {
    display: none; grid-template-columns: repeat(3, 32px); grid-template-rows: repeat(2, 30px);
    grid-template-areas: ". n ." "w s e"; gap: 2px; opacity: .8;
}
.movement-controls { position: absolute; display: flex; align-items: flex-end; gap: 5px; right: 5px; bottom: 5px; }
.controls button { padding: 0; font-size: 19px; touch-action: none; user-select: none; }
.controls [data-direction="1"] { grid-area: n; }
.controls [data-direction="2"] { grid-area: e; }
.controls [data-direction="3"] { grid-area: s; }
.controls [data-direction="4"] { grid-area: w; }
.pack { grid-area: pack; min-height: 0; overflow-y: auto; padding: 3px; --slot-size: 40px; }
.inventory-window { width: max-content; max-width: 100%; margin-bottom: 4px; }
.inventory-window h2 { font-size: 12px; padding: 2px 3px; background: #222; overflow: hidden; text-overflow: ellipsis; }
.character-windows { display: flex; align-items: flex-start; gap: 2px; }
.gear { display: flex; gap: 1px; }
.gear-column { display: grid; gap: 1px; }
.gear-column:nth-child(odd) { margin-top: 30px; }
.bag, .keyring, .extra-slots { display: grid; grid-template-columns: repeat(6, var(--slot-size)); gap: 1px; }
.keyring { grid-template-columns: repeat(3, var(--slot-size)); }
.extra-slots { grid-template-columns: repeat(2, var(--slot-size)); justify-content: center; margin-top: 1px; }
.slot { position: relative; width: var(--slot-size); height: var(--slot-size); padding: 0; border: 0; display: grid; place-items: center; background: #bdc0c5 url("/assets/gui/slot.png") center / 100% 100% no-repeat; touch-action: manipulation; }
.slot:hover, .slot[aria-pressed="true"], .slot[data-drop] { background: #bdc0c5 url("/assets/gui/slot.png") center / 100% 100% no-repeat; box-shadow: inset 0 0 0 2px #5694c3; }
.slot img { width: 32px; height: 32px; object-fit: contain; image-rendering: pixelated; pointer-events: none; }
.slot img[data-sheet] { width: 32px; height: 32px; object-fit: none; object-position: 0 0; }
.slot span { pointer-events: none; position: absolute; top: 1px; right: 2px; font-size: 10px; font-weight: bold; color: #fff; text-shadow: 1px 1px #000, -1px -1px #000, 1px -1px #000, -1px 1px #000; }
#item-actions { position: sticky; bottom: 0; background: #24292e; border: 1px solid var(--edge); padding: 4px; }
#item-status { margin-bottom: 4px; overflow-wrap: anywhere; }

#chat-panel { grid-area: chat; background: var(--metal); padding: 3px 4px; }
#send { display: flex; gap: 3px; }
#chat-text { flex: 1; min-width: 0; }
.chat-log { list-style: none; padding: 3px 6px; margin: 3px 0 0; height: 76px; overflow-y: auto; overflow-wrap: anywhere; background: #2e2e2e; border: 1px solid #151515; }
#chat-private { background: #3c1e00; }
.chat-log li { line-height: 1.45; font-weight: bold; color: #c8f0f0; }
.chat-log li[data-style="regular"] { font-weight: normal; }
.chat-log time { color: #dcdcdc; font-size: 11px; font-style: italic; font-weight: normal; }
.chat-log strong { color: #ffffdc; font-weight: normal; font-style: italic; }
.chat-log .chat-reference { color: #5aaaff; font-style: italic; font-weight: bold; }
.chat-tabs { display: flex; gap: 2px; }
.chat-tabs button { font-size: 12px; padding: 2px 6px; background: #2a2a2a; }
.chat-tabs button[aria-pressed="true"] { border-top-color: transparent; background: #484848; }
.chat-tabs button[data-unread]::before { content: "* "; }
button:disabled { opacity: .58; cursor: default; }

.dialogue {
    position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%);
    width: min(670px, calc(100% - 18px)); max-height: calc(100% - 46px); overflow: auto; z-index: 4;
    padding: 0 9px 9px; box-shadow: 0 3px 12px #0008;
}
.dialogue header, .window-panel header {
    display: flex; justify-content: space-between; align-items: center; gap: 8px;
    padding: 4px 6px; margin: 0 -9px 8px; background: #323c45; border-bottom: 1px solid var(--edge);
}
.dialogue p { margin: 6px 0; line-height: 1.35; white-space: pre-line; }
.dialogue p:empty { display: none; }
.npc-heading { display: flex; align-items: flex-start; gap: 12px; border-bottom: 1px solid var(--edge); padding-bottom: 6px; }
#npc-portrait { flex: 0 0 64px; background: #191d20; border: 1px solid var(--edge); }
#npc-player, #npc-narration { color: #a8bed2; font-size: 12px; }
#npc-description, #npc-feedback { color: #a8bed2; }
#npc-speech { margin: 8px 0 12px; font-size: 14px; }
#npc-answer-label { margin: 8px 0 5px; font-size: 11px; text-transform: uppercase; }
#npc-offer h4, #npc-reward h4 { margin: 7px 0; font-size: 11px; text-transform: uppercase; }
#npc-offer-items { max-height: min(190px, 30dvh); overflow-y: auto; scrollbar-color: #798692 #252b30; }
.npc-item { display: flex; align-items: center; gap: 7px; padding: 5px; border-bottom: 1px solid var(--edge); }
.npc-item strong, .npc-item small { display: block; }
.npc-item small { margin-top: 2px; color: #b6c8d6; }
.npc-item img { width: 32px; height: 32px; object-fit: contain; image-rendering: pixelated; flex: 0 0 32px; }
.npc-item img[data-sheet] { object-fit: none; object-position: 0 0; }
.npc-item input[type=number] { width: 60px; margin-left: auto; color: #fff; background: #222629; border-color: var(--edge); }
#npc-answer-toggle { margin-top: 5px; }
#npc-money { color: #ffce71; }
.topic-button { display: inline; border: 0; padding: 0; background: none; color: var(--blue); text-decoration: underline; }
#npc-replies { display: grid; gap: 4px; }
#npc-replies button { text-align: left; min-height: 34px; }
#npc-history { margin: 9px 0; }
#npc-history summary { cursor: pointer; color: var(--blue); }
#npc-history-lines { max-height: 100px; overflow-y: auto; margin: 3px 0; padding-left: 18px; }
#npc-text label { display: block; color: #a8bed2; font-size: 12px; margin-bottom: 4px; }
#npc-text > div { display: flex; gap: 4px; }
#npc-text input { flex: 1; min-width: 0; color: #fff; background: #1d2226; border: 1px solid var(--edge); }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; }
#settings { width: min(390px, calc(100% - 18px)); max-height: calc(100dvh - 18px); padding: 0; border: 0; color: inherit; background: transparent; }
#settings::backdrop { background: #080a0cb0; }
.settings-window { display: grid; gap: 8px; padding: 0 12px 12px; background: var(--pane); border: 7px solid transparent; border-image: var(--metal) 24 repeat; }
.settings-window header { display: flex; align-items: center; justify-content: space-between; margin-inline: -12px; padding: 5px 8px; background: #384149; }
.settings-window select { min-width: 0; min-height: 36px; padding-inline: 8px; color: inherit; background: #222629; border: 1px solid var(--edge); }
.settings-check { display: flex; align-items: center; gap: 6px; min-height: 38px; }
#journal { width: min(940px, calc(100% - 24px)); max-height: calc(100dvh - 26px); padding: 0; border: 0; color: inherit; background: transparent; }
#journal::backdrop { background: #080a0cb0; }
.journal-window { background: #252b31; border: 7px solid transparent; border-image: var(--metal) 24 repeat; }
.journal-window > header { display: flex; justify-content: space-between; align-items: center; padding: 5px 8px; background: #384149; }
#journal-tabs { display: flex; flex-wrap: wrap; gap: 4px; padding: 7px; background: #2a3036; }
.journal-content { display: grid; grid-template-columns: minmax(200px, 30%) minmax(0, 1fr); min-height: min(52dvh, 430px); max-height: 67dvh; }
#journal-index { min-height: 0; display: flex; flex-direction: column; border-right: 1px solid var(--edge); padding: 7px; }
#journal-filters, #journal-index footer { display: flex; flex-wrap: wrap; gap: 4px; }
#journal-index footer { padding-top: 6px; justify-content: space-between; }
#journal-tabs button[aria-pressed="true"], #journal-filters button[aria-pressed="true"], .journal-row[aria-pressed="true"] { background: #3b5062; border-color: #7aa4c9; }
#journal-search-box { display: grid; gap: 4px; padding: 5px 0; }
#journal-search { min-width: 0; }
#journal-list { min-height: 0; flex: 1; display: grid; grid-auto-rows: max-content; gap: 4px; padding-top: 6px; overflow-y: auto; }
.journal-row { display: flex; align-items: center; gap: 7px; text-align: left; min-height: 36px; }
.journal-row strong, .journal-row small { display: block; overflow-wrap: anywhere; }
.journal-row img, .journal-objectives img { width: 25px; height: 25px; flex: 0 0 25px; object-fit: contain; image-rendering: pixelated; }
#journal-detail { padding: 12px; min-height: 0; overflow-y: auto; overflow-wrap: anywhere; }
#journal-detail p { margin: 8px 0; line-height: 1.4; white-space: pre-line; }
#journal-detail ol { padding-left: 20px; }
#journal-detail li { margin-bottom: 4px; }
.journal-objectives { display: grid; grid-template-columns: repeat(auto-fit, minmax(125px, 1fr)); gap: 5px; list-style: none; padding: 0; }
.journal-objectives li { display: flex; gap: 7px; align-items: center; padding: 6px; background: #303943; border: 1px solid var(--edge); }
.journal-objectives small { display: block; }
.journal-next, #journal-detail summary { color: var(--blue); }
.journal-warning { color: #ffd070; }
.journal-status, .journal-row small { color: #b6c8d6; }
#journal-detail > button { margin-top: 10px; }

@media (max-width: 760px), (pointer: coarse) and (max-width: 1000px) {
    main:has(#game:not([hidden])), main[data-view="compact"]:has(#game:not([hidden])) { height: 100dvh; overflow: hidden; }
    #game { display: flex; flex-direction: column; }
    .masthead { min-height: 40px; }
    .phone-panels { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 3px; padding: 3px; background: var(--metal); }
    .phone-panels button { min-height: 42px; }
    .stage { display: block; position: relative; flex: 1; min-height: 0; height: auto; overflow: hidden; }
    .world { height: 100%; }
    .left-column { display: contents; }
    .stage #character, .stage #guide, .stage #pack { display: none; position: absolute; z-index: 3; overflow: auto; max-height: calc(100% - 34px); top: 31px; }
    .stage #character[data-open], .stage #guide[data-open], .stage #pack[data-open] { display: block; }
    #character, #guide { left: 7px; right: auto; width: min(255px, calc(100% - 14px)); }
    #pack { right: 7px; width: min(260px, calc(100% - 14px)); }
    #pack .character-windows { flex-wrap: wrap; }
    #item-actions button { min-height: 34px; }
    .controls { display: grid; grid-template-columns: repeat(3, 46px); grid-template-rows: repeat(2, 44px); opacity: .93; }
    .movement-controls { right: auto; left: 11px; bottom: 10px; }
    #chat-text { min-height: 40px; font-size: 16px; }
    #guide-button, #conversation-button, .journal-desktop { display: none; }
    .chat-tabs button { min-height: 26px; }
    #npc-replies button { min-height: 42px; }
    #login input, #npc-text input, .npc-item input[type=number], .settings-window select, #character-choice select { font-size: 16px; }
    #close-dialogue, #settings header button, #journal header button { min-width: 39px; min-height: 39px; }
    #open-settings { min-height: 34px; min-width: 36px; justify-content: center; }
    #open-settings span { display: none; }
}
@media (max-width: 700px) {
    #journal { width: calc(100% - 12px); }
    .journal-content { display: block; max-height: 75dvh; overflow-y: auto; }
    #journal-index { border-right: 0; border-bottom: 1px solid var(--edge); }
    #journal-list { max-height: 24dvh; }
    #journal-tabs button, #journal-filters button { min-height: 36px; }
    .phone-panels button { padding: 4px; font-size: 11px; }
}

@media (max-height: 480px) and (pointer: coarse) {
    .phone-panels button { min-height: 32px; }
    .chat-log { height: 46px; }
    .movement-controls { bottom: 5px; }
    .controls { grid-template-columns: repeat(3, 42px); grid-template-rows: repeat(2, 39px); }
}
