.modal-wrapper {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  width: 100%;
  position: absolute;
}

/* Hide login buttons when a login-screen modal is open on mobile */
@media (max-width: 768px), (max-height: 500px) {
  #login-wrapper .modal-wrapper.has-modal ~ .login-settings {
    visibility: hidden;
  }
}

.modal {
  display: none;
  position: absolute;
  z-index: 30000;
  border: 1px solid black;
  box-shadow: 0px 0px 10px 0px black;
  background-image: url("/png/bg.png");
}

.modal-header {
  background-image: url("/png/bg2.png");
  cursor: grab;
  text-align: center;
  padding: 4px;
}

.modal .modal-footer {
  text-align: right;
}

.modal-body {
  box-shadow: inset 0 0 6px black;
  padding: 12px;
}

/* Chat Options — channel select list */
.channel-select-list {
  background-color: #2a2018;
  background-image: url("/png/bg3.png");
  border: 1px solid black;
  cursor: pointer;
}

.channel-select-item {
  padding: 4px 8px;
  color: white;
  font-weight: bold;
  background-color: #2a2018;
  background-image: url("/png/bg3.png");
}

.channel-select-item:nth-child(even) {
  background-color: #1e1a12;
  background-image: url("/png/bg2.png");
}

.channel-select-item:hover {
  color: grey;
}

.channel-select-item.selected {
  background-color: #4a3a20;
  color: #ffcc00;
}

/* Force readable text in dropdowns on Windows (OS renders select with black text on dark bg) */
.modal select,
#settings-modal select,
#fps-limit,
#display-mode,
#mouse-control-mode,
#clock-format,
#aspect-ratio-mode,
#hotbar-preset,
#movement-style {
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
  color: #fff !important;
  background-color: #2a2018 !important;
  border: 1px solid #555;
  padding: 3px 6px;
  font-family: inherit;
  font-size: inherit;
  cursor: pointer;
}



.tab-flex {
  display: flex;
}

.offer-tab {
  flex: 1 1 auto;
  padding: 6px;
  background-image: url("/png/bg2.png");
  color: #999;
  border: 1px solid #333;
  cursor: pointer;
  font-weight: normal;
}

.offers {
  display: flex;
  flex-wrap: wrap;
  padding: 8px;
  justify-content: flex-start;
  align-items: flex-start;
  background: #1a1a1a;
  max-height: 150px;
  overflow-y: scroll;
  overflow-x: hidden;
  scrollbar-width: thin;
  scrollbar-color: rgba(255, 215, 0, 0.4) rgba(0, 0, 0, 0.3);
}

.offers::-webkit-scrollbar {
  width: 6px;
}

.offers::-webkit-scrollbar-thumb {
  background: rgba(255, 215, 0, 0.4);
  border-radius: 3px;
}

.offers::-webkit-scrollbar-track {
  background: rgba(0, 0, 0, 0.3);
}

.offers canvas {
  margin: 2px;
  cursor: pointer;
  border: 1px solid transparent;
  background-image: url("/png/bg3.png");
  background-size: cover;
}

.offers canvas:hover {
  border: 1px solid gold;
}

.offer-tab.selected {
  border-bottom: 0px;
  background-image: url("/png/bg3.png");
  color: #fff;
  font-weight: bold;
  border: 1px solid gold;
  border-bottom: 0px;
}

.modal input {
  color: white;
  border: 1px solid black;
}

/* Active focus styling for enter-name input on mobile (virtual keyboard open) */
#enter-name.enter-name-active {
  border: 2px solid #ddb944;
  outline: none;
  box-shadow: 0 0 6px rgba(221, 185, 68, 0.5);
  background-color: rgba(0, 0, 0, 0.6);
}

/* Character Selection Modal */
.character-select-list {
  display: flex;
  flex-direction: column;
  background-image: url("/png/bg3.png");
  max-height: 280px;
  overflow-y: auto;
  scrollbar-width: thin;
}

.character-select-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 10px;
  cursor: pointer;
  border-bottom: 1px solid rgba(0, 0, 0, 0.3);
  font-size: 12px;
}

.character-select-row:last-child {
  border-bottom: none;
}

.character-select-row:hover {
  background-color: rgba(255, 255, 255, 0.1);
}

.character-select-row.selected {
  background-color: rgba(255, 204, 0, 0.2);
  border: 1px solid #ffcc00;
}

.character-select-name {
  font-weight: bold;
}

.character-select-info {
  font-size: 11px;
  opacity: 0.8;
}

.character-create-form {
  padding: 6px;
}

.character-create-form input,
.character-create-form select {
  width: 100%;
  box-sizing: border-box;
}

/* Enter Name modal — fixed center positioning for mobile with virtual keyboard */
@media (max-width: 768px), (max-height: 500px) {
  #enter-name-modal {
    width: 240px;
    max-width: 80vw;
    z-index: 99999;
  }

  #enter-name-modal .modal-body {
    padding: 8px;
  }

  #enter-name-modal .modal-header {
    padding: 4px;
    font-size: 12px;
  }

  #enter-name-modal #enter-name {
    width: 100%;
    box-sizing: border-box;
    font-size: 14px;
    padding: 6px;
  }

  #enter-name-modal .modal-footer button {
    padding: 6px 12px;
    font-size: 12px;
  }
}

#offer-modal {
  width: 360px;
  max-width: 90vw;
}

/* Trade Select Modal (mobile player selection) */
#trade-select-modal {
  width: 280px;
  max-width: 90vw;
}

.trade-select-players {
  display: flex;
  flex-direction: column;
  background-image: url("/png/bg3.png");
  max-height: 200px;
  overflow-y: auto;
  scrollbar-width: thin;
}

.trade-select-player {
  display: flex;
  align-items: center;
  padding: 10px;
  cursor: pointer;
  border-bottom: 1px solid rgba(0, 0, 0, 0.3);
  font-size: 12px;
}

.trade-select-player:last-child {
  border-bottom: none;
}

.trade-select-player:hover {
  background-color: rgba(255, 255, 255, 0.1);
}

/* Travel Destination Modal */
#travel-modal {
  width: 300px;
  max-width: 90vw;
}

.travel-destinations {
  display: flex;
  flex-direction: column;
  background-image: url("/png/bg3.png");
  max-height: 200px;
  overflow-y: auto;
  scrollbar-width: thin;
}

.travel-destination {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 10px;
  cursor: pointer;
  border-bottom: 1px solid rgba(0, 0, 0, 0.3);
}

.travel-destination:last-child {
  border-bottom: none;
}

.travel-destination:hover {
  background-color: rgba(255, 255, 255, 0.1);
}

.travel-destination.selected {
  background-color: rgba(255, 215, 0, 0.2);
  border-color: gold;
}

.travel-dest-name {
  flex: 1;
  font-size: 12px;
}

.travel-dest-price {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 12px;
  color: gold;
}

.travel-dest-price img {
  width: 12px;
  height: 12px;
}

/* Bottom fade indicator when travel list is scrollable */
.travel-destinations.has-overflow {
  position: relative;
  border-bottom: 2px solid rgba(255, 215, 0, 0.3);
}

#offer-price {
  width: 64px;
  display: inline-block;
}

#offer-name-wrapper {
  flex: 1 0 auto;
  text-align: right;
}

#buy-count-wrapper {
  display: flex;
}

#buy-count {
  width: 100px;
  margin-left: 8px;
  margin-right: 8px;
}

#offer-count {
  width: 32px;
  text-align: right;
}

#offer-info {
  min-height: 40px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  border: 1px solid black;
  padding: 4px;
  background-image: url("/png/bg2.png");
}

#offer-gold-wrapper {
  text-align: right;
  font-size: 11px;
}

#offer-gold-wrapper img {
  vertical-align: middle;
  height: 12px;
}

#book-text-area {
  height: 220px;
  resize: none;
  scrollbar-width: thin;
  background-image: url("/png/bg2.png");
  color: white;
  font-weight: bold;
  padding: 16px;
  padding-right: 8px;
  font-size: 12px;
  font-family: Verdana;
  box-shadow: inset 0 0 8px black;
  border: 1px solid black;
  border-radius: 16px 0px 0px 16px;
}

#book-text-area:disabled {
  color: grey;
}

#book-text-area:focus {
  outline: none;
}

.map-modal-wrapper {
  position: relative;
}

.map-modal-wrapper span {
  position: absolute;
  left: 4px;
  top: 4px;
}

.map-modal-wrapper .vline {
  pointer-events: none;
  border-bottom: 1px solid white;
  width: 256px;
  top: 128px;
  position: absolute;
}

.map-modal-wrapper .hline {
  pointer-events: none;
  border-right: 1px solid white;
  left: 128px;
  position: absolute;
  height: 256px;
}

#spellbook-list {
  max-height: 250px;
  overflow-y: auto;
  overflow-x: hidden;
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
  background-image: url("/png/bg.png");
  width: 400px;
}

.spellbook-wrapper {
  width: 45%;
  border: 1px solid black;
  flex: 1 1 auto;
  display: flex;
  margin: 4px;
  align-items: center;
  background-image: url("/png/bg2.png");
}

.spellbook-wrapper canvas {
  flex: 0 1 auto;
  width: 32px;
  height: 32px;
  box-shadow: 0px 0px 4px 0px black;
  cursor: pointer;
}

.spellbook-wrapper span {
  flex: 0 1 auto;
  text-align: center;
  width: 100%;
}

#spellbook-wrapper-prototype {
  display: none;
}

.trade-section {
  display: flex;
  flex-direction: column;
  padding: 4px;
  width: 100%;
}

.trade-label {
  font-weight: bold;
  font-size: 11px;
  margin-bottom: 2px;
  color: lightgrey;
}

.trade-slot {
  display: flex;
  align-items: center;
  gap: 6px;
}

.trade-slot canvas {
  flex: 0 0 32px;
  border: 1px solid #444;
  background-image: url("/png/bg3.png");
}

.trade-slot span {
  font-size: 11px;
  color: grey;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.trade-buttons {
  display: flex;
  gap: 4px;
  width: 100%;
  padding: 4px;
  justify-content: flex-end;
}

.trade-buttons button {
  font-size: 11px;
  padding: 2px 8px;
}

#trade-window .body {
  display: flex;
  flex-direction: column;
  padding: 4px;
}

.trade-contents {
  display: flex;
  flex-wrap: wrap;
  gap: 2px;
  margin-top: 4px;
}

.trade-content-slot {
  border: 1px solid #444;
  background-image: url("/png/bg3.png");
}

/* Modal Backdrop — covers screen behind modals, tap to close */
#modal-backdrop {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  z-index: 29999;
}

/* Hotbar Add Modal */
#hotbar-add-modal {
  width: 420px;
  max-width: 95vw;
}

.hotbar-add-tabs {
  display: flex;
  gap: 2px;
  margin-bottom: 8px;
}

.hotbar-add-tab {
  flex: 1;
  padding: 6px;
  text-align: center;
  cursor: pointer;
  border: 1px solid #333;
  background-image: url("/png/bg2.png");
  color: #aaa;
}

.hotbar-add-tab.active {
  color: white;
  border-bottom-color: transparent;
  background-image: url("/png/bg.png");
}

.hotbar-item-grid {
  max-height: 200px;
  overflow-y: auto;
  scrollbar-width: thin;
}

.hotbar-item-entry {
  display: flex;
  align-items: center;
  padding: 4px;
  cursor: pointer;
  border-bottom: 1px solid #222;
}

.hotbar-item-entry:hover {
  background-color: rgba(255, 255, 255, 0.1);
}

.hotbar-item-entry canvas {
  flex: 0 0 32px;
  width: 32px;
  height: 32px;
  box-shadow: 0px 0px 4px 0px black;
}

.hotbar-item-entry .item-label {
  margin-left: 8px;
  font-size: 11px;
}

.hotbar-mode-btn {
  display: block;
  width: 100%;
  padding: 8px;
  margin-bottom: 4px;
  cursor: pointer;
  background: #333;
  color: #fff;
  border: 1px solid #555;
  font-size: 12px;
  font-family: Martel, serif;
}

.hotbar-mode-btn:hover {
  background: #444;
  border-color: #888;
}

#hotbar-spell-list {
  max-height: 200px;
  overflow-y: auto;
  overflow-x: hidden;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  background-image: url("/png/bg.png");
  width: 100%;
}

#hotbar-add-text-input {
  width: 100%;
  margin-top: 8px;
  padding: 6px;
}

#settings-logout-button {
  background: #6b2020;
  color: #ff9999;
  border: 1px solid #993333;
}

#settings-logout-button:hover {
  background: #832a2a;
}

/* Settings modal — desktop layout */
#settings-modal .settings-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 6px 0;
}

#settings-modal .settings-row label {
  white-space: nowrap;
  min-width: 110px;
}

#settings-modal .settings-row select {
  max-width: 160px;
}

#settings-modal .settings-row button {
  max-width: 200px;
}

@media (max-width: 768px), (max-height: 500px) {
  /* NPC Trade Offer Modal - mobile positioning and touch fixes */
  #offer-modal {
    position: fixed !important;
    top: 50% !important;
    left: 50% !important;
    transform: translate(-50%, -50%);
    bottom: auto !important;
    width: 300px;
    max-width: 85vw;
    max-height: 70vh;
    overflow: hidden;
    z-index: 99999;
  }

  #offer-modal .modal-body {
    padding: 6px;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    max-height: calc(70vh - 30px);
  }

  #offer-modal .modal-body .offers,
  #offer-modal .modal-body #offer-info {
    flex-shrink: 1;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  #offer-modal .modal-body .tab-flex,
  #offer-modal .modal-body hr,
  #offer-modal .modal-body .modal-footer {
    flex-shrink: 0;
  }

  #offer-modal .modal-header {
    padding: 6px;
    font-size: 12px;
  }

  #offer-modal .offers {
    max-height: 80px;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: auto;
  }

  #offer-modal .offers::-webkit-scrollbar {
    width: 6px;
  }

  #offer-modal .offers::-webkit-scrollbar-thumb {
    background: rgba(255, 215, 0, 0.4);
    border-radius: 3px;
  }

  #offer-modal .offers::-webkit-scrollbar-track {
    background: rgba(0, 0, 0, 0.3);
  }

  /* Fade hint at bottom of scrollable offer grid */
  #offer-modal .offers {
    border-bottom: 2px solid rgba(255, 215, 0, 0.3);
  }

  /* Enable touch interaction on amount controls */
  #buy-count-wrapper {
    touch-action: manipulation;
    pointer-events: auto;
  }

  #buy-count {
    touch-action: manipulation;
  }

  #buy-count-minus,
  #buy-count-plus {
    touch-action: manipulation;
    min-width: 36px;
    min-height: 36px;
  }

  #hotbar-add-modal {
    position: fixed !important;
    top: 50% !important;
    left: 50% !important;
    transform: translate(-50%, -50%) !important;
    bottom: auto !important;
    width: 280px;
    max-width: 85vw;
    max-height: 70vh;
    overflow: hidden;
    z-index: 99999;
  }

  #hotbar-add-modal .modal-body {
    padding: 6px;
    max-height: calc(70vh - 40px);
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  #hotbar-add-modal .modal-header {
    padding: 6px;
    font-size: 12px;
  }

  .hotbar-add-tab {
    padding: 6px 4px;
    font-size: 11px;
  }

  .hotbar-item-grid,
  #hotbar-spell-list {
    max-height: none;
    overflow-y: visible;
  }

  /* Hide spell search on mobile */
  #hotbar-spell-search {
    display: none;
  }

  /* Compact spell list for mobile */
  #hotbar-spell-list {
    display: flex;
    flex-direction: column;
    gap: 2px;
  }

  #hotbar-spell-list .spellbook-entry {
    padding: 4px;
  }

  #hotbar-spell-list .spellbook-entry canvas {
    width: 24px;
    height: 24px;
    flex: 0 0 24px;
    margin-right: 6px;
  }

  #hotbar-spell-list .spellbook-entry .spell-info {
    font-size: 11px;
    line-height: 1.2;
  }

  #hotbar-spell-list .spellbook-entry .spell-info small,
  #hotbar-spell-list .spellbook-entry .spell-info span {
    display: none;
  }

  #travel-modal {
    position: fixed !important;
    top: 50% !important;
    left: 50% !important;
    transform: translate(-50%, -50%);
    bottom: auto !important;
    width: 280px;
    max-width: 85vw;
    max-height: 60vh;
    overflow: hidden;
    z-index: 99999;
  }

  #travel-modal .modal-body {
    padding: 6px;
    max-height: calc(60vh - 40px);
    overflow: hidden;
    display: flex;
    flex-direction: column;
  }

  #travel-modal .travel-destinations {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    /* Visible scroll track for mobile — iOS/Android hide scrollbars by default */
    scrollbar-width: auto;
  }

  #travel-modal .travel-destinations::-webkit-scrollbar {
    width: 6px;
  }

  #travel-modal .travel-destinations::-webkit-scrollbar-thumb {
    background: rgba(255, 215, 0, 0.4);
    border-radius: 3px;
  }

  #travel-modal .travel-destinations::-webkit-scrollbar-track {
    background: rgba(0, 0, 0, 0.3);
  }

  #travel-modal .modal-header {
    padding: 6px;
    font-size: 12px;
  }

  .travel-destination {
    padding: 10px 10px;
  }

  /* Trade Select Modal — centered, fits portrait and landscape */
  #trade-select-modal {
    position: fixed !important;
    top: 50% !important;
    left: 50% !important;
    transform: translate(-50%, -50%);
    bottom: auto !important;
    width: 280px;
    max-width: 85vw;
    max-height: 60vh;
    overflow: hidden;
    z-index: 99999;
  }

  #trade-select-modal .modal-body {
    padding: 6px;
    max-height: calc(60vh - 40px);
    overflow: hidden;
    display: flex;
    flex-direction: column;
  }

  #trade-select-modal .trade-select-players {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  #trade-select-modal .modal-header {
    padding: 6px;
    font-size: 12px;
  }

  .trade-select-player {
    padding: 10px;
  }

  /* Settings modal — centered, scrollable, above mobile overlays */
  #settings-modal {
    position: fixed !important;
    top: 50% !important;
    left: 50% !important;
    transform: translate(-50%, -50%);
    bottom: auto !important;
    width: 320px;
    max-width: 85vw;
    max-height: 70vh;
    overflow: hidden;
    z-index: 99999;
  }

  #settings-modal .modal-body {
    padding: 8px;
    max-height: calc(70vh - 40px);
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  #settings-modal .modal-header {
    padding: 6px;
    font-size: 12px;
  }

  #settings-modal .checkboxes {
    flex-direction: column;
    gap: 2px;
  }

  #settings-modal .checkboxes label {
    padding-right: 0;
    font-size: 11px;
  }

  #settings-modal .settings-row {
    display: flex;
    flex-direction: column;
    gap: 2px;
    margin: 4px 0;
  }

  #settings-modal .settings-row label {
    font-size: 11px;
  }

  #settings-modal .settings-row select,
  #settings-modal .settings-row input[type="range"] {
    width: 100%;
    box-sizing: border-box;
  }

  #settings-modal hr {
    margin: 6px 0;
  }

  /* Character Selection Modal — centered for mobile */
  #character-select-modal {
    position: fixed !important;
    top: 50% !important;
    left: 50% !important;
    transform: translate(-50%, -50%);
    bottom: auto !important;
    width: 280px;
    max-width: 85vw;
    max-height: 70vh;
    overflow: hidden;
    z-index: 99999;
  }

  #character-select-modal .modal-body {
    padding: 6px;
    max-height: calc(70vh - 40px);
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  #character-select-modal .modal-header {
    padding: 6px;
    font-size: 12px;
  }

  .character-select-row {
    padding: 10px;
  }

  .character-create-form input,
  .character-create-form select {
    font-size: 14px;
    padding: 8px;
  }
}

/* NPC Conversation Dialog - floating popup for keyword choices */
#npc-dialog {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  z-index: 29998;
  background-image: url("/png/bg.png");
  border: 1px solid black;
  box-shadow: 0px 0px 10px 0px black;
  padding: 6px;
  min-width: 200px;
  max-width: min(340px, 90vw);
  text-align: center;
}

#npc-dialog-close {
  position: absolute;
  top: -8px;
  right: -8px;
  cursor: pointer;
  color: #fff;
  font-size: 14px;
  font-weight: bold;
  line-height: 1;
  z-index: 1;
  width: 18px;
  height: 18px;
  background: #555;
  border: 1px solid #888;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}

#npc-dialog-close:hover {
  background: #c33;
  border-color: #e55;
}

#npc-dialog-header {
  background-image: url("/png/bg2.png");
  padding: 4px 12px;
  margin-bottom: 6px;
  font-weight: bold;
  font-size: 12px;
  white-space: nowrap;
}

#npc-dialog-message {
  padding: 6px 8px;
  margin-bottom: 6px;
  font-size: 11px;
  line-height: 1.4;
  color: #ddd;
  text-align: left;
  max-height: 120px;
  overflow-y: auto;
}

#npc-dialog-message .npc-keyword {
  color: #FFD700;
  font-weight: bold;
}

#npc-dialog-buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  justify-content: center;
}

#npc-dialog-buttons button {
  padding: 6px 14px;
  font-weight: bold;
  color: #FFD700;
  border: 1px solid rgba(255, 215, 0, 0.4);
  border-radius: 3px;
  cursor: pointer;
  font-size: 12px;
  background-image: url("/png/bg3.png");
}

#npc-dialog-buttons button:hover {
  color: #FFF;
  border-color: #FFD700;
  background-image: url("/png/bg2.png");
}