/* =============================================
   AniVerse module system (WidgetGrid / WidgetContainer / WidgetToolbar / ModulePanel)
   Shared by Title Details and Stats. Layout logic lives in the components;
   this file only presents it.
   Normal mode  = "my personalised page": quiet module frames.
   Arrange mode = "I am building my page": handles, rails, drop targets.
   ============================================= */

/* --- Entrance: one short fade, no stagger --- */
@keyframes widgetFadeInUp {
    from { opacity: 0; transform: translateY(4px); }
    to { opacity: 1; transform: translateY(0); }
}

.widget-enter {
    animation: widgetFadeInUp var(--av-dur-base) var(--av-ease-out) backwards; /* no lasting transform: fixed dialogs inside modules must stay viewport-anchored */
    animation-delay: 0s !important; /* overrides WidgetContainer's inline stagger */
}

/* --- Grid & columns (same flex model as before: up to 3 per row, then wrap) --- */
.widget-grid {
    display: flex;
    flex-direction: column;
    gap: var(--av-space-4);
}

.widget-columns {
    display: flex;
    flex-wrap: wrap;
    align-items: flex-start;
    gap: var(--av-space-4);
}

.widget-column {
    position: relative;
    flex: 1 1 calc(33.333% - var(--av-space-4));
    min-width: 280px;
    min-height: 48px;
    display: flex;
    flex-direction: column;
    gap: var(--av-space-4);
}

/* --- Module frame --- */
.widget-container {
    position: relative;
    min-width: 0;
    background: var(--av-surface-1);
    border: 1px solid var(--av-line-strong);
    border-radius: var(--av-radius-widget);
    transition: border-color var(--av-dur-fast) var(--av-ease-out), opacity var(--av-dur-fast) var(--av-ease-out),
        box-shadow var(--av-dur-base) var(--av-ease-out);
}

.widget-body {
    min-width: 0;
}

/* A module whose content rendered nothing (e.g. Related Series for a non-MAL title) takes no space. */
.widget-container:not(.edit-mode):has(> .widget-body:empty) {
    display: none;
}

/* Frameless modules in normal mode (the Cover & Info hero is itself a panel composition). */
.widget-container:not(.edit-mode):has(.av-hero) {
    background: none;
    border-color: transparent;
}

/* --- Arrange mode --- */
.widget-edit-mode {
    padding: var(--av-space-3);
    border: 1px dashed var(--av-line-strong);
    border-radius: 12px;
}

.widget-container.edit-mode {
    border-color: var(--av-line-strong);
    cursor: grab;
}

.widget-container.edit-mode:hover {
    border-color: var(--av-line-hover);
}

.widget-container.edit-mode .widget-body {
    opacity: 0.55;
    pointer-events: none;
}

.widget-container.widget-dragging {
    opacity: 0.45;
    box-shadow: var(--av-shadow-overlay);
    cursor: grabbing;
}

.widget-container.widget-drop-target {
    border-color: var(--av-text-1) !important;
    box-shadow: 0 0 0 1px var(--av-text-1);
}

.widget-header-edit {
    display: flex;
    align-items: center;
    gap: var(--av-space-2);
    min-height: 44px;
    padding: 4px 6px 4px 4px;
    border-bottom: 1px solid var(--av-line);
}

.widget-grip {
    width: 32px;
    height: 32px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    border-radius: var(--av-radius-control);
    color: var(--av-text-3);
    cursor: grab;
}

.widget-grip:hover {
    color: var(--av-text-1);
    background: var(--av-surface-2);
}

.widget-grip:active {
    cursor: grabbing;
}

.widget-title {
    flex: 1 1 auto;
    min-width: 0;
    font-size: var(--av-fs-small);
    font-weight: 500;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.widget-actions {
    display: flex;
    gap: 2px;
}

.widget-toggle {
    width: 36px;
    height: 36px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    background: transparent;
    border: 0;
    border-radius: var(--av-radius-control);
    color: var(--av-text-2);
    cursor: pointer;
}

.widget-toggle:hover {
    background: var(--av-surface-2);
    color: var(--av-text-1);
}

.widget-toggle.hidden {
    color: var(--av-text-3);
}

.widget-hidden-placeholder {
    padding: var(--av-space-3);
    color: var(--av-text-3);
    font-size: var(--av-fs-small);
    text-align: center;
}

/* Column rails in Arrange mode */
.widget-edit-mode .widget-column {
    padding: var(--av-space-2);
    border-radius: var(--av-radius-widget);
    transition: background-color var(--av-dur-fast) var(--av-ease-out), box-shadow var(--av-dur-fast) var(--av-ease-out);
}

.widget-column.column-drop-target {
    background: rgba(237, 235, 230, 0.03);
    box-shadow: inset 0 0 0 1px var(--av-line-hover);
}

.column-header-edit {
    display: flex;
    align-items: center;
    justify-content: space-between;
    min-height: 28px;
    padding: 0 2px;
}

.column-label {
    font-family: var(--av-font-mono);
    font-size: 10px;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--av-text-3);
}

.column-remove-btn {
    width: 28px;
    height: 28px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    background: transparent;
    border: 0;
    border-radius: var(--av-radius-control);
    color: var(--av-text-3);
    cursor: pointer;
}

.column-remove-btn:hover {
    background: var(--av-danger-fill);
    color: var(--av-danger);
}

.column-add-widget {
    position: relative;
}

.column-add-btn {
    width: 100%;
    min-height: 40px;
    background: transparent;
    border: 1px dashed var(--av-line-hover);
    border-radius: var(--av-radius-widget);
    color: var(--av-text-2);
    font-size: 12px;
    cursor: pointer;
}

.column-add-btn:hover {
    border-color: var(--av-text-2);
    color: var(--av-text-1);
}

.widget-picker-dropdown {
    position: absolute;
    left: 0;
    right: 0;
    top: calc(100% + 6px);
    z-index: 30;
    display: flex;
    flex-direction: column;
    padding: 6px;
    background: var(--av-surface-2);
    border: 1px solid var(--av-line-strong);
    border-radius: var(--av-radius-widget);
    box-shadow: var(--av-shadow-overlay);
}

.widget-picker-item {
    min-height: 36px;
    padding: 0 10px;
    background: transparent;
    border: 0;
    border-radius: var(--av-radius-control);
    color: var(--av-text-1);
    font-size: var(--av-fs-small);
    text-align: left;
    cursor: pointer;
}

.widget-picker-item:hover {
    background: var(--av-surface-3);
}

.widget-column-add {
    flex: 0 0 120px;
    align-self: stretch;
    min-height: 120px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 4px;
    background: transparent;
    border: 1px dashed var(--av-line-strong);
    border-radius: var(--av-radius-widget);
    color: var(--av-text-3);
    font-size: 12px;
    cursor: pointer;
}

.widget-column-add:hover {
    border-color: var(--av-text-2);
    color: var(--av-text-1);
}

.widget-column-add-count {
    font-size: 10px;
}

/* --- Toolbar --- */
.widget-toolbar {
    display: flex;
    justify-content: flex-end;
}

.widget-toolbar-actions {
    flex: 1 1 auto;
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: var(--av-space-3);
    padding: 10px 12px 10px 16px;
    background: var(--av-surface-1);
    border: 1px solid var(--av-line-strong);
    border-radius: var(--av-radius-widget);
}

.widget-toolbar-icon {
    color: var(--av-seal);
}

.widget-toolbar-text {
    flex: 1 1 240px;
    display: flex;
    flex-direction: column;
    gap: 2px;
    min-width: 0;
}

.widget-toolbar-label {
    font-size: 14px;
    font-weight: 600;
}

.widget-toolbar-sublabel {
    font-size: 12px;
    color: var(--av-text-2);
}

.widget-toolbar-buttons {
    display: flex;
    flex-wrap: wrap;
    gap: var(--av-space-2);
}

.widget-toolbar-count {
    min-width: 20px;
    padding: 1px 6px;
    border-radius: var(--av-radius-pill);
    background: var(--av-surface-3);
    color: var(--av-text-2);
    font-size: 11px;
}

/* --- Module Panel (drawer) --- */
.module-panel-overlay {
    position: fixed;
    inset: 0;
    z-index: 1100;
    background: var(--av-overlay);
    animation: av-fade-in var(--av-dur-base) var(--av-ease-out);
}

.module-panel {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    z-index: 1110;
    width: min(400px, 100vw);
    display: flex;
    flex-direction: column;
    background: var(--av-surface-1);
    border-left: 1px solid var(--av-line-strong);
    box-shadow: -16px 0 40px rgba(0, 0, 0, 0.4);
    animation: av-drawer-in var(--av-dur-slow) var(--av-ease-out);
}

.module-panel:focus {
    outline: none;
}

@keyframes av-drawer-in {
    from { transform: translateX(24px); opacity: 0; }
    to { transform: none; opacity: 1; }
}

.module-panel-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--av-space-3);
    padding: 20px 16px 16px 24px;
    border-bottom: 1px solid var(--av-line);
}

.module-panel-header h3 {
    font-family: var(--av-font-serif);
    font-size: 30px;
    font-weight: 400;
    line-height: 1.1;
}

.module-panel-close {
    width: var(--av-touch);
    height: var(--av-touch);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    background: transparent;
    border: 0;
    border-radius: var(--av-radius-control);
    color: var(--av-text-1);
    cursor: pointer;
}

.module-panel-close:hover {
    background: var(--av-surface-2);
}

.module-panel-content {
    flex: 1 1 auto;
    overflow-y: auto;
    padding: var(--av-space-4) var(--av-space-5) var(--av-space-6);
    display: flex;
    flex-direction: column;
    gap: var(--av-space-5);
}

.module-panel-section-title {
    padding-bottom: var(--av-space-2);
    font-family: var(--av-font-mono);
    font-size: var(--av-fs-label);
    font-weight: 400;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--av-text-3);
}

.module-panel-item {
    display: flex;
    align-items: center;
    gap: var(--av-space-3);
    min-height: 56px;
    padding: 8px 0;
    border-top: 1px solid var(--av-line);
}

.module-panel-item-info {
    flex: 1 1 auto;
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.module-panel-item-name {
    font-size: var(--av-fs-small);
    font-weight: 500;
}

.module-panel-item-desc {
    font-size: 12px;
    color: var(--av-text-3);
}

.module-panel-item-hidden .module-panel-item-name {
    color: var(--av-text-2);
}

.module-panel-btn {
    min-width: 36px;
    height: 36px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    flex-shrink: 0;
    padding: 0 10px;
    background: transparent;
    border: 1px solid var(--av-line-strong);
    border-radius: var(--av-radius-control);
    color: var(--av-text-2);
    font-size: 12px;
    cursor: pointer;
}

.module-panel-btn:hover {
    background: var(--av-surface-2);
    border-color: var(--av-line-hover);
    color: var(--av-text-1);
}

.module-panel-btn-add {
    color: var(--av-text-1);
}

.module-panel-full {
    font-size: 10px;
    letter-spacing: 0.04em;
    color: var(--av-text-3);
}

.module-panel-empty {
    color: var(--av-text-3);
    font-size: var(--av-fs-small);
}

/* --- Phones: columns stack (same as before) --- */
@media (max-width: 768px) {
    .widget-columns {
        flex-direction: column;
    }

    .widget-column {
        flex: 0 0 auto;
        width: 100%;
        min-width: 0; /* intrinsic widths (e.g. text inputs) must not widen the stack */
    }

    .widget-column-add {
        min-height: 48px;
        flex-basis: auto;
    }

    .widget-toolbar-buttons {
        display: grid;
        grid-template-columns: repeat(2, minmax(0, 1fr));
        width: 100%;
    }

    .widget-toolbar-buttons > button {
        justify-content: center;
        min-height: var(--av-touch);
    }
}

/* Arrange mode: the module header names the module, so its inner heading steps back */
.widget-container.edit-mode .title-panel > h4,
.widget-container.edit-mode .series-panel > h4 {
    display: none;
}
