/* Shared refinements: glossy light surfaces, crisp midnight colors, quieter focus. */
:root:not([data-theme]),
:root[data-theme='light'] {
  --background: #d9f2f6;
  --button-sheen: linear-gradient(#f6feff 0%, #d7f2f7 48%, #bee4ed 52%, #d9f1f5 100%);
  --hover: #c3edf4;
  --active: #b2dce7;
  --border: #86b6c4;
  --text: #173f50;
  --muted: #496d7b;
  --focus: #056f90;
  --input: #f5fcfe;
  --panel-fill: #edf8fa;
  --panel-inset: #d3ebf0;
  --apsu-panel: linear-gradient(160deg, #fbfeff 0%, #ecf8fa 44%, #d3edf3 100%);
  --page-gradient:
    radial-gradient(ellipse at 12% 0%, #f0fff1 0%, transparent 47%),
    linear-gradient(155deg, #cceaf3, #afd8e6 65%, #d0efed);
  --world-panel: linear-gradient(150deg, #d9f4f7, #addce8);
  --send-button: linear-gradient(#39b8d2 0%, #1088ae 48%, #086f98 52%, #1383a5 100%);
  --separator: #c5e2e9;
  --system-fill: #deedf2;
  --apsu-shadow: 0 1px 0 #ffffffd9 inset, 0 6px 20px #184a5d12;
}
:root[data-theme='midnight'] {
  --background: #061526;
  --text: #e4f1ff;
  --muted: #a0bad3;
  --focus: #8fcbff;
  --button-sheen: linear-gradient(#173c5b, #102e49);
  --hover: #1d4a6e;
  --active: #21577e;
  --border: #305572;
  --input: #081d31;
  --panel-fill: #0b243a;
  --panel-inset: #0d2b44;
  --apsu-panel: linear-gradient(150deg, #112f49, #0b2238 75%);
  --page-gradient: linear-gradient(#061526, #061526);
  --world-panel: linear-gradient(#17435c, #102b44);
  --separator: #25465f;
  --send-button: linear-gradient(#26729a, #1a557c);
  --system-fill: #16334a;
  --apsu-shadow: 0 1px 0 #7ebbe916 inset, 0 8px 24px #0002;
}
:root[data-theme='midnight'] body.app-page::before {
  background-image:
    radial-gradient(circle at 31px 46px, #d0efff 0 0.65px, transparent 1.4px),
    radial-gradient(circle at 227px 177px, #b6d6fc 0 0.6px, transparent 1.3px);
  background-size: 467px 389px;
  opacity: 0.45;
  animation-duration: 16s;
}
:root[data-theme='midnight'] body.app-page::after {
  background-image: none;
}
.app-page :is(button, .button-link, summary) {
  transition:
    background-color 0.12s,
    border-color 0.12s;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}
:root[data-input-mode='pointer']
  .app-page
  :is(button, a, input, textarea, select, summary, [tabindex]):focus {
  outline: none;
  box-shadow: none;
}
:root[data-input-mode='keyboard']
  .app-page
  :is(button, a, input, textarea, select, summary, [tabindex]):focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}
.app-page :is(input, textarea, select):focus {
  border-color: var(--focus);
}
.app-page #chat-messages li {
  padding: 9px 4px;
  line-height: 1.65;
  overflow-wrap: anywhere;
}
.app-page #chat-messages strong {
  display: inline;
  text-transform: none;
  font:
    700 14px/1.65 'Nunito Sans',
    sans-serif;
  margin-right: 4px;
}
.app-page #chat-messages p {
  display: inline;
  font:
    400 15px/1.65 'Lora',
    serif;
  margin: 0;
}
.app-page #chat-messages time {
  float: right;
  font-size: 10px;
  margin-left: 12px;
  padding-top: 3px;
  opacity: 0.7;
}
.app-page #chat-messages .message-delivery {
  display: block;
  color: var(--muted);
  font:
    11px/1.5 'Nunito Sans',
    sans-serif;
}
.app-page #chat-messages .system-message {
  border-radius: 8px;
  padding: 8px 10px;
}
.app-page #chat-messages .system-message p {
  font-family: 'Nunito Sans', sans-serif;
  font-size: 13px;
}
.app-page #chat-form {
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: end;
}
.app-page #chat-input {
  grid-column: 1/-1;
  grid-row: 2;
  resize: vertical;
  overflow: auto;
  min-height: 58px;
  max-height: 40vh;
  width: 100%;
  box-sizing: border-box;
  line-height: 1.5;
}
.app-page #chat-form .chat-attachment-tools {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  grid-column: 1;
  grid-row: 4;
}
.app-page #chat-form .chat-attachment-tools label {
  display: inline-flex;
  cursor: pointer;
  font-size: 12px;
  gap: 6px;
}
.app-page .chat-attachment-tools input {
  max-width: 180px;
  font-size: 11px;
}
.app-page #chat-form #chat-image-preview {
  grid-column: 1/-1;
  grid-row: 3;
}
.app-page #chat-image-preview:empty {
  display: none;
}
.chat-attachments {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin-top: 8px;
}
.chat-attachments img {
  display: block;
  max-width: 170px;
  max-height: 150px;
  border-radius: 6px;
  object-fit: contain;
}
.chat-attachments button {
  padding: 4px;
}
.chat-attachments > div {
  display: grid;
  gap: 4px;
}
.app-page #chat-messages {
  height: var(--chat-height, 40vh);
  min-height: 180px;
  flex: none;
  resize: vertical;
  overflow: auto;
}
.app-page .room-chat {
  min-height: 0;
}
.avatar-frame {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: none;
  overflow: hidden;
  border-radius: 50%;
  background: var(--active);
}
.avatar-frame img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
  border: 0;
  margin: 0;
  max-width: none;
}
.room-avatar {
  width: 34px;
  height: 34px;
  font-weight: 700;
}
.app-page .character-portrait-frame {
  width: 130px;
  height: 130px;
}
.app-page .character-portrait-frame img {
  width: 100%;
  height: 100%;
  border-radius: 0;
}
.app-page .player-portrait {
  border-radius: 50%;
}
.app-page .character-presence {
  display: flex;
  align-items: center;
  gap: 8px;
  text-align: left;
  width: 100%;
  padding: 7px;
  background: transparent;
}
.typing-indicator {
  font-size: 11px;
  font-weight: 400;
  color: var(--muted);
  font-style: italic;
}
.circle-crop canvas {
  border-radius: 50%;
  box-shadow: 0 0 0 2px var(--border);
}
.world-profile-extras {
  margin-top: 20px;
  padding-top: 12px;
  border-top: 1px solid var(--border);
}
.player-badge {
  border: 1px solid var(--border);
}
.command-results article,
.connection-row,
#admin-locations article {
  padding: 12px;
  margin-top: 8px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--input);
}
.connection-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
}
#admin-locations small {
  display: block;
  color: var(--muted);
}
.online-player-list li {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}
@media (max-width: 600px) {
  .chat-attachments img {
    max-width: 120px;
    max-height: 120px;
  }
  .app-page #chat-messages {
    height: var(--chat-height, 45vh);
  }
  .app-page #chat-form {
    padding: 9px;
  }
  .chat-attachment-tools small {
    font-size: 10px;
  }
  .app-page .character-presence {
    flex-wrap: wrap;
  }
}
@media (prefers-reduced-motion: reduce) {
  .app-page :is(button, .button-link, summary) {
    transition: none;
  }
}

:root[data-theme='light'] {
  --button: #d6edf4;
}
:root[data-theme='midnight'] {
  --button: #12334f;
}
.app-page
  :is(button, .button-link, summary):not(#chat-send):not(.theme-option):not(.character-presence) {
  background-image: var(--button-sheen, none);
}

.app-page .room-chat {
  height: var(--chat-height, calc(100dvh - 220px));
  min-height: 500px;
  box-sizing: border-box;
}
.app-page #chat-messages {
  height: auto;
  min-height: 100px;
  flex: 1 1 auto;
  resize: none;
}
.app-page #chat-form {
  flex-shrink: 0;
}
.app-page #chat-input {
  max-height: 180px;
}
.app-page .chat-tools button {
  padding: 6px 9px;
}
.app-page .panel-grip {
  background-image: none !important;
}

.app-page #chat-form #chat-send {
  grid-column: 2;
  grid-row: 4;
}
.app-page #chat-form .field-feedback {
  grid-column: 1/-1;
  grid-row: 5;
}
.app-page #chat-form {
  margin: 0;
}
.app-page #chat-form > label {
  grid-row: 1;
}
.app-page #chat-input {
  margin: 0;
}
.app-page #chat-messages {
  min-height: 60px;
}
.app-page #room-characters li::before {
  display: none;
}

.app-page .chat-attachment-tools label {
  position: relative;
}
.app-page .chat-attachment-tools input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
  cursor: pointer;
}
.app-page .chat-attachment-tools label:focus-within {
  border-color: var(--focus);
}
.app-page #chat-image-preview {
  max-height: 120px;
  overflow: auto;
}
.app-page #chat-image-preview img {
  max-height: 65px;
}

/* Stability refinements */
.auth-links {
  display: grid;
  gap: 10px;
  margin-top: 18px;
}
.app-page .auth-links .button-link {
  margin: 0;
  text-align: center;
}
.app-page .room-chat {
  position: relative;
  padding-bottom: 28px;
}
.chat-resize-handle {
  position: absolute;
  bottom: 0;
  left: 12px;
  right: 12px;
  height: 20px;
  cursor: ns-resize;
  touch-action: none;
  user-select: none;
  display: grid;
  place-items: center;
}
.chat-resize-handle::after {
  content: '';
  width: 44px;
  height: 4px;
  border-radius: 4px;
  background: var(--border);
}
.chat-resize-handle:hover::after {
  background: var(--focus);
}
.app-page #world-map {
  padding: 8px;
  min-height: 150px;
  overflow: hidden;
}
.app-page #world-map svg {
  width: 100%;
  height: 150px;
  max-height: none;
}
.app-page #world-map .nearby-map text {
  font-size: 21px;
}
#room-owner {
  color: var(--muted);
  font-size: 12px;
  margin: 10px 0;
}
.portrait-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}
.app-page .apsu-image-crop canvas {
  cursor: grab;
  touch-action: none;
}
.app-page .apsu-image-crop canvas:active {
  cursor: grabbing;
}

.app-page .image-upload input[type='checkbox'] {
  display: inline;
  width: auto;
  margin-right: 8px;
}
