﻿/*
 * Final authoritative mobile/safe-area/admin-drawer overrides. Must load last.
 * Extracted from public/styles.css lines 4058-4399.
 */

/* =========================
   MOBILE SAFE-AREA POLISH
========================= */

#hamburger {
  display: none;
}

@media screen and (max-width: 900px), screen and (max-device-width: 480px), screen and (hover: none) and (pointer: coarse) and (max-width: 1100px) {
  html,
  body {
    min-height: 100%;
  }

  #container {
    height: 100vh;
    height: 100dvh;
    width: 100%;
    max-width: 100%;
    min-height: 0;
  }

  #sidebar {
    position: fixed;
    top: 0;
    left: 0;
    z-index: 500;
    width: 260px;
    max-width: calc(100vw - 42px);
    transform: translateX(-100%);
    transition: transform 0.25s ease;
    box-shadow: 4px 0 24px rgba(0, 0, 0, 0.6);
  }

  #sidebar.open {
    transform: translateX(0);
  }

  #chat-col {
    width: 100% !important;
    flex: 1 1 auto;
    min-height: 0;
    min-width: 0;
  }

  #mobile-bar {
    display: none;
  }

  #chat-headerbar {
    min-height: 56px;
    gap: 9px;
    padding: calc(7px + env(safe-area-inset-top, 0px)) 10px 7px;
    box-sizing: border-box;
    flex-wrap: wrap;
  }

  #hamburger {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex: 0 0 auto;
    min-width: 44px;
    min-height: 44px;
    font-size: 20px;
    padding: 0;
    border: 1px solid rgba(231, 199, 143, 0.16);
    border-radius: 12px;
    background: rgba(48, 41, 34, 0.7);
    color: #f2e7d8;
  }

  #sidebar-nav {
    flex: 1 1 auto;
    display: flex;
    flex-direction: column;
    min-height: 0;
    overflow: hidden;
    gap: 0;
    padding: 8px 12px 0;
  }

  .sidebar-mobile-controls:not([hidden]) {
    order: 0;
    flex: 0 0 auto;
    gap: 0;
    padding: 8px 0 6px;
    border-bottom: 0;
  }

  .sidebar-workspace-actions {
    order: 1;
    flex: 0 0 auto;
    padding: 6px 0 8px;
    margin: 0;
  }

  .chat-session-list-wrap {
    order: 2;
    flex: 1 1 auto;
    min-height: 92px;
    max-height: none;
    padding: 6px 0 10px;
    overflow: hidden;
  }

  .chat-session-list {
    flex: 1 1 auto;
    min-height: 0;
    max-height: none;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
  }

  .chat-session-open {
    min-height: 50px;
    align-items: flex-start;
    text-align: left;
    padding: 11px 10px;
  }

  .chat-session-title {
    width: 100%;
    text-align: left;
    font-size: 16px;
    line-height: 1.35;
  }

  .chat-session-empty {
    font-size: 14px;
  }

  .chat-session-delete {
    min-width: 38px;
    min-height: 38px;
    font-size: 18px;
  }

  .chat-session-icon {
    min-width: 38px;
    min-height: 38px;
    font-size: 17px;
    opacity: 0.9;
  }

  .chat-session-confirm-delete {
    min-height: 38px;
    padding: 0 12px;
    font-size: 13px;
  }

  .chat-session-rename-input {
    min-height: 40px;
    font-size: 17px;
    margin-left: 8px;
  }

  .sidebar-utility-nav button,
  #sidebar-footer button {
    min-height: 40px;
    font-size: 14px;
  }

  .sidebar-support-link {
    min-height: 26px;
    font-size: 12px;
  }

  .sidebar-mobile-control-label {
    display: none !important;
  }

  .sidebar-mobile-control-group {
    gap: 0;
  }

  .sidebar-account {
    display: none !important;
  }

  #sidebar-footer {
    flex: 0 0 auto;
    margin-top: auto;
    padding: 9px 12px calc(12px + env(safe-area-inset-bottom, 0px));
    gap: 2px;
  }

  .sidebar-utility-nav {
    gap: 2px;
  }

  .sidebar-footer-row {
    gap: 5px;
  }

  #user-bar-email {
    font-size: 14px;
  }

  .sidebar-status {
    font-size: 13px;
  }

  #chat-headerbar #profile {
    min-width: 0;
    min-height: 46px;
    padding: 9px 12px;
    font-size: 17px;
  }

  #chat-headerbar .chat-style-topbar {
    flex: 1 1 42%;
    min-width: 0;
  }

  #chat-headerbar .chat-style-select {
    min-height: 46px;
    padding: 9px 12px;
    font-size: 17px;
  }

  .workspace-switcher {
    flex: 1 1 auto;
    min-width: 0;
    justify-content: stretch;
  }

  .workspace-switcher button {
    flex: 1 1 0;
    min-height: 40px;
    padding: 8px 9px;
    font-size: 15px;
  }

  #chat-headerbar.ai-lab-header {
    flex-wrap: wrap;
    align-content: center;
  }

  #chat-headerbar.ai-lab-header .workspace-switcher {
    order: 2;
    flex: 1 1 100%;
    width: 100%;
  }

  #chat-headerbar.ai-lab-header #profile {
    order: 3;
    flex: 0 1 28%;
    max-width: 32%;
  }

  #chat-headerbar.ai-lab-header .mai-style-topbar {
    order: 4;
    flex: 1 1 100%;
    width: 100%;
    gap: 7px;
  }

  #chat-headerbar.ai-lab-header .mai-style-select {
    min-height: 46px;
    padding: 9px 12px;
    font-size: 17px;
    max-width: 100%;
  }

  #chat-headerbar.ai-lab-header .mai-orch-select {
    min-height: 40px;
    padding: 8px 12px;
    font-size: 15px;
  }

  #chat .msg {
    margin: 18px 0;
  }

  #mai-chat .mai-msg {
    margin-bottom: 19px;
  }

  #chat .msg .bubble,
  #chat .msg.user .bubble.user,
  #chat .msg.assistant .bubble.assistant,
  #mai-chat .mai-msg .mai-bubble,
  #mai-chat .mai-msg-a .mai-bubble,
  #mai-chat .mai-msg-b .mai-bubble,
  #mai-chat .mai-msg-user .mai-bubble {
    padding: 17px 19px;
    font-size: 20px;
    line-height: 1.7;
  }

  #chat .history-label,
  #mai-chat .mai-msg-label {
    font-size: 13px;
    line-height: 1.3;
  }

  #chat .timestamp,
  #chat .message-time,
  #chat .meta,
  #chat .chat-history-hint,
  #chat .token-badge,
  #mai-chat .mai-meta,
  #mai-chat .mai-session-bar {
    font-size: 13px;
    line-height: 1.35;
  }

  #topbar-gear {
    min-width: 46px;
    min-height: 46px;
    font-size: 21px;
    margin: 0;
    border-color: transparent !important;
    background: transparent !important;
    box-shadow: none !important;
  }

  #chat,
  #mai-chat {
    padding-bottom: 14px;
    min-height: 0;
  }

  #input-area {
    position: sticky;
    bottom: 0;
    z-index: 20;
    gap: 10px;
    padding: 9px 10px calc(11px + env(safe-area-inset-bottom, 0px));
    box-sizing: border-box;
    background: linear-gradient(180deg, rgba(25, 21, 18, 0.96) 0%, #211c18 100%);
  }

  .chat-language-row {
    width: 100%;
    min-width: 0;
    min-height: 34px;
    justify-content: flex-start;
    flex-wrap: nowrap;
    gap: 6px;
    overflow: hidden;
  }

  .chat-runtime-pill {
    flex: 0 1 auto;
    max-width: 100%;
    min-width: 0;
    min-height: 36px;
  }

  .chat-runtime-pill:has(#language) {
    flex: 0 1 44%;
    max-width: 156px;
  }

  .chat-runtime-pill:has(#modelProvider) {
    flex: 1 1 0;
  }

  .chat-language-row select {
    width: 100%;
    min-height: 36px;
    min-width: 0;
    max-width: none;
    field-sizing: fixed;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 15px;
    padding: 7px 24px 7px 6px;
  }

  #model {
    max-width: none;
  }

  #input-area textarea#message {
    min-height: 88px;
    height: 88px;
    padding: 15px 17px;
    font-size: 19px;
    line-height: 1.5;
  }

  #input-area textarea#message::placeholder {
    font-size: 18px;
  }

  #send-btn {
    width: 80px;
    height: 88px;
    min-height: 88px;
    font-size: 19px;
    font-weight: 850;
  }

  .mai-mobile-quick-controls.visible {
    justify-content: space-between;
    gap: 6px 10px;
    padding: 6px 10px 7px;
  }

  .mai-session-control {
    gap: 7px;
    min-height: 38px;
  }

  .mai-session-control-rounds {
    flex-basis: auto;
  }

  .mai-mobile-control-label {
    font-size: 13px;
    letter-spacing: 0.06em;
  }

  .mai-switch {
    width: 46px;
    height: 28px;
  }

  .mai-switch span {
    width: 20px;
    height: 20px;
  }

  .mai-switch.is-on span {
    transform: translateX(18px);
  }

  .mai-round-stepper button,
  .mai-round-stepper .mai-ctrl-input {
    min-height: 38px;
  }

  .mai-round-stepper button {
    width: 38px;
    font-size: 18px;
  }

  .mai-round-stepper .mai-ctrl-input {
    width: 46px;
    font-size: 17px;
  }

  /* Hide "Rounds" label on mobile — stepper is self-explanatory, saves row space */
  .mai-session-control-rounds .mai-mobile-control-label {
    display: none;
  }

  /* Compact stop icon sized to match stepper button height for vertical alignment */
  .mai-stop-ctrl-btn {
    width: 30px;
    height: 30px;
    font-size: 12px;
    line-height: 30px;
  }

  .mai-bot-row select,
  #topbar-controls select,
  .row select,
  .field select,
  .settings-bot-col select {
    min-height: 46px;
    padding: 11px 12px;
    font-size: 17px;
  }

  #sidebar {
    height: 100vh;
    height: 100dvh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    overscroll-behavior: contain;
  }

  #sidebar-footer {
    padding-bottom: calc(12px + env(safe-area-inset-bottom, 0px));
  }

  #right {
    display: none;
    position: fixed;
    inset: 0;
    z-index: 600;
    border-left: none;
    height: 100vh;
    height: 100dvh;
    width: 100%;
    min-width: 0;
    overflow: hidden;
  }

  body.studio-right-hidden #right:not(.mobile-open) {
    display: none;
  }

  #right.mobile-open {
    display: flex;
    flex-direction: column;
    max-height: 100dvh;
    overscroll-behavior: contain;
  }

  #resizer {
    display: none;
  }

  #mobile-back-bar {
    min-height: 44px;
    padding: calc(8px + env(safe-area-inset-top, 0px)) 12px 8px;
    box-sizing: border-box;
  }

  #panelContent {
    flex: 1 1 auto;
    min-height: 0;
    overflow: hidden;
  }

  .section.scrollable,
  .admin-content {
    -webkit-overflow-scrolling: touch;
  }

  .admin-shell {
    min-height: 0;
    height: 100%;
  }

  .admin-nav {
    position: sticky;
    top: 0;
    z-index: 5;
    flex-shrink: 0;
    padding: 10px 12px;
    background: rgba(31, 26, 22, 0.98);
    backdrop-filter: blur(10px);
    scrollbar-width: thin;
  }

  .admin-nav::before {
    content: "Admin";
    flex: 0 0 auto;
    align-self: center;
    color: #b59f82;
    font-size: 11px;
    font-weight: 700;
    letter-spacing: 0.12em;
    text-transform: uppercase;
    padding: 0 4px;
  }

  .admin-nav-item {
    min-height: 36px;
  }

  .admin-content {
    min-height: 0;
    padding: 18px 14px calc(22px + env(safe-area-inset-bottom, 0px));
  }

  body.topbar-sheet-open #topbar-controls {
    padding: 18px 16px calc(28px + env(safe-area-inset-bottom, 0px));
    max-height: calc(100dvh - 24px);
    overflow-y: auto;
  }

  #topbar-controls,
  .settings-page,
  .section,
  .admin-content {
    font-size: 16px;
  }

  .settings-page input,
  .settings-page textarea,
  .settings-page select,
  .settings-page button,
  #topbar-controls select,
  #topbar-controls button,
  .form-group input,
  .form-group textarea,
  .form-group select,
  .form-group button {
    font-size: 16px;
  }

  .settings-page label,
  .settings-hint,
  .settings-desc,
  .field-hint {
    font-size: 14px;
    line-height: 1.45;
  }

  .settings-nav-item {
    min-height: 42px;
    font-size: 15px;
  }
}

/* =========================
   PWA STANDALONE SAFE-AREA
========================= */

/* In installed PWA mode (standalone), the status bar overlays the very top of the
   screen because we use apple-mobile-web-app-status-bar-style: black-translucent.
   The chat header already accounts for this via env(safe-area-inset-top), but the
   sidebar overlay starts at top:0 and its brand bar needs the same treatment. */
@media screen and (max-width: 900px), screen and (max-device-width: 480px), screen and (hover: none) and (pointer: coarse) and (max-width: 1100px) {
  .sidebar-brand {
    padding-top: calc(18px + env(safe-area-inset-top, 0px));
    min-height: calc(78px + env(safe-area-inset-top, 0px));
  }
}

/* =========================
   ADMIN DRAWER NAVIGATION
========================= */

.admin-drawer-overlay,
.admin-mobile-menu-btn,
.admin-nav-close {
  display: none;
}

.admin-shell {
  display: grid;
  grid-template-columns: 186px minmax(0, 1fr);
  background: #191512;
}

.admin-nav {
  width: auto;
  min-width: 0;
  padding: 18px 12px;
  gap: 6px;
  background: #141413;
  border-right: 1px solid rgba(245, 239, 230, 0.05);
}

.admin-nav-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin: 0 4px 14px;
  padding: 0 0 14px;
}

.admin-nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 40px;
  padding: 9px 12px;
  border-radius: 9px;
  background: transparent;
  border-color: transparent;
}

.admin-nav-icon {
  width: 18px;
  flex: 0 0 18px;
  text-align: center;
}

.admin-content {
  min-width: 0;
}

@media screen and (max-width: 900px), screen and (max-device-width: 480px), screen and (hover: none) and (pointer: coarse) and (max-width: 1100px) {
  .admin-shell {
    display: flex;
    flex-direction: column;
    position: relative;
    height: 100%;
    min-height: 0;
  }

  .admin-mobile-header {
    position: sticky;
    top: 0;
    z-index: 6;
    display: flex;
    flex-direction: column;
    gap: 6px;
    margin: 0 0 14px;
    padding: 0 0 10px;
    background: #191512;
  }

  .admin-mobile-title-row {
    display: flex;
    align-items: center;
    gap: 10px;
    min-width: 0;
  }

  .admin-mobile-menu-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex: 0 0 38px;
    width: 38px;
    min-width: 38px;
    height: 38px;
    min-height: 38px;
    margin: 0;
    padding: 0;
    border: 1px solid rgba(231, 199, 143, 0.2);
    border-radius: 12px;
    background: rgba(48, 41, 34, 0.94);
    color: #f2e7d8;
    font-size: 18px;
    font-weight: 700;
  }

  .admin-mobile-menu-label {
    display: none;
  }

  .admin-mobile-current-title {
    min-width: 0;
    overflow: hidden;
    color: #fff7eb;
    font-size: 21px;
    font-weight: 800;
    line-height: 1.15;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .admin-back-to-chat {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    align-self: flex-start;
    min-height: 28px;
    margin: 0;
    padding: 2px 0;
    border: 0;
    background: transparent;
    color: #d8cbb8;
    font-size: 13px;
    font-weight: 700;
  }

  .admin-page-title {
    display: none;
  }

  .admin-nav {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    z-index: 750;
    width: min(282px, calc(100vw - 42px));
    flex-direction: column;
    gap: 8px;
    padding: calc(18px + env(safe-area-inset-top, 0px)) 14px calc(18px + env(safe-area-inset-bottom, 0px));
    border-right: 1px solid rgba(245, 239, 230, 0.07);
    border-bottom: 0;
    background: #141413;
    box-shadow: 20px 0 52px -30px rgba(0, 0, 0, 0.9);
    transform: translateX(-105%);
    transition: transform 0.22s ease;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  .admin-shell.admin-drawer-open .admin-nav {
    transform: translateX(0);
  }

  .admin-drawer-overlay {
    position: fixed;
    inset: 0;
    z-index: 740;
    background: rgba(0, 0, 0, 0.48);
  }

  .admin-shell.admin-drawer-open .admin-drawer-overlay {
    display: block;
  }

  .admin-nav::before {
    display: none;
  }

  .admin-nav-title {
    display: flex;
    margin: 0 4px 12px;
    padding: 0 0 14px;
  }

  .admin-nav-close {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    margin: 0;
    padding: 0;
    border: 1px solid rgba(231, 199, 143, 0.18);
    border-radius: 999px;
    background: rgba(48, 41, 34, 0.8);
    color: #d8cbb8;
    font-size: 20px;
    line-height: 1;
  }

  .admin-nav-item {
    width: 100%;
    flex: 0 0 auto;
    min-height: 44px;
    padding: 10px 13px;
    font-size: 15px;
    white-space: normal;
    background: transparent;
    border-color: transparent;
  }

  .admin-nav-item.active::before {
    left: 6px;
    top: 50%;
    bottom: auto;
    width: 4px;
    height: 18px;
    transform: translateY(-50%);
  }

  .admin-content {
    flex: 1;
    overflow-y: auto;
    padding: 16px 14px calc(22px + env(safe-area-inset-bottom, 0px));
  }
}

@media screen and (orientation: landscape) and (max-height: 520px) and (hover: none) and (pointer: coarse) and (max-width: 1100px) {
  #chat,
  #mai-chat {
    padding: 8px 7px 8px 3px !important;
  }

  #chat .msg,
  #mai-chat .mai-msg {
    margin-left: 0 !important;
    margin-right: 0 !important;
  }

  #chat .msg .bubble,
  #chat .msg.user .bubble.user,
  #chat .msg.assistant .bubble.assistant,
  #mai-chat .mai-msg .mai-bubble,
  #mai-chat .mai-msg-a .mai-bubble,
  #mai-chat .mai-msg-b .mai-bubble,
  #mai-chat .mai-msg-user .mai-bubble {
    max-width: calc(100% - 8px) !important;
  }

  .chat-language-row {
    display: none !important;
  }

  #input-area {
    gap: 8px !important;
    padding: 6px 7px calc(7px + env(safe-area-inset-bottom, 0px)) 3px !important;
  }

  #input-area .chat-composer-main {
    gap: 3px !important;
  }

  #input-area textarea#message {
    height: 54px !important;
    min-height: 54px !important;
    padding: 10px 12px !important;
    font-size: 17px !important;
    line-height: 1.35 !important;
  }

  #input-area textarea#message::placeholder {
    font-size: 16px !important;
  }

  #send-btn {
    width: 68px !important;
    height: 54px !important;
    min-height: 54px !important;
    margin-top: 0 !important;
    font-size: 16px !important;
  }

  .message-char-status {
    min-height: 10px !important;
    font-size: 9px !important;
    line-height: 1 !important;
  }

  .mai-input-area {
    padding: 6px 8px calc(7px + env(safe-area-inset-bottom, 0px)) !important;
  }

  .mai-input-row {
    gap: 8px !important;
    min-height: 54px !important;
  }

  .mai-input-row input[type="text"],
  .mai-input-row button {
    height: 54px !important;
    min-height: 54px !important;
  }

  .mai-input-row input[type="text"] {
    padding: 10px 12px !important;
    font-size: 17px !important;
  }

  .mai-input-row button {
    width: 68px !important;
    font-size: 16px !important;
  }

  .mai-status {
    margin-top: 4px !important;
  }
}

/* Mobile AI Config drawer: keep close/back access reachable on iPhone. */
@media screen and (max-width: 900px), screen and (max-device-width: 480px), screen and (hover: none) and (pointer: coarse) and (max-width: 1100px) {
  body.aiconfig-drawer-open {
    overflow: hidden;
  }

  .aiconfig-drawer-scrim {
    z-index: 880;
    overscroll-behavior: contain;
  }

  .aiconfig-drawer {
    inset: 0;
    width: 100vw;
    max-width: none;
    min-width: 0;
    height: 100vh;
    height: 100dvh;
    z-index: 890;
    border-left: 0;
    border-radius: 0;
    overflow: hidden;
    overscroll-behavior: contain;
  }

  .aiconfig-drawer-head {
    position: sticky;
    top: 0;
    z-index: 2;
    min-height: calc(56px + env(safe-area-inset-top, 0px));
    padding: calc(8px + env(safe-area-inset-top, 0px)) 14px 10px;
    gap: 9px;
    background: rgba(14, 17, 23, 0.98);
    border-bottom-color: rgba(231, 199, 143, 0.14);
    box-shadow: 0 12px 28px -24px rgba(0, 0, 0, 0.9);
  }

  .aiconfig-drawer-mobile-back {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex: 0 0 44px;
    width: 44px;
    height: 44px;
    margin: 0;
    padding: 0;
    border: 0;
    border-radius: 999px;
    background: rgba(245, 239, 230, 0.045);
    color: #f2e7d8;
    font-size: 24px;
    line-height: 1;
  }

  .aiconfig-drawer-eyebrow {
    display: none;
  }

  .aiconfig-drawer-title {
    min-width: 0;
    overflow: hidden;
    font-size: 17px;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .aiconfig-drawer-pill {
    max-width: 46vw;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .aiconfig-drawer-close {
    display: none;
  }

  .aiconfig-drawer-body {
    flex: 1 1 auto;
    min-height: 0;
    padding: 18px 14px calc(24px + env(safe-area-inset-bottom, 0px));
    overflow-y: auto;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
  }
}
