:root {
    --primary: #2c3e50;
    --accent: #2980b9;
    --bg-gray: #f0f2f5;
    --paper: #fff;
    --nav-height: 50px;
    --guide-color: #e74c3c;
    --pot-bg: #dfe6e9
}

* {
    box-sizing: border-box
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    margin: 0;
    display: flex;
    flex-direction: column;
    height: 100vh;
    height: 100dvh;
    overflow: hidden;
    background-color: var(--bg-gray);
    user-select: none;
    -webkit-user-select: none;
    touch-action: none
}

#top-nav {
    background: var(--primary);
    color: #fff;
    display: flex;
    justify-content: space-around;
    align-items: center;
    z-index: 1000;
    flex-shrink: 0;
    box-shadow: 0 2px 5px rgba(0, 0, 0, .2);
    padding-top: env(safe-area-inset-top);
    padding-left: env(safe-area-inset-left);
    padding-right: env(safe-area-inset-right);
    height: calc(var(--nav-height) + env(safe-area-inset-top))
}

.nav-item {
    flex: 1;
    text-align: center;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: pointer;
    font-size: 16px;
    font-weight: 500;
    opacity: .7;
    border-bottom: 4px solid transparent;
    transition: .2s
}

.nav-item.active {
    opacity: 1;
    border-bottom-color: var(--accent);
    background: rgba(255, 255, 255, .1)
}

.nav-icon {
    margin-right: 8px;
    font-size: 18px
}

.view-section {
    flex-grow: 1;
    display: none;
    flex-direction: column;
    overflow: hidden;
    position: relative;
    background: var(--bg-gray);
    width: 100%;
    height: 100%
}

.view-section.active {
    display: flex
}

button {
    cursor: pointer;
    border: none;
    border-radius: 6px;
    padding: 8px 12px;
    font-size: 14px;
    font-weight: 500;
    transition: .2s;
    touch-action: manipulation
}

button:active {
    transform: scale(.96);
    opacity: .9
}

.btn-primary {
    background: var(--accent);
    color: #fff
}

.btn-success {
    background: #27ae60;
    color: #fff
}

.btn-danger {
    background: #c0392b;
    color: #fff
}

.btn-format {
    background: #8e44ad;
    color: #fff
}

.btn-ghost {
    background: transparent;
    border: 1px solid #ccc;
    color: #333
}

select.ui-select,
input.ui-input {
    padding: 8px;
    border-radius: 6px;
    border: 1px solid #ccc;
    background: #fff;
    font-size: 14px
}

input.ui-input {
    width: 60px;
    text-align: center
}

h2,
h3 {
    margin: 0 0 10px 0;
    color: var(--primary)
}

#view-classes {
    padding: 20px;
    overflow-y: auto
}

.class-card {
    background: #fff;
    padding: 15px;
    border-radius: 8px;
    margin-bottom: 10px;
    box-shadow: 0 2px 5px rgba(0, 0, 0, .05);
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-left: 5px solid transparent;
    cursor: pointer
}

.class-card.selected {
    border-left-color: var(--accent);
    background: #eef7fb
}

.student-editor {
    margin-top: 20px;
    flex-grow: 1;
    display: flex;
    flex-direction: column;
    min-height: 0
}

#class-students-area {
    width: 100%;
    flex-grow: 1;
    border: 1px solid #ccc;
    border-radius: 6px;
    padding: 10px;
    font-family: inherit;
    resize: none;
    min-height: 150px
}

.editor-card {
    background: #f8fafc;
    border-radius: 12px;
    padding: 20px;
    margin-top: 15px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, .08)
}

.gender-columns {
    display: flex;
    gap: 20px
}

.gender-column {
    flex: 1;
    display: flex;
    flex-direction: column
}

.gender-label {
    font-weight: 600;
    color: var(--primary);
    margin-bottom: 8px;
    font-size: 14px
}

.gender-area {
    flex: 1;
    min-height: 180px;
    border: 1px solid #ccc;
    border-radius: 6px;
    padding: 10px;
    font-family: inherit;
    resize: vertical
}

.auto-save-hint {
    text-align: center;
    color: #27ae60;
    font-style: italic;
    margin-top: 15px;
    font-size: 13px
}

#sidebar-collapsed {
    display: none;
    width: 40px;
    background: var(--accent);
    color: #fff;
    font-size: 18px;
    cursor: pointer;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    border-right: 1px solid #ccc
}

#sidebar-collapsed:hover {
    background: #1a6da3
}

#toolbar {
    padding: 8px;
    background: #fff;
    border-bottom: 1px solid #ccc;
    display: flex;
    gap: 8px;
    align-items: center;
    overflow-x: auto;
    flex-shrink: 0;
    white-space: nowrap;
    -webkit-overflow-scrolling: touch
}

.btn-toggle {
    background-color: #ecf0f1;
    color: #2c3e50;
    border: 1px solid #bdc3c7
}

.btn-toggle.active {
    background-color: #f1c40f !important;
    color: #000 !important;
    font-weight: 700;
    border: 2px solid #d35400 !important
}

#versions-bar {
    padding: 8px 10px;
    background: #e0e0e0;
    display: flex;
    gap: 10px;
    overflow-x: auto;
    min-height: 50px;
    align-items: center;
    flex-shrink: 0
}

.version-tag {
    background: #fff;
    padding: 8px 14px;
    border-radius: 6px;
    font-size: 14px;
    display: flex;
    align-items: center;
    gap: 12px;
    border: 1px solid #bbb;
    white-space: nowrap;
    cursor: pointer;
    min-height: 40px;
    box-shadow: 0 1px 3px rgba(0, 0, 0, .1)
}

.version-tag:hover {
    background: #f8f8f8
}

.version-tag .del-btn {
    color: #c0392b;
    font-weight: 700;
    font-size: 18px;
    padding: 4px 8px;
    cursor: pointer;
    border-radius: 4px
}

.version-tag .del-btn:hover {
    background: rgba(192, 57, 43, .1)
}

#viewport {
    flex-grow: 1;
    position: relative;
    overflow: hidden;
    cursor: grab;
    touch-action: none;
    background-image: radial-gradient(#4a5568 1px, transparent 1px);
    background-size: 25px 25px
}

#viewport:active {
    cursor: grabbing
}

#world {
    position: absolute;
    top: 0;
    left: 0;
    transform-origin: 0 0;
    will-change: transform
}

#a4-paper {
    background: #fff;
    position: absolute;
    left: 0;
    top: 0;
    box-shadow: 0 0 30px rgba(0, 0, 0, .5);
    transition: width .3s, height .3s
}

#a4-paper.portrait {
    width: 210mm;
    height: 297mm
}

#a4-paper.landscape {
    width: 297mm;
    height: 210mm
}

#lasso-box {
    position: absolute;
    border: 1px solid #2980b9;
    background-color: rgba(41, 128, 185, .2);
    display: none;
    pointer-events: none;
    z-index: 9999
}

.desk {
    position: absolute;
    background: #ecf0f1;
    border: 2px solid #bdc3c7;
    border-radius: 6px;
    display: flex;
    justify-content: center;
    align-items: center;
    box-shadow: 2px 2px 5px rgba(0, 0, 0, .1);
    touch-action: manipulation;
    pointer-events: auto;
    z-index: 10
}

.desk.selected {
    border-color: var(--accent);
    border-width: 3px;
    z-index: 100;
    box-shadow: 0 0 15px rgba(41, 128, 185, .8)
}

.desk.single {
    width: 70px;
    height: 70px
}

.desk.double {
    width: 144px;
    height: 70px;
    flex-direction: row
}

.desk.teacher {
    width: 144px;
    height: 70px;
    background: #dfe6e9;
    border-radius: 12px
}

.desk.vertical {
    width: 70px;
    height: 144px;
    flex-direction: column
}

.seat {
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    text-align: center;
    font-size: 14px;
    overflow: hidden;
    height: 100%;
    width: 100%;
    position: relative;
    white-space: normal;
    line-height: 1.1;
    padding: 1px;
    word-wrap: break-word
}

.desk.double:not(.vertical) .seat:first-child {
    border-right: 1px dashed #ccc
}

.desk.double.vertical .seat:first-child {
    border-bottom: 1px dashed #ccc
}

.placeholder-text {
    color: #2980b9;
    font-weight: 700;
    font-size: 10px;
    background: rgba(41, 128, 185, .1);
    padding: 4px;
    border-radius: 4px;
    pointer-events: none;
    display: none
}

.desk.selected .placeholder-text {
    display: block
}

.desk-controls {
    position: absolute;
    top: -90px;
    left: 50%;
    transform: translateX(-50%);
    background: #fff;
    border: 1px solid var(--accent);
    border-radius: 8px;
    display: none;
    padding: 5px;
    gap: 8px;
    z-index: 200;
    box-shadow: 0 4px 10px rgba(0, 0, 0, .2)
}

#group-controls-overlay {
    position: absolute;
    background: #fff;
    border: 2px solid var(--accent);
    border-radius: 12px;
    display: none;
    padding: 8px;
    gap: 12px;
    z-index: 2000;
    box-shadow: 0 5px 15px rgba(0, 0, 0, .3);
    transform: translate(-50%, -100%);
    margin-top: -25px
}

.ctrl-btn {
    width: 40px;
    height: 40px;
    background: #f8f9fa;
    border: 1px solid #ddd;
    border-radius: 50%;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 18px;
    touch-action: manipulation
}

.ctrl-btn:active {
    background: #eee
}

.ctrl-btn.large {
    width: 50px;
    height: 50px;
    font-size: 22px
}

.move-handle {
    background: var(--accent);
    color: #fff;
    cursor: move
}

.guide-line {
    position: absolute;
    background-color: var(--guide-color);
    z-index: 500;
    pointer-events: none
}

.guide-line.vertical {
    width: 2px;
    height: 100%;
    top: 0;
    border-left: 2px dashed var(--guide-color);
    background: transparent
}

.guide-line.horizontal {
    height: 2px;
    width: 100%;
    left: 0;
    border-top: 2px dashed var(--guide-color);
    background: transparent
}

.guide-line.page-center {
    border-color: red;
    border-style: solid;
    opacity: .8
}

#zoom-ui {
    position: absolute;
    bottom: 20px;
    right: 20px;
    display: flex;
    flex-direction: column;
    gap: 10px;
    z-index: 90
}

.z-btn {
    width: 44px;
    height: 44px;
    border-radius: 50%;
    background: #fff;
    border: 1px solid #999;
    font-size: 20px;
    display: flex;
    justify-content: center;
    align-items: center;
    touch-action: manipulation
}

#view-groups {
    flex-direction: row;
    height: 100%;
    width: 100%
}

#group-sidebar {
    width: 280px;
    background: #fff;
    border-right: 1px solid #ccc;
    display: flex;
    flex-direction: column;
    padding: 10px;
    overflow: hidden;
    flex-shrink: 0
}

#group-sidebar.hidden {
    display: none
}

#toggle-sidebar-btn {
    z-index: 100;
    background: var(--accent);
    color: #fff;
    cursor: pointer;
    box-shadow: 2px 0 5px rgba(0, 0, 0, .2)
}

#student-selection-list {
    flex-grow: 1;
    overflow-y: auto;
    margin-top: 10px;
    border: 1px solid #eee;
    border-radius: 4px;
    padding-bottom: 20px
}

.select-item {
    padding: 12px 10px;
    border-bottom: 1px solid #eee;
    display: flex;
    align-items: center;
    gap: 10px;
    cursor: pointer
}

.select-item:active {
    background: #f0f0f0
}

.select-item.absent {
    opacity: .5
}

.chk-indicator {
    width: 22px;
    height: 22px;
    border: 2px solid var(--accent);
    border-radius: 4px;
    display: flex;
    justify-content: center;
    align-items: center;
    color: #fff;
    background: #fff;
    flex-shrink: 0
}

.select-item.checked .chk-indicator {
    background: var(--accent)
}

.select-item.checked .chk-indicator::after {
    content: '✓';
    font-size: 16px;
    font-weight: 700
}

#group-workspace {
    flex-grow: 1;
    background: #f0f2f5;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    position: relative
}

#group-controls {
    padding: 10px;
    background: #fff;
    border-bottom: 1px solid #ccc;
    display: flex;
    gap: 15px;
    align-items: center;
    flex-wrap: wrap;
    flex-shrink: 0;
    box-shadow: 0 1px 3px rgba(0, 0, 0, .05);
    z-index: 10
}

#pots-interface {
    display: none;
    flex-direction: column;
    background: #e8ecf1;
    border-bottom: 1px solid #ccc;
    padding: 10px;
    height: 35vh;
    min-height: 250px;
    flex-shrink: 0
}

#pots-interface.hidden-pots {
    display: none !important
}

#pots-container {
    display: flex;
    gap: 10px;
    padding-bottom: 5px;
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    height: 100%;
    align-items: stretch
}

.pot-box {
    min-width: 220px;
    width: 220px;
    background: #fff;
    border: 2px dashed #95a5a6;
    border-radius: 8px;
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
    max-height: 100%;
    overflow: hidden
}

.pot-header {
    background: #95a5a6;
    color: #fff;
    padding: 8px;
    text-align: center;
    font-weight: 700;
    border-radius: 6px 6px 0 0;
    flex-shrink: 0
}

.pot-content {
    padding: 5px;
    flex-grow: 1;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    background: rgba(0, 0, 0, .02)
}

#group-container {
    flex-grow: 1;
    padding: 20px;
    overflow-y: auto;
    display: flex;
    flex-wrap: wrap;
    gap: 15px;
    align-content: flex-start;
    -webkit-overflow-scrolling: touch
}

.group-box {
    background: #fff;
    border-radius: 8px;
    width: 240px;
    min-height: 160px;
    box-shadow: 0 3px 6px rgba(0, 0, 0, .1);
    display: flex;
    flex-direction: column;
    border-top: 6px solid #ccc;
    margin-bottom: 10px;
    transition: transform .2s
}

.group-header {
    padding: 10px;
    font-weight: 700;
    text-align: center;
    border-bottom: 1px solid #eee;
    font-size: 1.1em;
    color: var(--primary)
}

.group-members {
    flex-grow: 1;
    padding: 8px;
    min-height: 50px
}

.group-member-item {
    background: #f8f9fa;
    margin-bottom: 6px;
    padding: 10px;
    border-radius: 6px;
    font-size: 14px;
    border: 1px solid #eee;
    display: flex;
    justify-content: space-between;
    align-items: center;
    box-shadow: 0 1px 2px rgba(0, 0, 0, .05);
    user-select: none;
    -webkit-user-select: none;
    touch-action: pan-y
}

.group-member-item.absent {
    opacity: .4;
    background: #eee
}

.drag-handle {
    color: #666;
    font-weight: 900;
    letter-spacing: .5px;
    padding: 6px 8px;
    cursor: grab;
    border-radius: 6px;
    background: rgba(0, 0, 0, .04)
}

.group-member-item:active {
    cursor: grabbing
}

.grp-color-0 {
    border-top-color: #e74c3c
}

.grp-color-1 {
    border-top-color: #3498db
}

.grp-color-2 {
    border-top-color: #2ecc71
}

.grp-color-3 {
    border-top-color: #f1c40f
}

.grp-color-4 {
    border-top-color: #9b59b6
}

.grp-color-5 {
    border-top-color: #e67e22
}

.grp-unassigned {
    border-top-color: #7f8c8d;
    background: #f5f5f5
}

#random-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, .9);
    z-index: 3000;
    display: none;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    color: #fff;
    backdrop-filter: blur(5px)
}

#wheel-container {
    position: relative;
    width: 300px;
    height: 300px
}

#wheel-canvas {
    width: 100%;
    height: 100%
}

#wheel-pointer {
    position: absolute;
    top: -20px;
    left: 50%;
    transform: translateX(-50%);
    font-size: 40px;
    z-index: 10
}

#winner-display {
    font-size: 3rem;
    font-weight: 800;
    text-align: center;
    padding: 20px;
    text-shadow: 0 0 20px rgba(255, 255, 255, .2);
    margin-top: 20px;
    opacity: 0;
    transition: opacity .3s
}

#modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, .6);
    display: none;
    align-items: center;
    justify-content: center;
    z-index: 2500
}

.modal {
    background: #fff;
    padding: 20px;
    border-radius: 12px;
    width: 320px;
    max-height: 80vh;
    display: flex;
    flex-direction: column
}

.modal h3 {
    margin-bottom: 15px
}

.student-option {
    padding: 12px;
    border-bottom: 1px solid #eee;
    cursor: pointer
}

.student-option:hover {
    background: #f5f5f5
}

.student-option.swap {
    color: var(--accent);
    font-weight: 500
}

.shuffle-options {
    display: flex;
    flex-direction: column;
    gap: 10px;
    margin: 15px 0
}

.shuffle-option {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px;
    border: 1px solid #ddd;
    border-radius: 6px;
    cursor: pointer
}

.shuffle-option:hover {
    background: #f5f5f5
}

.shuffle-option.selected {
    border-color: var(--accent);
    background: #eef7fb
}

.shuffle-option input {
    margin: 0
}

.gender-area {
    width: 100%;
    flex-grow: 1;
    border: 1px solid #ccc;
    border-radius: 6px;
    padding: 10px;
    font-family: inherit;
    resize: none;
    min-height: 150px
}

#student-selection-list,
.pot-content,
#group-container {
    touch-action: pan-y;
    overscroll-behavior: contain
}

#pots-container {
    touch-action: pan-x;
    overscroll-behavior: contain
}

@media (max-width:900px) {
    #view-groups {
        flex-direction: column
    }

    #group-sidebar {
        width: 100%;
        height: 25vh;
        border-right: none;
        border-bottom: 2px solid #ccc
    }

    #group-sidebar.hidden {
        display: none
    }

    #group-workspace {
        height: 75vh;
        flex: 1
    }

    #group-container {
        justify-content: center;
        padding: 10px
    }

    .group-box {
        width: 46%;
        margin-bottom: 10px
    }

    #group-controls {
        justify-content: space-between;
        padding: 8px
    }

    .nav-item {
        font-size: 12px;
        flex-direction: column;
        gap: 2px
    }

    .nav-icon {
        margin: 0;
        font-size: 20px
    }

    .pot-box {
        width: 180px;
        min-width: 180px
    }

    #wheel-container {
        width: 250px;
        height: 250px
    }

    #winner-display {
        font-size: 2rem
    }
}