@layer layouts {
  .app-layout {
    display: grid;
    grid-template-columns: auto 1fr;
    grid-template-rows: auto 1fr;
    height: 100vh;
    height: calc(100vh - env(safe-area-inset-bottom, 0));
    width: 100vw;
    padding-bottom: env(safe-area-inset-bottom, 0);
  }

  .app-header {
    grid-column: 1 / -1;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 var(--space-4);
    padding-top: env(safe-area-inset-top, 0);
    background: var(--color-bg-header);
    border-bottom: 1px solid var(--color-border);
    min-height: calc(var(--header-height) + env(safe-area-inset-top, 0));
  }

  .header-left,
  .header-right {
    display: flex;
    align-items: center;
    gap: var(--space-2);
  }

  .schema-panel {
    grid-row: 2;
    overflow: hidden;
    border-right: 1px solid var(--color-border);
    background: var(--color-bg-surface);
    position: relative;
    display: flex;
    flex-direction: column;
    min-height: 0;
    width: var(--sidebar-width, 260px);
    transition: width 0.2s ease;
  }
  .schema-panel.notrans,
  .schema-panel.notrans .sidebar-inner {
    transition: none !important;
  }
  .sidebar-inner {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow-y: auto;
    min-height: 0;
    transition: transform 0.2s ease;
  }
  .schema-panel.collapsed .sidebar-inner {
    transform: translateX(-100%);
  }
  .sidebar-collapse-btn {
    position: absolute;
    top: 4px;
    right: 2px;
    z-index: 20;
    padding: 2px 5px;
    font-size: 12px;
    line-height: 1;
    cursor: pointer;
    background: var(--color-bg-hover);
    border: 1px solid var(--color-border-light);
    color: var(--color-text-muted);
    border-radius: 4px;
    opacity: 0.7;
  }
  .sidebar-collapse-btn:hover {
    opacity: 1;
    background: var(--color-bg-surface);
    color: var(--color-text);
  }

  .schema-resize-handle {
    position: fixed;
    top: 0;
    left: 0;
    width: 8px;
    height: 100vh;
    cursor: col-resize;
    z-index: 100;
    background: transparent;
  }
  .schema-resize-handle::after {
    content: '';
    position: absolute;
    top: 0;
    left: 2px;
    width: 4px;
    height: 100%;
    background: transparent;
    transition: background .15s;
  }
  .schema-resize-handle:hover::after,
  .schema-resize-handle:active::after {
    background: var(--color-accent);
  }

  .schema-resize-handle::after {
    content: '';
    position: absolute;
    top: 0;
    left: 3px;
    width: 4px;
    height: 100%;
    background: transparent;
    transition: background .15s;
  }

  @media (max-width: 768px) {
    .schema-resize-handle {
      display: none;
    }
  }

  .schema-toggle {
    display: none;
  }

  .mobile-menu-btn {
    display: none;
  }

  .schema-close {
    display: none;
  }

  .mobile-menu-panel {
    display: none;
  }

  .main-content {
    grid-row: 2;
    display: flex;
    flex-direction: column;
    overflow: hidden;
  }

  .template-bar {
    display: flex;
    align-items: center;
    gap: var(--space-1);
    padding: var(--space-1) var(--space-3);
    border-bottom: 1px solid var(--color-border);
    min-height: var(--template-bar-height);
    background: var(--color-bg-surface);
  }

  .tab-bar {
    display: flex;
    align-items: stretch;
    min-height: 30px;
    background: var(--color-bg);
    overflow-x: auto;
    overflow-y: hidden;
    gap: 0;
    flex-shrink: 0;
  }

  .tab-item {
    display: flex;
    align-items: center;
    gap: var(--space-1);
    padding: 4px 12px;
    font-size: 12px;
    font-family: var(--font-mono);
    color: var(--color-text-muted);
    background: var(--color-bg-hover);
    border-right: 1px solid var(--color-border-light);
    cursor: pointer;
    user-select: none;
    white-space: nowrap;
    transition: background 0.1s, color 0.1s;
  }

  .tab-item:hover {
    background: var(--color-bg-active);
    color: var(--color-text);
  }

  .tab-item.active {
    background: var(--color-bg-editor);
    color: var(--color-text);
    border-top: 2px solid var(--color-accent);
  }

  .tab-item .tab-close,
  .tab-close-btn {
    font-weight: 900;
    font-size: 16px;
    color: var(--color-text-muted);
    padding: 0 4px;
    border: none;
    background: none;
    cursor: pointer;
    line-height: 1;
  }

  .tab-item .tab-close:hover,
  .tab-close-btn:hover {
    color: var(--color-error);
  }

  .tab-solution-label {
    margin: 6px;
    font-size: 12px;
    font-weight: 600;
    color: var(--color-accent);
    text-transform: uppercase;
    letter-spacing: .05em;
  }


  .tab-separator {
    width: 1px;
    background: var(--color-border);
    margin: 4px 2px;
    flex-shrink: 0;
  }

  .editor-pane-wrap {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
    overflow: hidden;
  }

  body.floating-tabs .editor-pane-wrap {
    position: relative;
    overflow: clip;
  }

  body.floating-tabs .tab-bar {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    z-index: 10;
    background: color-mix(in srgb, var(--color-bg) 50%, transparent);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
  }

  body.floating-tabs .tab-item {
    background: transparent;
    border-right-color: color-mix(in srgb, var(--color-border-light) 50%, transparent);
  }

  body.floating-tabs .tab-item:hover {
    background: color-mix(in srgb, var(--color-bg-active) 50%, transparent);
  }

  body.floating-tabs .tab-item.active {
    background: color-mix(in srgb, var(--color-bg-editor) 50%, transparent);
  }

  body.floating-tabs.has-tabs .cm-content {
    padding-top: calc(var(--space-2) + 30px) !important;
  }

  body.floating-bottom.has-tabs .cm-content {
    padding-bottom: 80px !important;
  }

  body.floating-bottom .execute-bar {
    position: relative;
    z-index: 5;
    background: color-mix(in srgb, var(--color-bg) 50%, transparent);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    border-bottom: 1px solid color-mix(in srgb, var(--color-border) 50%, transparent);
  }

  body.floating-bottom.has-tabs .execute-bar {
    margin-top: -36px;
  }

  body.floating-bottom #results-container {
    position: relative;
    z-index: 4;
  }

  body.floating-bottom.has-tabs #results-container {
    margin-top: 4px;
  }

  body.floating-bottom .results-header {
    background: color-mix(in srgb, var(--color-bg) 50%, transparent);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
  }

  body.floating-bottom .results-output {
    background: color-mix(in srgb, var(--color-bg) 50%, transparent);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
  }

  body.floating-bottom .results-table-wrapper {
    background: transparent;
  }

  body.floating-bottom .results-table tr td {
    background: color-mix(in srgb, var(--color-bg) 50%, transparent) !important;
  }

  body.floating-bottom .results-table tr:nth-child(even) td {
    background: color-mix(in srgb, var(--color-bg) 45%, transparent) !important;
  }

  body.floating-bottom .results-table tr:hover td {
    background: color-mix(in srgb, var(--color-bg-hover) 60%, transparent) !important;
  }

  body.floating-bottom .results-table th {
    background: color-mix(in srgb, var(--color-bg-header) 50%, transparent);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
  }

  body.floating-bottom .results-empty {
    background: transparent;
  }

  body.floating-bottom .results-error {
    background: color-mix(in srgb, var(--color-error) 15%, transparent);
  }

  .editor-container {
    flex: 1;
    overflow: hidden;
    min-height: 0;
  }

  .editor-split {
    flex: 1 1 var(--editor-size, 50%);
    display: flex;
    overflow: hidden;
    min-height: var(--editor-min-height);
  }

  .editor-pane {
    flex: 1;
    min-width: 0;
    overflow: hidden;
    display: flex;
    flex-direction: column;
  }

  .editor-pane.hidden {
    display: none;
  }

  .editor-divider {
    width: 4px;
    cursor: col-resize;
    background: transparent;
    transition: background 0.15s;
    flex-shrink: 0;
  }

  .editor-divider:hover,
  .editor-divider:active {
    background: var(--color-accent);
  }

  .editor-divider.hidden {
    display: none;
  }

  .editor-resize-handle {
    height: 4px;
    cursor: row-resize;
    background: transparent;
    transition: background 0.15s;
    flex-shrink: 0;
  }

  .editor-resize-handle:hover,
  .editor-resize-handle:active {
    background: var(--color-accent);
  }

  .execute-bar {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    padding: var(--space-1) var(--space-3);
    min-height: var(--execute-bar-height);
    background: var(--color-bg-surface);
    border-bottom: 1px solid var(--color-border);
  }

  .execute-hint {
    font-size: var(--size-fluid-1);
    color: var(--color-text-muted);
  }

  .execute-spacer {
    flex: 1;
  }

  .results-zoom-label {
    font-size: var(--size-fluid-1);
    color: var(--color-text-muted);
    margin-right: var(--space-1);
  }

  .results-zoom {
    width: 60px;
    accent-color: var(--color-accent);
  }

  .results-zoom-value {
    font-family: var(--font-mono);
    font-size: var(--size-fluid-1);
    color: var(--color-text-muted);
    min-width: 20px;
    text-align: center;
  }

  .results-container {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    min-height: 100px;
  }

  .results-header {
    padding: var(--space-1) var(--space-3);
    font-size: var(--size-fluid-1);
    color: var(--color-text-muted);
    border-bottom: 1px solid var(--color-border-light);
    background: var(--color-bg);
  }

  .results-output {
    flex: 1;
    overflow: hidden;
    padding: 0;
  }

  @media (max-width: 768px) {
    .app-layout {
      grid-template-columns: 1fr;
    }

    .schema-toggle {
      display: inline-flex;
    }

    .mobile-menu-btn {
      display: inline-flex;
    }

    .header-right .btn,
    .header-right .mobile-menu-btn {
      display: none;
    }

    .header-right .recent-wrap,
    #btn-gensql {
      display: inline-flex;
    }

    .header-right .recent-wrap .btn {
      display: inline-flex;
    }

    .schema-panel {
      position: fixed;
      top: var(--header-height);
      left: 0;
      width: 80vw;
      max-width: 320px;
      height: calc(100vh - var(--header-height));
      z-index: 100;
      transform: translateX(-100%);
      transition: transform 0.2s ease;
      box-shadow: 2px 0 12px rgba(0,0,0,0.2);
    }

    .schema-panel.open {
      transform: translateX(0);
    }

    #schema-recent-dropdown {
      position: static;
      width: 100%;
      box-shadow: none;
      border-radius: 0;
      border: 1px solid var(--color-border);
      max-height: 150px;
      overflow-y: auto;
    }

    input, select, textarea, button, .btn, .dropdown-item, .schema-table-name,
    .schema-toggle, #btn-schema-close, .btn-schema-ddl, .btn-schema-drop,
    .btn-kbd, .btn-tpl, .btn-close, .btn-primary, .btn-danger, .btn-sm {
      font-size: 16px !important;
    }

    .app-header {
      padding: 0 var(--space-2);
    }

    .header-left, .header-right {
      gap: var(--space-1);
    }

    .header-right .btn {
      padding: var(--space-1) var(--space-1);
    }

    .header-right .recent-wrap .btn {
      font-size: 16px !important;
    }

    .execute-hint {
      display: none;
    }

    .main-content {
      transition: margin-left 0.2s ease, margin-right 0.2s ease;
    }

    .schema-close {
      display: inline-flex;
    }

    .schema-panel.overlay-backdrop::before {
      content: '';
      position: fixed;
      top: 0;
      left: 0;
      width: 100vw;
      height: 100vh;
      background: rgba(0,0,0,0.3);
      z-index: -1;
    }

    .mobile-menu-panel {
      display: flex;
      flex-direction: column;
      position: fixed;
      top: var(--header-height);
      right: 0;
      width: 260px;
      height: calc(100vh - var(--header-height));
      background: var(--color-bg-surface);
      border-left: 1px solid var(--color-border);
      z-index: 100;
      transform: translateX(100%);
      transition: transform 0.2s ease;
      box-shadow: -2px 0 12px rgba(0,0,0,0.2);
    }

    .mobile-menu-panel.open {
      transform: translateX(0);
    }

    .mobile-menu-header {
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: var(--space-3);
      border-bottom: 1px solid var(--color-border);
      font-weight: 600;
    }

    .mobile-menu-actions {
      display: flex;
      flex-direction: column;
      padding: var(--space-2);
      gap: var(--space-1);
    }

    .mobile-menu-actions .btn {
      width: 100%;
      justify-content: flex-start;
      padding: var(--space-2) var(--space-3);
    }
  }
}
